九度oj 1335

题目描述:

sun所在学校每年都要举行电脑节,今年电脑节有一个新的趣味比赛项目叫做闯迷宫。
sun的室友在帮电脑节设计迷宫,所以室友就请sun帮忙计算下走出迷宫的最少步数。
知道了最少步数就可以辅助控制比赛难度以及去掉一些没有路径到达终点的map。
比赛规则是:从原点(0,0)开始走到终点(n-1,n-1),只能上下左右4个方向走,只能在给定的矩阵里走。

输入:

输入有多组数据。
每组数据输入n(0<n<=100),然后输入n*n的01矩阵,0代表该格子没有障碍,为1表示有障碍物。
注意:如果输入中的原点和终点为1则这个迷宫是不可达的。

输出:

对每组输入输出该迷宫的最短步数,若不能到达则输出-1。

样例输入:
2
0 1
0 0
5
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
0 1 1 1 0
1 0 1 0 0
样例输出:
2
8
#include<iostream>
#include<queue>
#define true 1
#define false 0
using namespace std;
struct A{
       int x;
       int y;
       int t;
       };
       int maze[110][110];
       bool mark[110][110];
       queue<A> q;
       int go[][2]={
           {-1,0},
           {1,0},
           {0,1},
           {0,-1}
           };
           int BFS(int n)
           {
               while(q.empty()==0)
               {
               A now=q.front();
               q.pop();
               for(int i=0;i<4;i++)
               {
                       int nx=now.x+go[i][0];
                       int ny=now.y+go[i][1];
                       int nt=now.t+1;
                       if(nx<0||nx>=n||ny<0||ny>=n)
                       continue;
                       if(maze[nx][ny]==1)
                       continue;
                       if(mark[nx][ny]==0)
                       continue;
                       if(nx==n-1&&ny==n-1)
                       return nt;
                       A tmp;
                       tmp.x=nx;
                       tmp.y=ny;
                       tmp.t=nt;
                       q.push(tmp);
                       mark[nx][ny]=false;
                       }
                       }
                       return -1;
                       }
                       int main()
                       {
                           int n;
                           while(cin>>n)
                           {
                                        for(int i=0;i<n;i++)
                                        {
                                                for(int j=0;j<n;j++)
                                                {
                                                        mark[i][j]=true;
                                                        maze[i][j]=0;
                                                        }
                                                        }
                                                        while(q.empty()==0)
                                                        {
                                                                           q.pop();
                                                                           }
                                                                           for(int i=0;i<n;i++)
                                                                           {
                                                                                   for(int j=0;j<n;j++)
                                                                                   {
                                                                                           cin>>maze[i][j];
                                                                                           }
                                                                                           }
                                                                                           if(maze[0][0]==1||maze[n-1][n-1]==1)
                                                                                           {
                                                                                           cout<<"-1"<<endl;
                                                                                           }
                                                                                           else
                                                                                           {
                                                                                               A tmp;
                                                                                               tmp.x=0;
                                                                                               tmp.y=0;
                                                                                               tmp.t=0;
                                                                                               q.push(tmp);
                                                                                               mark[0][0]=false;
                                                                                               int ans=BFS(n);
                                                                                               if(ans==-1)
                                                                                               {
                                                                                                         cout<<"-1"<<endl;
                                                                                                         }
                                                                                                         else
                                                                                                         cout<<ans<<endl;
                                                                                                         }
                                                                                                         }
                                                                                                         }
           


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值