您的位置:首页 > 其它

HDU 1010 Tempter of the Bone(奇偶剪枝)

2012-08-16 22:25 309 查看
/*
题意:从S到D,能否在T时刻到达

主要使用奇偶剪枝

主要收获:
①奇偶剪枝
②尽量减少scanf("%c", &c);的使用
*/

#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
const int nMax = 8;
char map[nMax][nMax];
int visit[nMax][nMax];
int n, m, t;
int sx, sy, dx, dy;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int ans;

void dfs(int x, int y, int step)
{
if(ans) return;
if(x == dx && y == dy)
{
if(step == t)
ans = 1;
return;
}
if(step >= t)//④这个剪枝效率太低,可以忽略,因为已经走了t步才进行判断
return;
int d = t - step - abs(x - dx) - abs(y - dy);//①奇偶剪枝,abs(x - dx) + abs(y - dy) 与 t - step奇偶性相同。这里剪枝效果最明显
if(d < 0 || d % 2 != 0) return;//②d < 0 表示,走理想中的最短路径也无法到达
int i;
for(i = 0; i < 4; ++ i)
{
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if(xx >= 0 && xx < n && yy >= 0 && yy < m && map[xx][yy] != 'X' && !visit[xx][yy])
{
visit[xx][yy] = 1;
dfs(xx, yy, step + 1);
visit[xx][yy] = 0;
}
}
}

int main()
{
while(scanf("%d%d%d", &n, &m, &t) != EOF)
{
if(n == 0) break;
int i, j;
int sumWall = 0;
for(i = 0; i < n; ++ i)
{
scanf("%s", map[i]);//这里曾经WA了N多次,尽量减少使用scanf("%c", &c);
for(j = 0; j < m; ++ j)
{
if(map[i][j] == 'S')
sx = i, sy = j;
else if(map[i][j] == 'D')
dx = i, dy = j;
else if(map[i][j] == 'X')
sumWall++;
}
}
if(t >= n * m - sumWall)//③,这里的剪枝会优化5、6倍左右
{
printf("NO\n");
continue;
}
ans = 0;
memset(visit, 0, sizeof(visit));
visit[sx][sy] = 1;
dfs(sx, sy, 0);
printf("%s\n", ans ? "YES" : "NO");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  优化 c