标签:des style blog http ar io color os sp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 75141 Accepted Submission(s): 20531
一开始没看清楚用的是BFS果断错,后来用深搜TLE,但是搞懂了奇偶剪枝。
s
|
||||
|
|
||||
|
|
||||
|
|
||||
+
|
—
|
—
|
—
|
e
|
s
|
—
|
—
|
—
|
|
—
|
—
|
+
|
||
|
|
+
|
|||
|
|
||||
+
|
—
|
—
|
—
|
e
|
推广之,若 t-[abs(ex-sx)+abs(ey-sy)] 结果为非偶数(奇数),则无法在t步恰好到达;
返回,false;
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<stdlib.h> #include<algorithm> #define LL __int64 using namespace std; const int MAXN=100; int n,m,T,vis[MAXN][MAXN]; int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; char map[MAXN][MAXN]; bool flag; struct node { int x,y; }star,en; bool judge(int x,int y) { if(x<0 || x>=n || y<0 || y>=m || vis[x][y] || map[x][y]==‘X‘) return false; return true; } void DFS(int x,int y,int cur) { if(flag) return ; if(cur==T) { if(x==en.x && y==en.y) flag=true; return ; } int dis=abs(x-en.x)+abs(y-en.y);//当前节点到终点 int res=T-dis-cur;//还剩下多少步 if(res%2==1) return ; for(int i=0;i<4;i++) { int xx=x+dir[i][0]; int yy=y+dir[i][1]; if(judge(xx,yy)) { vis[xx][yy]=1; DFS(xx,yy,cur+1); vis[xx][yy]=0; } } } int main() { //freopen("in.txt","r",stdin); while(scanf("%d %d %d",&n,&m,&T) && (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‘) { star.x=i; star.y=j; } if(map[i][j]==‘D‘) { en.x=i; en.y=j; } if(map[i][j]==‘X‘) cnt++; } } if(n*m-cnt-1<T){printf("NO\n");continue;} memset(vis,0,sizeof(vis)); flag=false; vis[star.x][star.y]=1; DFS(star.x,star.y,0); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }
HDU 1010 Tempter of the Bone (DFS 奇偶剪枝)
标签:des style blog http ar io color os sp
原文地址:http://www.cnblogs.com/clliff/p/4167867.html