题目大意:要你求一个迷宫中,起点s经过给定的步数是否能到终点t,迷宫中有挡板,挡板是不能走的格子
解题思路:很明显的搜索题,用深度优先或宽度优先遍历搜索是否满足,给定步数,也就是求起点根结点到终点最终解的那个搜索树经过的边树
深度优先遍历,要用到奇偶性剪枝,开始不知道啥叫。。。。奇偶性剪枝,
看了这哥们的博客才明白:
详情见:http://www.cppblog.com/Geek/archive/2010/04/26/113615.html
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
struct Point
{
int x, y;
};
const int maxn = 8;
char maze[maxn][maxn];
bool map[maxn][maxn];
int n, m, t, step;
Point dir[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
Point s, e;
bool dfs(int x, int y);
int main()
{
while(true)
{
scanf("%d %d %d", &n, &m, &t);
if(n == 0 && m == 0 && t == 0)
break;
memset(map, 0, sizeof(map));
step = 0;
for(int i = 0; i < n; i++)
{
char *p;
scanf("%s", maze[i]);
if ((p = strchr(maze[i], 'S')) != NULL)
{
s.x = i;
s.y = p - maze[i];
}
if ((p = strchr(maze[i], 'D')) != NULL)
{
e.x = i;
e.y = p - maze[i];
}
}
if(dfs(s.x, s.y))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
bool dfs(int x, int y)
{
if(step > t)
return false;
int st = abs(x - y);
int ed = abs(e.x - e.y);
if(x >= 0 && x < n && y >= 0 && y < m && !map[x][y] && maze[x][y] != 'X' && (t - step - st - ed) % 2 == 0)
map[x][y] = true;
else
return false;
if(x == e.x && y == e.y && step == t)
return true;
for(int i = 0; i < 4; i++)
{
int dx = x + dir[i].x;
int dy = y + dir[i].y;
int tmp = step;
step++;
if(dfs(dx, dy))
return true;
step = tmp;
}
map[x][y] = false;
return false;
}