poj 3009 DFS
来源:互联网 发布:mac 扫描局域网ip 编辑:程序博客网 时间:2024/06/05 11:56
题目链接点击打开链接
题意:一个球只能走直线(x , y 方向) , 碰到石头的话,球会停下, 而且石头会消失。停一次算走一步,求到终点的最小步数(超过10认为走不到终点)
思路:最多不会超过10步 , 最多走4^10次,可暴力
#include <stdio.h>#include <algorithm>#include <string>#include <string.h>#include <math.h>#include <iostream>using namespace std ;const int maxn = 25 , INF = 1e8 ;int Map[maxn][maxn] ;int sx , sy , ex, ey , w ,h , Min = INF;int d[4][2] = {0 , -1 ,-1 , 0 ,0 , 1 ,1 , 0} ;void Init(){ for(int i = 0 ; i < h ; i ++) { for(int j = 0 ; j < w ; j ++) { scanf("%d" , &Map[i][j]) ; if(Map[i][j] == 2) { sx = i , sy = j ; Map[i][j] = 0 ; // attention , 起点可多次访问,直接处理为0比较好 } if(Map[i][j] == 3) { ex = i , ey = j ; } } } Min = INF ;}bool ok(int x , int y){ if( x >= 0 && x < h && y >= 0 && y < w) return 1 ; return 0 ;}void dfs(int x , int y , int step){ if(step >= 10) return ; for(int i = 0 ; i < 4 ; i ++) { int k = x + d[i][0] , v = y + d[i][1] ; if(Map[k][v] == 1) continue ; //前面石头的话,换方向 while(!Map[k][v] ) { k += d[i][0] ; v += d[i][1] ; } if(ok(k , v )) { if(Map[k][v] == 1) { Map[k][v] = 0 ; //石头消失 dfs(k - d[i][0] , v - d[i][1] , step + 1) ; //后退一步继续选方向 Map[k][v] = 1 ; //attention reset to 1 , 其他扫描恢复原有状态 } if(Map[k][v] == 3 ) Min = min(Min , step + 1) ; // 如果到达终点的话,选出较小的步骤 } } return ;}int main(){ //freopen("a.txt" , "r" , stdin) ; while(scanf("%d%d" , &w , &h) != EOF && w && h) { Init(); dfs(sx , sy , 0) ; if(Min <= 10 ) printf("%d\n" , Min) ; else printf("-1\n") ; } return 0 ;}
0 0
- POJ 3009 dfs
- POJ 3009 DFS +剪枝
- DFS poj 3009
- poj 3009 dfs
- poj 3009 (DFS)
- poj 3009 dfs + 恢复
- poj 3009 dfs+剪枝
- poj 3009 dfs
- poj 3009 (dfs)
- poj 3009 dfs
- poj 3009 DFS +回溯
- poj 3009 DFS
- poj 3009(dfs)
- POJ 3009 dfs暴搜
- POJ 3009(Dfs)
- POJ 3009 DFS+剪枝
- poj 3009 dfs
- poj 3009 DFS
- fun_template
- 洛谷 P1066 [NOIP2006 T4] 2^k进制数
- Js组件的一些写法
- 单片机pcb板的绘制
- C++中的cout.setf()跟cout.precision()的作用
- poj 3009 DFS
- 面向对象_链式编程
- user story的重要性
- 平衡树插入调整的记忆策略
- iOS基础:事件传递
- 【openjudge 计算概论(A)】[基础编程练习(数据成分)]
- syncsort command on unix
- uva 116 单向TSP
- 使用url读取csdn的博客访问量并将记录保存到本地