nyoj 483 Nightmare 【bfs】

来源:互联网 发布:三菱数控系统编程 编辑:程序博客网 时间:2024/05/16 04:31

Nightmare

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
描述
Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The labyrinth has an exit, Ignatius should get out of the labyrinth before the bomb explodes. The initial exploding time of the bomb is set to 6 minutes. To prevent the bomb from exploding by shake, Ignatius had to move slowly, that is to move from one area to the nearest area(that is, if Ignatius stands on (x,y) now, he could only on (x+1,y), (x-1,y), (x,y+1), or (x,y-1) in the next minute) takes him 1 minute. Some area in the labyrinth contains a Bomb-Reset-Equipment. They could reset the exploding time to 6 minutes.

Given the layout of the labyrinth and Ignatius' start position, please tell Ignatius whether he could get out of the labyrinth, if he could, output the minimum time that he has to use to find the exit of the labyrinth, else output -1.

Here are some rules:
1. We can assume the labyrinth is a 2 array.
2. Each minute, Ignatius could only get to one of the nearest area, and he should not walk out of the border, of course he could not walk on a wall, too.
3. If Ignatius get to the exit when the exploding time turns to 0, he can't get out of the labyrinth.
4. If Ignatius get to the area which contains Bomb-Rest-Equipment when the exploding time turns to 0, he can't use the equipment to reset the bomb.
5. A Bomb-Reset-Equipment can be used as many times as you wish, if it is needed, Ignatius can get to any areas in the labyrinth as many times as you wish.
6. The time to reset the exploding time can be ignore, in other words, if Ignatius get to an area which contain Bomb-Rest-Equipment, and the exploding time is larger than 0, the exploding time would be reset to 6.
输入
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case starts with two integers N and M(1<=N,Mm=8) which indicate the size of the labyrinth. Then N lines follow, each line contains M integers. The array indicates the layout of the labyrinth.
There are five integers which indicate the different type of area in the labyrinth:
0: The area is a wall, Ignatius should not walk on it.
1: The area contains nothing, Ignatius can walk on it.
2: Ignatius' start position, Ignatius starts his escape from this position.
3: The exit of the labyrinth, Ignatius' target position.
4: The area contains a Bomb-Reset-Equipment, Ignatius can delay the exploding time by walking to these areas.

输出
For each test case, if Ignatius can get out of the labyrinth, you should output the minimum time he needs, else you should just output -1.

样例输入
23 32 1 11 1 01 1 34 82 1 1 0 1 1 1 01 0 4 1 1 0 4 11 0 0 0 0 0 0 11 1 1 4 1 1 1 3
样例输出
4-1
上传者
ACM_林志强

思路:

          这道题和杭电的蚂蚁的那道题类似,在进行处理的时候,要注意行列的最大值,并且要区分行和列所对应的m和n,然后把bfs改编一下就OK了!(注意:因为有时间限制,所以要进行判断!)

代码:

#include <stdio.h>#include <string.h>#include <algorithm>#include <queue>#define INF 0x3f3f3f3fusing namespace std;int mp[10][10];int n,m;//n行m列 int x,y,ex,ey;int flag;int ans;int vis[10][10];int dx[4]={0,1,-1,0};int dy[4]={1,0,0,-1};struct node {int x,y,step,time;bool friend operator < (node a,node b)//用优先队列做的,时间复杂度降低! {return a.step>b.step;}}a,temp;void inin(){flag=0;ans=INF;//flag记录是否能到达终点! for(int i=1;i<=n;i++)//ans记录到达终点所用的时间的最小值! {for(int j=1;j<=m;j++){scanf("%d",&mp[i][j]);if(mp[i][j]==2)//起点 {x=i;y=j;}if(mp[i][j]==3)//终点! {ex=i;ey=j;}}}}int judge(){if(temp.x<1||temp.x>n)return 0;//这一点n和m一定不能写反了! if(temp.y<1||temp.y>m)return 0;//就这一点写反了,错了10几次! if(mp[temp.x][temp.y]==0) return 0;if(vis[temp.x][temp.y]==1) return 0;if(temp.step>=ans)return 0;if(a.time==1)return 0;//比模板多了这一句! return 1;//因为在判断处等于1,然后在下面减1,就变成0了,到终点剩余时间不能为0就不符合了! }//所以在这里面时间必须大于1,才能往下执行 void bfs(){a.x=x;a.y=y;a.step=0;a.time=6;priority_queue<node>q;q.push(a);memset(vis,0,sizeof(vis));vis[x][y]=1;while(!q.empty()){a=q.top();q.pop();for(int i=0;i<4;i++){temp.x=a.x+dx[i];temp.y=a.y+dy[i];temp.step=a.step+1;if(judge()){if(mp[temp.x][temp.y]==4)//这一句不能放到if语句外面 {//如果放到外面,如果满足此if语句,也不能通过judge(); temp.time=6;//因为你为vis赋值了! vis[temp.x][temp.y]=1;//因为第二次经过这个点之后所用的时间肯定比第一次多! }elsetemp.time=a.time-1;if(temp.x==ex&&temp.y==ey){ans=temp.step;flag=1;return;}q.push(temp);}}    }}int main(){int T;scanf("%d",&T);while(T--){scanf("%d%d",&n,&m);inin(); bfs();if(flag==0)printf("-1\n");elseprintf("%d\n",ans);}return 0;}



 

0 0