Tempter of the Bone II
Time Limit: 10000/5000 MS (Java/Others) | Memory Limit: 98304/32768 K (Java/Others) |
---|
Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze was changed and the way he came in was lost.He realized that the bone was a trap, and he tried desperately to get out of this maze.
The maze was a rectangle with the sizes of N by M. The maze is made up of a door,many walls and many explosives. Doggie need to reach the door to escape from the tempter. In every second, he could move one block to one of the upper, lower, left or right neighboring blocks. And if the destination is a wall, Doggie need another second explode and a explosive to explode it if he had some explosives. Once he entered a block with explosives,he can take away all of the explosives. Can the poor doggie survive? Please help him.
Input
The input consists of multiple test cases. The first line of each test case contains two integers N, M,(2 <= N, M <= 8). which denote the sizes of the maze.The next N lines give the maze layout, with each line containing M characters. A character is one of the following:
‘X’: a block of wall;
‘S’: the start point of the doggie;
‘D’: the Door;
‘.’: an empty block;
‘1’–‘9’:explosives in that block.
Note,initially he had no explosives.
The input is terminated with two 0’s. This test case is not to be processed.
Output
For each test case, print the minimum time the doggie need to escape if the doggie can survive, or -1 otherwise.
Sample Input
4 4
SX…
XX…
…
1…D
4 4
S.X1
…
…XX
…XD
0 0
Sample Output
-1
9
遇到炸弹装兜兜里,遇到墙就炸了它,是平路就直接走,注意实时更新身上的炸弹数,然后用这次的炸弹数跟上次的比,如果多的话,说明上次有可能炸错墙了走了冤枉路或者这条路线上的炸弹本来就多(炸弹多意味着能炸更多的墙,更可能到终点),将这种情况入队,更新状态。
因为有可能炸错墙,所以不能在原图中修改,在结构体中用一个state[]数组记录状态,state[i][j]表示在走到(i,j)的时候的炸弹数。
参考范文: https://blog.csdn.net/DIOML/article/details/50638834
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
int n, m, ans, sx, sy;
char map[10][10];
int dir[4][2] = { 0, 1, 0, -1, 1, 0, -1, 0 };
struct node
{
int x, y, tnt, step;//横纵坐标,炸弹数,步数
int state[8][8];//储存状态
int check(int x, int y)//检查坐标合法
{
if(x>=0&&x<n&&y>=0&&y<m)
return 1;
return 0;
}
};
int bfs()
{
queue<node>p;
while(!p.empty())
p.pop();
node now;
memset(now.state, -1, sizeof(now.state));//防止和0炸弹数的区分
now.x=sx, now.y=sy;
now.step=0, now.tnt=0;
now.state[sx][sy]=0;
p.push(now);
while(!p.empty())
{
now=p.front();
p.pop();
if(now.step>=ans&&ans!=1e8)
continue;
for(int i=0; i<4; i++)
{
int x1=now.x+dir[i][0];
int y1=now.y+dir[i][1];
int t1=now.step;
int tnt=-1;
if(now.check(x1, y1))//检查该点合法
{
if(map[x1][y1]=='D')
{
ans=min(ans, t1+1);//记录最优解
continue;
}
else if(now.state[x1][y1]>-1||map[x1][y1]=='.')//平路
{
t1+=1;
tnt=now.tnt;
}
else if(map[x1][y1]=='X'&&now.tnt>0)//有炸弹就炸墙
{
t1+=2;
tnt=now.tnt-1;
}
else if(isdigit(map[x1][y1]))//捡炸弹
{
t1+=1;
tnt=now.tnt+(map[x1][y1]-'0');
}
if(tnt>now.state[x1][y1])//这次的炸弹比上次的多
{
node temp;
temp.x=x1, temp.y=y1, temp.step=t1;
temp.tnt=tnt;
memcpy(temp.state, now.state, sizeof(temp.state));//now.state复制到temp.state上
temp.state[x1][y1]=tnt;
p.push(temp);
}
}
}
}
return ans==1e8 ? 0:1;//如果没找到答案返回0
}
int main()
{
while(scanf("%d%d", &m, &n), n||m)
{
for(int i=0; i<n; i++)
{
scanf("%s", map[i]);
if(strchr(map[i], 'S'))
sx=i, sy=strchr(map[i], 'S')-map[i];
}
ans=1e8;
printf("%d\n", bfs() ? ans:-1);//没找到答案返回-1
}
return 0;
}