CDUTCM OJ 1292 walk in maze

1292: walk in maze

时间限制: 1 Sec  内存限制: 128 MB
提交: 14  解决: 10
[提交][状态][讨论版][命题人:外部导入]

题目描述

Define a two-dimensional array int b[n][n], and it represents a maze.The number 1 represents the wall,and the number 0 reprensents the walking path.You are only allowed to walk vertically or horizontally in the maze.You need to figure out if you can walk from one starting point to one terminal point(the starting point and the terminal point are setted by yourself).

输入

The input consists of several groups of test cases.
The first line of every cases is a integer n,which represents a n*n maze.
Then you should input n rows and n columns,which represent the layout of the maze.
The following line consists of two integers x1,y1,which represent the starting point's abscissa and ordinate.
The last line consists of two integers x2,y2,which represent the terminal point's abscissa and ordinate.
(The starting point and the terminal point must be the walking path,not the wall)

输出

If you can walk from the starting point to the terminal point,output the number of steps you need to walk.(One adjacent movement is one step.)
If you can't walk from the starting point to the terminal point,output"Unable to pass through"(Don't output the "").

样例输入

5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
1 1
5 5
5
0 1 0 1 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 1
0 0 0 1 0
1 1
5 5

样例输出

9
Unable to pass through

 

大意是:走迷宫,给一个起点一个终点,走出去就给出步数,没走出去就输出  Unable to pass through  一道bfs

注意:关键字例如:end ,map 等的定义,否则会有编译错误

核心代码:

  1. for(int i=0;i<4;i++)
  2.         {
  3.             to.x=q.front().x+dx[i];//选择走那一步,上下左右,可以走的地方
  4.             to.y=q.front().y+dy[i];
  5.             to.temp=q.front().temp+1;//每走一步,步数就加1
  6.             if(to.x>0&&to.y>0&&to.x<=n&&to.y<=n&&!map_1[to.x][to.y])//判断走的下一步是否符合条件(没有墙,并且在地图范围内)
  7.             {
  8.                 map_1[q.front().x][q.front().y]=1;//走过的就给他一堵墙表示已经走过,不能往回走
  9.                 q.push(to);
  10.             } 
  11.             if(to.x==en.x&&to.y==en.y)//走到终点,返回步数temp
  12.             {
  13.                 return to.temp;
  14.             }
  15.         }
  1. #include<bits/stdc++.h>

  2. using namespace std;

  3. int map_1[50][50]={0};

  4. int n;

  5. struct point {

  6.     int x;

  7.     int y;

  8.     int temp;

  9. };

  10. point start;

  11. point en;

  12. int ans;

  13. int dx[4]={-1,0,0,1};

  14. int dy[4]={0,1,-1,0};

  15. queue<point>q;

  16. int bfs()

  17. {

  18.     while(!q.empty()) q.pop();

  19.     q.push(start);

  20.     while(!q.empty())

  21.     {

  22.     struct point to;

  23.         for(int i=0;i<4;i++)

  24.         {

  25.             to.x=q.front().x+dx[i];

  26.             to.y=q.front().y+dy[i];

  27.             to.temp=q.front().temp+1;

  28.             if(to.x>0&&to.y>0&&to.x<=n&&to.y<=n&&!map_1[to.x][to.y])

  29.             {

  30.                 map_1[q.front().x][q.front().y]=1;

  31.                 q.push(to);

  32.             } 

  33.             if(to.x==en.x&&to.y==en.y)

  34.             {

  35.                 return to.temp;

  36.             }

  37.         }

  38.             q.pop();

  39.     }

  40.     return 0;

  41. }

  42. int main()

  43. {

  44.     while(cin>>n)

  45.     {

  46.         for(int i=1;i<=n;i++)

  47.         {

  48.             for(int j=1;j<=n;j++)

  49.             {

  50.                 cin>>map_1[i][j];

  51.             }

  52.         }

  53.         cin>>start.x>>start.y>>en.x>>en.y;

  54.         start.temp=1;

  55.         map_1[start.x][start.y]=1;

  56.         ans=bfs();

  57.         if(ans==0)

  58.         {

  59.             cout<<"Unable to pass through"<<endl;

  60.         }

  61.         else

  62.         cout<<ans<<endl;

  63.     } 

  64.     return 0;

猜你喜欢

转载自blog.csdn.net/xiaoa_axiao/article/details/81129152