原题链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1010
题目大意:
略
思路:
时间刚好的时候才可以逃离
如果当前位置和终点的位置的最短距离大于剩余时间也不可以
如果当前位置和终点的位置的最短距离和剩余时间的奇偶性不同也不可以(一块地砖只能走一次)
代码如下:
#include <iostream>
#include <cstdio>
char maze[10][10]; //存放迷宫信息
int check[10][10]; //标记数组。用于检测这个点是否走过
int Maze_Long,Maze_Wide;
int S_x,S_y;
int D_x,D_y;
bool Can;
int Time;
using namespace std;
int fabs( int p )
{
return p > 0? p : -p;
}
void Go( int start_x, int start_y ,int time )
{
if( Can )
return;
//到达判定
if( start_x == D_x && start_y == D_y && time == Time )
{
Can = true;
return;
}
//最短距离
int Short = fabs( start_x - D_x ) + fabs( start_y - D_y );
//剩余的最短距离和剩余时间的关系。
if( Short > Time - time || (Short + Time - time )%2 != 0 )
return;
int Xz[4]={-1,0,1,0};
int Yz[4]={0,1,0,-1};
int k,new_x,new_y;
for( k = 0; k < 4; k++ )
{
new_x = start_x + Xz[k];
new_y = start_y + Yz[k];
//越界判定
if( new_x < 0 || new_x >= Maze_Wide || new_y < 0 || new_y >= Maze_Long)
continue;
if( maze[new_x][new_y] != 'X' && !check[new_x][new_y] )
{
check[new_x][new_y] = 1;
Go( new_x,new_y,time+1 );
check[new_x][new_y] = 0;
}
}
}
int main()
{
while(1)
{
int time;
int count;
count = 0;
cin>>Maze_Long>>Maze_Wide>>Time;
if( Maze_Long == 0 && Maze_Wide == 0 && Time == 0 )
break;
int i,j;
for( i = 0; i < Maze_Wide; i++ )
for( j = 0; j < Maze_Long; j++ )
{
cin>>maze[i][j];
if( maze[i][j] == 'S') {S_x = i; S_y = j;}
if( maze[i][j] == 'D') {D_x = i; D_y = j;}
if( maze[i][j] == 'X') {count++;}
}
//如果可以走的砖块比时间还少。肯定不可以
if( Maze_Long * Maze_Wide - count -1 < Time )
{
cout<<"NO"<<endl;
continue;
}
for( i = 0; i < Maze_Wide; i++ )
for( j = 0; j < Maze_Long; j++ )
check[i][j] = 0;
Can = false;
time = 0;
check[S_x][S_y] = 1;
Go( S_x,S_y,time );
if( Can )
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}