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.
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.
'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
看别人题解的,,,哎
/*
** 时间:2014-10-1
** 知识点:
** 题意:
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
const int MAX = 1000000;
int n,m,sx,sy;
int fx[]={1,0,-1,0,1};
char mp[10][10];
struct node{
int x,y,vs,setp;
int vis[8][8];
};
int bfs()
{
int res=MAX;
node a;
a.x=sx;
a.y=sy;
a.vs=a.setp=0;
memset(a.vis,-1,sizeof (a.vis));
a.vis[sx][sy]=0;
queue<node>Q;
Q.push(a);
while(!Q.empty())
{
//cout<<1<<endl;
node q=Q.front();
Q.pop();
if(res!=MAX&&q.setp>=res)
continue;
for(int i=0; i<4; i++)
{
int bx=q.x+fx[i];
int by=q.y+fx[i+1];
int bvs=-1;
int bsetp=q.setp;
if(bx<0||bx>=n||by<0||by>=m)continue;
if(mp[bx][by]=='D')
{
res=res<bsetp+1?res:bsetp+1;continue;
}else if(q.vis[bx][by]>-1||mp[bx][by]=='.')//巧妙啊,怎么想不到呢,可以避免重复选择炸弹
{
bsetp+=1;
bvs=q.vs;
}else if(mp[bx][by]=='X'&&q.vs>0)
{
bsetp+=2;
bvs=q.vs-1;
}else if(isdigit(mp[bx][by])){
bsetp+=1;
bvs=q.vs+mp[bx][by]-'0';
}
if(bvs>q.vis[bx][by]){
a.x=bx;a.y=by;a.setp=bsetp;
a.vs=bvs;
memcpy(a.vis,q.vis,sizeof(a.vis));
a.vis[a.x][a.y]=bvs;
Q.push(a);
}
}
}
if(res==MAX)res=-1;
return res;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF){
if(n==0&&m==0)break;
for(int i=0; i < n; i++){
scanf("%s",mp[i]);
for(int j=0; j < m; j++)
{
if(mp[i][j]=='S')
{
sx=i;sy=j;
}
}
}
printf("%d\n",bfs());
}
return 0;
}