- 如果所给的时间(步数) t 小于最短步数path,那么一定走不到。
- 若满足t>path。但是如果能在恰好 t 步的时候,走到出口处。那么(t-path)必须是二的倍数。
关于第二种方案的解释:
这种方案学名为“奇偶剪枝”。我们已知了最短的步数就是直角三角形的两条直角边,实际上的路径却不一定非要沿着这两条边走的。仔细看看只要是移动方向一直是右、下,那么走到的时候总步数也一定是path的。然而由于墙的存在或许我们不可能一直右、下的走下去。为了避开墙,我们可能会向左走,向上走等等。但为了到达目的地,你在最短步数的基础上,如果向右走了一步,那么某一时候也必须再向左走一步来弥补。所以(t-path)一定要是2的倍数。
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
Input
'X': a block of wall, which the doggie cannot enter;
'S': the start point of the doggie;
'D': the Door; or
'.': an empty block.
The input is terminated with three 0's. This test case is not to be processed.
Output
Sample Input
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
Sample Output
YES
#include<iostream>
#include<stdio.h>
using namespace std;
bool vis[][];
struct Node
{
int x,y;
int step;
} node[][];
int n,m,t;
char ma[][];
int dx[]= {,,,-};
int dy[]= {,-,,};
bool dfs(int x,int y)
{
if(ma[x][y]=='S')
{
node[x][y].step=;
}
if(ma[x][y]=='D')
{
if(node[x][y].step==t) return true;
else return false;
}
for(int i=; i<; i++)
{
int nx=dx[i]+x;
int ny=dy[i]+y;
if((!vis[nx][ny])&&(nx>=&&ny>=)&&(nx<n&&ny<m)&&(ma[nx][ny]!='X'))
{
vis[nx][ny]=true;
node[nx][ny].step=node[x][y].step+;
if(node[nx][ny].step<=t)
if(dfs(nx,ny)) return true;
vis[nx][ny]=false;
node[nx][ny].step--;
}
}
return false;
}
int main()
{ while((scanf("%d%d%d",&n,&m,&t))&&(n+m+t)!=)
{
int x,y;
int ex,ey;
for(int i=; i<; i++)
for(int j=; j<; j++)
{
vis[i][j]=false;
node[i][j].step=;
}
for(int i=; i<n; i++)
{
scanf("%s",ma[i]);
}
for(int i=; i<n; i++)
for(int j=; j<m; j++)
if(ma[i][j]=='S')
{
x=i;
y=j;
}
else if(ma[i][j]=='D')
{
ex=i;
ey=j;
}
if(abs(ex-x)+abs(ey-y)>t||(t-abs(ex-x)-abs(ey-y))%==)
{
printf("NO\n");
continue;
}
if(dfs(x,y))
printf("YES\n");
else printf("NO\n");
}
return ;
}