HDOJ 1026 dfs路径保存
2013-12-27 15:59
239 查看
#include<cstdio> #include<cstring> #include<cmath> int g[101][101]; #define inf 0xffffff int n,m; int min; int sx[4]={0,1,0,-1}; int sy[4]={1,0,-1,0}; int que[1001][2]; int front,rear; void dfs(int x,int y,int c_step) { if(x==n&&y==m) { if(c_step<min) {min=c_step;return;} } if(abs(x-n)+abs(y-m)+c_step>n*m-1)return; for(int i=0;i<4;i++) { int xx,yy; xx=x+sx[i]; yy=y+sy[i]; if(xx<=n&&xx>=1&&yy<=m&&yy>=1&&g[xx][yy]!=-1) { int zhi=g[xx][yy]; g[xx][yy]=-1; dfs(xx,yy,c_step+zhi+1); g[xx][yy]=zhi; } } } int main() { char c; int i,j; while(scanf("%d %d",&n,&m)!=EOF) { getchar(); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { c=getchar(); if(c=='.') g[i][j]=0; else if(c=='#') g[i][j]=-1; else g[i][j]=c-'0'; } getchar(); } min=inf; int start=g[1][1]; dfs(1,1,start); printf("\n"); if(min>n*m-1) printf("God please help our poor hero.\n"); else printf("It takes %d seconds to reach the target position, let me show you the way.\n",min); printf("FINISH\n"); } return 0; }
View Code
相关文章推荐
- hdoj 1026 Ignatius and the Princess I 最小步数,并且保存路径
- poj 3984 BFS+DFS(保存路径)
- bfs--hdoj1026(打印路径)
- hdoj 1026 Ignatius and the Princess I 【bfs + 路径记录 【好题】 】
- HDOJ 1026 Ignatius and the Princess I (BFS + 优先队列 + 输出路径)
- HDU1539Shredding Company(DFS+保存最优路径)
- 哈密顿绕行世界问题 简单的dfs()注意保存输出路径
- hdoj 1026 Ignatius and the Princess I【bfs&&路径输出】
- hdu 1026 优先队列 + 路径保存
- 【二叉树】DFS计算路径和,保存路径【113. Path Sum II】【112. Path Sum】
- CF-29D - Ant on the Tree(DFS+路径保存回扫)
- HDU 1026 Ignatius and the Princess I (bfs+手写队列保存路径)
- 组合数 【DFS】(保存路径)
- CF-29D - Ant on the Tree(DFS+路径保存回扫)
- 保存一棵树根到叶子之和为给定值的所有路径的DFS解法
- HDU_1026_Ignatius and the Princess I_BFS(保存路径)
- hdoj 1026 Ignatius and the Princess I 【BFS + 优先队列 + stack路径记录】 【经典题目】
- poj 2965 dfs+保存路径
- 入门经典-习题7-1,109-uva208消防车-DFS,并查集,打印路径,利用set保存边,字典序,STL⭐⭐⭐⭐⭐复杂度:3
- [置顶] HDOJ 1026 Ignatius and the Princess I (BFS+优先队列+记录路径)