[恢]hdu 1010

2011-12-26 10:54:08

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1010

题意:从S走到D是否存在一条路径(走过的方格不能再走)使得正好用T的时间走完。

mark:dfs加奇偶剪枝。

代码:

# include <stdio.h>
# include <string.h>


int sx, sy, ex, ey ;
int n, m, T;
char graph[110][110] ;
int visited[110][110] ;


int dfs (int x, int y, int t)
{
int i, xx, yy ;
int tab[4][2] = {0, 1, 0, -1, 1, 0, -1, 0} ;
if (t < 0) return 0 ;
if (t == 0 && x == ex && y == ey) return 1 ;
visited[x][y] = 1;
for (i = 0 ; i < 4 ; i++)
{
xx = x + tab[i][0] ;
yy = y + tab[i][1] ;
if (xx < 0 || xx >= n || yy < 0 || yy >= m) continue ;
if (graph[xx][yy] == 'X') continue ;
if (visited[xx][yy]) continue ;
if (dfs (xx, yy, t-1)) return 1 ;
}
visited[x][y] = 0 ;
return 0 ;
}


int main ()
{
int i, j ;
while (~scanf ("%d %d %d%*c", &n, &m, &T) &&(n||m||T))
{
for (i = 0 ; i < n ; i++)
scanf ("%s%*c", graph[i]) ;
for (i = 0 ; i < n ; i++)
for (j = 0 ; j < m ; j++)
if (graph[i][j] == 'S')
sx = i, sy = j ;
else if (graph[i][j] == 'D')
ex = i, ey = j ;
if (((sx+sy+ex+ey)&1) != (T&1))
{
puts ("NO") ;
continue ;
}
memset (visited, 0, sizeof(visited)) ;
puts (dfs(sx, sy, T) ? "YES" : "NO");
}
return 0 ;
}



原文地址:https://www.cnblogs.com/lzsz1212/p/2315362.html