//ZOJ2110骨头的诱惑,小狗出迷宫
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
struct
{
int x;
int y;
int dir;
}stack[35];
int main()
{
int i,j,si,sj,di,dj,n,m,t;
int wall,top,cnt,dir,remain;
char enter;
char map[6][6]; //因为N和M都是小于7的,所以用6就可以了
while(1)
{
scanf("%d%d%d",&n,&m,&t);
if(n == 0 && m == 0 && t == 0)
break;
wall = 0;
scanf("%c",&enter); //接收回车字符
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='S'){ si=i; sj=j; }
else if(map[i][j]=='D'){ di=i; dj=j; }
else if(map[i][j]=='X'){ wall++; }
}
scanf("%c",&enter); //接收回车字符
}
if(n*m-wall<=t)
{
printf("NO\n");
continue;
}
for(i=0;i<35;i++) //每次必须把方向都重置为0
stack[i].dir=0;
top=0;
cnt=0;
stack[top].x=si;
stack[top].y=sj;
while(top>=0)
{
i=stack[top].x;
j=stack[top].y;
dir=stack[top].dir;
if(dir == 0)
{
if(i==di && j==dj && cnt==t) //是否是正确砖块在0就判断
{
printf("YES\n");
break;
}
remain=(t-cnt)-abs(i-di)-abs(j-dj); //是否不能到,在0就判断
if(remain<0 || remain%2)
{
if(top == 0) //这里如果是刚开始就判断不行,则直接退出
{
top--;
break;
}
else
{
top--;
cnt--;
continue;
}
}
if(i>0 && map[i-1][j]!='X')
{
map[i][j]='X'; //本块砖在0这个位置就置为X
stack[top].dir++;
top++;
stack[top].x=i-1;
stack[top].y=j;
cnt++;
}
else
{
map[i][j]='X'; //本块砖在0这个位置就置为X
stack[top].dir++;
}
}
else if(dir == 1)
{
if(j<m-1 && map[i][j+1]!='X')
{
stack[top].dir++;
top++;
stack[top].x=i;
stack[top].y=j+1;
cnt++;
}
else
stack[top].dir++;
}
else if(dir == 2)
{
if(i<n-1 && map[i+1][j]!='X')
{
stack[top].dir++;
top++;
stack[top].x=i+1;
stack[top].y=j;
cnt++;
}
else
stack[top].dir++;
}
else if(dir == 3)
{
if(j>0 && map[i][j-1]!='X')
{
stack[top].dir++;
top++;
stack[top].x=i;
stack[top].y=j-1;
cnt++;
}
else
stack[top].dir++;
}
else if(dir == 4)
{
stack[top].dir=0;
map[i][j]='.'; //当本块砖不行的时候,在退出的时候一定要置为.
top--; //因为后面的路线可能会再次到这块砖上来
cnt--;
}
}
if(top<0)
printf("NO\n");
}
return 0;
}
/*测试结果:通过了ZOJ2110检测
3 4 5
S...
.X.X
...D
YES
4 4 8
.X.X
..S.
....
DX.X
YES
4 4 5
S.X.
..X.
..XD
....
NO
0 0 0
请按任意键继续. . .
*/
ZOJ2110 Tempter of the Bone
最新推荐文章于 2018-12-31 17:21:55 发布