【奇偶剪枝】HDU 1010 Tempter of the Bone

来源:互联网 发布:兄贵pat it 编辑:程序博客网 时间:2024/05/01 14:13

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010

 

剪枝:

1.奇偶剪枝:abs(sx-ex)+abs(sy-ey)为起点到终点的最短步数,若最短步数与要求步数t奇偶性不同,则不可能走到

2.当前步数不能大于t

3.图中'.'的数量+2<=t则不能走到

 

 

#include<stdio.h>#include<string.h>#include<math.h>#define MAXN 10char map[MAXN][MAXN];int n,m,t,sx,sy,ex,ey,flag;int dir[4][2]={0,1,1,0,0,-1,-1,0};void dfs(int x,int y,int tt){if(tt>t||flag) return ;if(tt==t&&x==ex&&y==ey){flag=1;return ;}if((abs(x-ex)+abs(y-ey))%2!=(t-tt)%2) return ;for(int i=0;i<4;i++){int xx=x+dir[i][0],yy=y+dir[i][1];if(xx<0||xx>=n||yy<0||yy>=m||map[xx][yy]=='X') continue;map[xx][yy]='X';dfs(xx,yy,tt+1);map[xx][yy]='.';}}char s[15];int main(){while(scanf("%d%d%d",&n,&m,&t)!=EOF&&(n+m+t)){int cnt=0;for(int i=0;i<n;i++){scanf("%s",&map[i]);for(int j=0;j<m;j++)if(map[i][j]=='S')sx=i,sy=j;else if(map[i][j]=='D')ex=i,ey=j;else if(map[i][j]=='.')cnt++;}if(abs(sx-ex)+abs(sy-ey)>t||cnt+2<=t||(t-abs(sx-ex)-abs(sy-ey))%2){printf("NO\n");continue;}map[ex][ey]='.';map[sx][sy]='X';flag=0;dfs(sx,sy,0);if(flag) printf("YES\n");else printf("NO\n");}return 0;}


 

 

 

0 0
原创粉丝点击