Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.
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.
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
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. 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, 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.
'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
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
Sample Input
4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
Sample Output
NO YES
分析:这个题目的意思是给定你起点S,和终点D,问你是否能在 T 时刻恰好到达终点D。
如果是T时刻内到达则用BFS求最短路,代码如下:
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
struct Node {
int x, y;
Node(int x=0, int y=0):x(x),y(y) {}
};
const int maxn = 50;
const int dr[] = { -1, 0, 1, 0 };
const int dc[] = { 0, 1, 0, -1 };
char maze[maxn][maxn];
int visit[maxn][maxn];
int n, m, t;
int sn, sm;
int dn, dm;
bool inside(const Node& v)
{
return v.x>=0&&v.x<n && v.y>=0&&v.y<m;
}
int solve()
{
memset(visit, -1, sizeof(visit));
visit[sn][sm] = 0;
queue<Node> q;
q.push(Node(sn,sm));
while(!q.empty()) {
Node u = q.front(); q.pop();
if(u.x==dn && u.y==dm) return visit[dn][dm];
for(int i=0; i<4; i++) {
Node v(u.x+dr[i], u.y+dc[i]);
if((maze[v.x][v.y]=='.'||maze[v.x][v.y]=='D') && visit[v.x][v.y]<0 && inside(v)) {
visit[v.x][v.y] = visit[u.x][u.y] + 1;
q.push(v);
}
}
}
return visit[dn][dm];
}
int main()
{
while(~scanf("%d%d%d", &n, &m, &t)&&n) {
for(int i=0; i<n; i++) {
getchar();
for(int j=0; j<m; j++) {
maze[i][j] = getchar();
if(maze[i][j]=='S') { sn=i; sm=j; }
if(maze[i][j]=='D') { dn=i; dm=j; }
}
}
int ans = solve();
if(ans>=0 && ans<=t) printf("YES\n");
else printf("NO\n");
}
return 0;
}
但是求T时刻恰好到达,显然是DFS,这里涉及奇偶剪支问题:
现假设起点为(sx,sy),终点为(ex,ey),给定t步恰好走到终点,
s
| ||||
|
| ||||
|
| ||||
|
| ||||
+
|
—
|
—
|
—
|
e
|
如图所示(“|”竖走,“—”横走,“+”转弯),易证abs(ex-sx)+abs(ey-sy)为此问题类中任意情况下,起点到终点的最短步数,记做step,此处step1=8;
s
|
—
|
—
|
—
| |
—
|
—
|
+
| ||
|
|
+
| |||
|
| ||||
+
|
—
|
—
|
—
|
e
|
如图,为一般情况下非
最短路径的任意走法举例,step2=14;
step2-step1=6,偏移路径为6,偶数(易证);
推广之,若 t-[abs(ex-sx)+abs(ey-sy)] 结果为非偶数(奇数),则无法在t步恰好到达;
返回,false;
反之亦反。
更多关于奇偶剪支问题点此查看
接下来我们来第二个剪枝:
我们把map的奇偶性以01编号:
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
我们发现从0走一步一定走到1,从1走一步一定走到0。
也就是说,如果当前的狗所在的坐标与D的坐标奇偶性不一样,那么狗需要走奇数步。
同理,如果狗所在坐标与D的坐标奇偶性一样,那么狗需要走偶数步数。
也就是说,狗的坐标x、y和对2取余是它的奇偶性,Dxy和对2取余是D的奇偶性。
两个奇偶性一加再对2取余,拿这个余数去与剩下时间对2取余的余数作比较即可。
我也是日乐购,最后一个换行符没吃!WA了两页!
#include <cstdio>
#include <cmath>
using namespace std;
char maze[10][10];
int n, m, t, ans, di, dj;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
bool inside(int i, int j) { return i>=0&&i<n && j>=0&&j<m; }
void dfs(int i, int j, int c)
{
if(i==di && j==dj) { if(c==t) ans = 1; return; }
if(c>=t) return;
for(int dir=0; dir<4; dir++) {
int x = i+dr[dir], y = j+dc[dir];
if(inside(x, y) && maze[x][y]!='X') {
maze[x][y] = 'X';
dfs(x, y, c+1);
maze[x][y] = '.';
if(ans) return;
}
}
}
int main()
{
while(~scanf("%d%d%d", &n, &m, &t)) {
if(!n && !m && !t) break;
int si, sj;
for(int i=0; i<n; i++) {
getchar();
for(int j=0; j<m; j++) {
maze[i][j] = getchar();
if(maze[i][j]=='S') { si=i; sj=j; }
if(maze[i][j]=='D') { di=i; dj=j; }
}
}
getchar();
ans = 0;
maze[si][sj] = 'X';
if(abs(si-di)+abs(sj-dj)<=t && (si+sj+di+dj+t)%2==0) dfs(si, sj, 0);
printf("%s\n", ans?"YES":"NO");
}
return 0;
}