初次编辑时间:2011-07-03
source page: http://acm.hdu.edu.cn/showproblem.php?pid=1072Nightmare
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8011 Accepted Submission(s): 3855
Problem Description
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.
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.
Input
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.
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.
Output
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.
Sample Input
3 3 3 2 1 1 1 1 0 1 1 3 4 8 2 1 1 0 1 1 1 0 1 0 4 1 1 0 4 1 1 0 0 0 0 0 0 1 1 1 1 4 1 1 1 3 5 8 1 2 1 1 1 1 1 4 1 0 0 0 1 0 0 1 1 4 1 0 1 1 0 1 1 0 0 0 0 3 0 1 1 1 4 1 1 1 1 1
Sample Output
4 -1 13
source code:
两种方法:
题目很普通,就是比平常的BFS题多了一个时间重置点,对此只要另开一个数组,记录到达当前点的剩余时间,大于走这个点,反之不走。
先是数组模拟:
#include<stdio.h>
#include<string.h>
struct node{
int x;
int y;
int time;
int step;
}q[1000],p;//定义队列q和中间变量p
int n,m,si,sj,T,front,tail,rear,step;
int dx[4]={1,0,0,-1};
int dy[4]={0,1,-1,0};
int mark[10][10],map[10][10];
void BFS()
{
int i,j;
//第一个点入队
q[0].x=si;
q[0].y=sj;
q[0].time=T;
q[0].step=0;
mark[si][sj]=6;
front=0; tail=0; rear=0;
while(front<=rear){//队首指针不等于队尾指针
for(j=0;j<4;j++){//扩展队列中的每一个新加入的点
p.x=q[front].x+dx[j];
p.y=q[front].y+dy[j];
p.time=q[front].time-1;
p.step=q[front].step+1;
if(map[p.x][p.y]==3&&p.time>0){
step=p.step;
return ;
}
if(map[p.x][p.y]==4&&p.time>0){
//mark[p.x][p.y]=6; //这行让我很蛋疼,刚开始一直错,检查了好几个小时才发现它是多余的
p.time=6;
}
if(p.x>=0&&p.y>=0&&p.x<n&&p.y<m&&map[p.x][p.y]!=0&&p.time>mark[p.x][p.y]){
//符合条件就入队,即添加到队尾
rear++;
q[rear].x=p.x;
q[rear].y=p.y;
q[rear].time=p.time;
q[rear].step=p.step;
mark[p.x][p.y]=p.time;
//printf("%d %d %d %d\n",q[rear].x,q[rear].y,q[rear].time,q[rear].step);
}
}
front++;
}
}
int main(){
int i,j,ca;
scanf("%d",&ca);
while(ca--){
scanf("%d%d",&n,&m);
memset(mark,0,sizeof(mark));
for(i=0;i<n;i++)
for(j=0;j<m;j++){
scanf("%d",&map[i][j]);
if(map[i][j]==2){
si=i;sj=j;
}
}
T=6; step=-1;
BFS();
/*
printf("\n");
for(i=0;i<n;i++){
for(j=0;j<m;j++)
printf("%d ",mark[i][j]);
printf("\n");
}
printf("\n");
*/
printf("%d\n",step);
}
return 0;
}
再是STL中的队列:
不知为什么用C写竟然错了,C++就没错,看来已有运用STL函数还是用C++比较安全
#include<iostream>
#include<queue> //包含的头文件
using namespace std;
struct Node
{
int x,y;
int time;
int step;
}p,q;
int map[9][9];
int mark[9][9];
int m,n,mins,si,sj;
int dir[4][2]={-1,0,1,0,0,-1,0,1};
void bfs()
{
int k;
queue<Node>Q;//建立node类型的队列,队列名为Q
p.step=0;
p.time=6;
p.x=si;
p.y=sj;
Q.push(p);//p入队
mark[si][sj]=p.time;
while(!Q.empty()){//若队列非空
q=Q.front();//取队首元素
Q.pop(); //清除第一个元素
下面是另外的含义
//Q.back(); 显示最后一个元素
//Q.size();输出现有元素个数
if(map[q.x][q.y]==3&&q.time>0){
mins=q.step;
return ;
}
if(map[q.x][q.y]==4)
q.time=6;
for(k=0;k<4;k++){
p.x=q.x+dir[k][0];
p.y=q.y+dir[k][1];
p.time=q.time-1;
p.step=q.step+1;
if(p.x>=0&&p.x<m&&p.y>=0&&p.y<n&&map[p.x][p.y]!=0&&q.time>0&&mark[p.x][p.y]<p.time){
//符合条件则入队
mark[p.x][p.y]=p.time;
Q.push(p);//从已有元素后增加元素
}
}
}
}
int main(){
int t;
cin>>t;
while(t--){
cin>>m>>n;
int i,j;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
cin>>map[i][j];
if(map[i][j]==2){
si=i;
sj=j;
}
mark[i][j]=0;
}
}
mins=-1;
bfs();
cout<<mins<<endl;
}
return 0;
}