zoj2110 Tempter of the Bone 搜索dfs
2014-07-25 20:33
288 查看
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1110
题目大意:翻译看的《图论算法理论,实现及应用》王桂平著,例2.1
思路:简单dfs,写好后直接TLE,看了好久还是被大腿发现找到路径后忘了写 if( findit ) return ; 简直智商着急。。。
时隔将近一年,又用非递归的方式重写了一遍,居然用了680MS。。。。
题目大意:翻译看的《图论算法理论,实现及应用》王桂平著,例2.1
思路:简单dfs,写好后直接TLE,看了好久还是被大腿发现找到路径后忘了写 if( findit ) return ; 简直智商着急。。。
///2014.7.25 ///zoj2110 //340MS #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; int n,m,t; char maze[10][10]; bool visited[10][10]; int sx,sy,ex,ey; bool findit; const int addx[4] = {0,1,0,-1}; const int addy[4] = {1,0,-1,0}; void init(){ for(int i=0 ; i<n ; i++) scanf("%s",maze[i]); memset(visited,false,sizeof(visited)); for(int i=0 ; i<n ; i++){ for(int j=0 ; j<m ; j++){ if( maze[i][j] == 'S' ) sx = i, sy = j; if( maze[i][j] == 'D' ) ex = i, ey = j; } } findit = false; } bool hasNext(int x,int y){ if( x<0 || x>=n || y<0 || y>=m ) return false; if( visited[x][y] ) return false; if( maze[x][y]=='X' ) return false; return true; } void dfs(int x,int y,int step){ if( findit ) return; if( x==ex && y==ey && step==t ){ findit = true; return ; } if( step==t ) return ; int temp = t-step-fabs(x-ex)-fabs(y-ey); if( temp<0 || temp%2 ) return ; for(int i=0 ; i<4 ; i++){ if( hasNext(x+addx[i],y+addy[i]) ){ visited[x+addx[i]][y+addy[i]] = true; dfs(x+addx[i],y+addy[i],step+1); visited[x+addx[i]][y+addy[i]] = false; } } } int main(){ while( ~scanf("%d %d %d",&n,&m,&t) && ( n+m+t ) ){ init(); visited[sx][sy] = true; dfs(sx,sy,0); if( findit ) printf("YES\n"); else printf("NO\n"); } return 0; }
时隔将近一年,又用非递归的方式重写了一遍,居然用了680MS。。。。
///2015.5.6 ///zoj2110 ///680MS #include <iostream> #include <cstdio> #include <stack> #include <cstring> #include <cmath> using namespace std; int n,m,t; int sx,sy,dx,dy; char maze[10][10]; bool visited[10][10]; int addx[5] = {-1,0,1,0,0}; int addy[5] = {0,1,0,-1,0}; bool findit; struct pathNode{ int x,y; int d; int i; }; void init(){ memset(visited,false,sizeof(visited)); for(int i=0 ; i<n ; i++) scanf("%s",maze[i]); for(int i=0 ; i<n ; i++){ for(int j=0 ; j<m ; j++){ if( maze[i][j]=='S' ) sx=i,sy=j; if( maze[i][j]=='D' ) dx=i,dy=j; } } findit = false; } bool hasNext(int x,int y){ if( x<0 || x>=n || y<0 || y>=m ) return false; if( visited[x][y] ) return false; if( maze[x][y]=='X' ) return false; return true; } bool dfs(int t){ stack<pathNode> path; pathNode temp; temp.x=sx,temp.y=sy,temp.d=0,temp.i=0; path.push(temp); visited[sx][sy] = true; while( !path.empty() ){ int x = path.top().x; int y = path.top().y; int d = path.top().d; int i = path.top().i; if( x==dx && y==dy && d==t ){ findit = true; break; } int shengyu = t-d-fabs(x-dx)-fabs(y-dy); if( shengyu<0 || shengyu%2 || d==t || i==4 ){ path.pop(); visited[x][y] = false; continue; } int nextX = x + addx[i]; int nextY = y + addy[i]; if( hasNext(nextX,nextY) ){ visited[nextX][nextY] = true; path.top().i ++; pathNode temp; temp.x=nextX,temp.y=nextY,temp.d=d+1,temp.i=0; path.push(temp); } else { path.top().i ++; } } if( findit ){ return true; } else { return false; } } int main(){ while( scanf("%d %d %d",&n,&m,&t) && (n+m+t) ){ init(); int shengyu = t-fabs(sx-dx)-fabs(sy-dy); if( shengyu<0 || shengyu%2 ){ printf("NO\n"); } else { if( dfs(t) ){ printf("YES\n"); } else { printf("NO\n"); } } } return 0; }
相关文章推荐
- hdu 1010 Tempter of the Bone dfs搜索 奇偶剪枝 解题报告
- 【搜索-DFS】hdu1010 Tempter of the Bone
- HDU1010 Tempter of the Bone(深度优先搜索DFS+奇偶性剪枝)
- HDU1010 Tempter of the Bone(搜索,dfs)
- hdu1010 Tempter of the Bone dfs搜索 剪枝
- hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)
- HDOJ 1010 Tempter of the Bone--DFS+奇偶剪枝(转)
- Tempter of the Bone(DFS+奇偶剪枝)
- HDU 1010 Tempter of the Bone - (DFS) 奇偶剪枝
- ZOJ 2110 Tempter of the Bone (DFS+减枝)
- zoj 2110 Tempter of the Bone【邻接阵,dfs】
- hdu 1010 Tempter of the Bone DFS+奇偶性剪枝
- DFS 专题 Tempter of the Bone
- FOJ--1046--Tempter of the Bone--解题报告(典型的迷宫搜索)
- 1010 Tempter of the Bone 深度搜索 奇偶剪枝
- HDU 1010 Tempter of the Bone(DFS)
- ZOJ-2110 Tempter of the Bone( 搜索,奇偶性剪枝)
- hdu 1010 Tempter of the Bone(DFS+剪枝优化)
- HDU 1010 Tempter of the Bone (DFS)
- HDU 1010 Tempter of the Bone &&ZOJ 2110【DFS】