zoukankan      html  css  js  c++  java
  • 走迷宫1 bnu 1054

    走迷宫是很有趣的一种游戏,能够锻炼人的记忆力和思维.现在,HK被困在一个迷宫里面了,请你帮助他找到一条最短的路径,能够让他走出迷宫.

    迷宫使用一个N*M的矩阵来描述,矩阵中用'.'代表空格可以通行,用'*'代表障碍物,用'S'代表出发点,用'T'代表出口.例如下面的一个矩阵就描述了一个8*8的迷宫

    .....T..
    ..*****.
    ......*.
    *.***.*.
    ......*.
    .****.*.
    S..*....
    ........

    每个字符代表1个格子,HK只能在格子间按上下左右的方向移动

     

    Input

    每个输入文件只包含一组输入数据.
    每组数据第一行是两个正整数N和M(N,M<=100).
    接着是一个N*M的矩阵.

     

    Output

    如果HK能够走出迷宫,输出最少需要的步数;否则输出-1.

     

    Sample Input

    8 8
    .....T..
    ..*****.
    ......*.
    *.***.*.
    ......*.
    .****.*.
    S..*....
    ........

    Sample Output

    11

    第一次queue,其实不难;
    <注意:不知道是OJ的问题还是什么,#define N 好像会 RE,以后还是少用的呵。>
     1 #include<iostream>
     2 #include<queue>
     3 #include<cstring>
     4 using namespace std;
     5 
     6 const int maxn = 100 + 10;
     7 char a[maxn][maxn];
     8 int vis[maxn][maxn],n,m;
     9 int ax[5]={-1,0,1,0};
    10 int ay[5]={0,-1,0,1};
    11 struct Thing 
    12 {
    13     int x, y;
    14     int step;
    15 }str;
    16 Thing end,now;
    17 
    18 int bfs ()
    19 {
    20     queue<Thing>que;
    21     que.push(str);
    22     while(!que.empty())
    23     {
    24        now=que.front();
    25        if(now.x==end.x&&now.y==end.y)
    26        {
    27          return now.step;
    28        }
    29        que.pop();
    30        for(int i=0;i<4;i++)
    31        {
    32             Thing temp;
    33             temp.x=now.x+ax[i];
    34             temp.y=now.y+ay[i];
    35             temp.step=now.step+1;
    36             if(temp.x >=0 &&temp.y<n&&temp.y>=0&&temp.y<m && !vis[temp.x][temp.y] && a[temp.x][temp.y]!='*')
    37             {
    38                 vis[temp.x][temp.y]=1;
    39                 que.push(temp);
    40          }
    41        }    
    42     }
    43     return -1;
    44 }
    45 int main ()
    46 {
    47     while(cin >> n >> m)
    48     {
    49         memset(vis,0,sizeof(vis));
    50         for(int i=0;i<n;i++)
    51         {
    52          for(int j=0;j<m;j++)
    53          {
    54             cin >> a[i][j];
    55             if(a[i][j]=='S')
    56             {
    57                 str.x=i;
    58                 str.y=j;
    59                 str.step=0;
    60                 vis[i][j]=1;
    61             }
    62             if(a[i][j]=='T')
    63             {
    64                end.x=i;
    65                end.y=j;     
    66             }
    67          }
    68         }
    69         //printf("-%d%d--%d%d-
    ",str.x,str.y,end.x,end.y);
    70         cout << bfs() << endl;
    71     }
    72     return 0;
    73 }
    View Code
  • 相关阅读:
    jquerymobile 页面间URL传值
    xcode 静态链接库的问题
    iPad 用户体验关键要素
    Enable SharePoint Designer for Project Web App PWA 2010
    后台定位
    做一个iPhone应用需要花多少钱?
    ios无法获取坐标
    重装系统后ORACLE数据库恢复的方法
    【Web】百度有聊官网的一些布局不好之处
    【Pagoda】在pagodabox里建立项目并连接数据库
  • 原文地址:https://www.cnblogs.com/ace-top/p/3452826.html
Copyright © 2011-2022 走看看