POJ 3009 Curling 2.0
2013-08-23 16:05
323 查看
简单的DFS。一开始把DFS里面的一个变量弄成 全局变量了,调试了半个小时才发现......
#include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; int MinStep; int map[25][25]; int h,w; struct Point { int h,w; }sp,ep,np; int jh[] = { 0,-1, 1, 0}; int jw[] = {-1, 0, 0, 1}; bool check(Point tp) { if(tp.h >= 1 && tp.h <= h && tp.w >= 1 && tp.w <= w && map[tp.h][tp.w] != 1 && map[tp.h][tp.w] != 3) return true; return false; } void dfs(int nh,int nw,int ans) { if(ans > 10) { return ; } if(nh == ep.h && nw == ep.w) { if(ans < MinStep) MinStep = ans; return ; } int i; Point tp; for(i = 0;i < 4; i++) { tp.h = nh; tp.w = nw; while(check(tp)) { tp.h += jh[i]; tp.w += jw[i]; } if(tp.h >= 1 && tp.h <= h && tp.w >= 1 && tp.w <= w && map[tp.h][tp.w] == 3) { // printf("nh = %d nw = %d tp.h = %d tp.w = %d ans = %d###\n",nh,nw,tp.h,tp.w,ans); dfs(tp.h,tp.w,ans+1); } else if(tp.h >= 1 && tp.h <= h && tp.w >= 1 && tp.w <= w && map[tp.h][tp.w] == 1) { tp.h -= jh[i]; tp.w -= jw[i]; if(tp.h != nh || tp.w != nw) { // printf("nh = %d nw = %d tp.h = %d tp.w = %d ans = %d***\n",nh,nw,tp.h,tp.w,ans); map[tp.h + jh[i]][tp.w + jw[i]] = 0; dfs(tp.h,tp.w,ans+1); map[tp.h + jh[i]][tp.w + jw[i]] = 1; } } } } int main() { int i,j; while(scanf("%d %d",&w,&h) && (w || h)) { MinStep = 1000; for(i = 1;i <= h; ++i) for(j = 1;j <= w; ++j) { scanf("%d",&map[i][j]); if(map[i][j] == 2) { sp.h = i; sp.w = j; } else if(map[i][j] == 3) { ep.h = i; ep.w = j; } } dfs(sp.h,sp.w,0); if(MinStep == 1000) printf("-1\n"); else printf("%d\n",MinStep); } return 0; }
相关文章推荐
- POJ3009——Curling 2.0(DFS)
- poj3009 Curling 2.0
- poj 3009 Curling 2.0
- POJ 3009 *** Curling 2.0
- POJ 3009 Curling 2.0 DFS
- poj 3009 Curling 2.0
- poj 3009 Curling 2.0
- poj 3009 Curling 2.0(dfs)
- POJ 3009 Curling 2.0 (dfs)
- POJ 3009 Curling 2.0【带回溯DFS】
- POJ 3009 Curling 2.0
- POJ 3009 Curling 2.0
- POJ 3009-Curling 2.0(DFS)
- POJ 3009 Curling 2.0 回溯,dfs 难度:0
- Curling 2.0 (POJ 3009, 深度优先搜索)
- Poj 3009 Curling 2.0
- POJ 3009 Curling 2.0 【dfs 而不是 bfs】
- POJ-3009 Curling 2.0 (深搜)
- poj 3009 Curling 2.0
- poj 3009 Curling 2.0(dfs)