POJ 3009 Curling 2.0 (dfs)
来源:互联网 发布:数据分析师证书难不难 编辑:程序博客网 时间:2024/05/22 12:31
click me to submit
IN this question , their is a little bit change.first we need to find a far enough location ,than we should go one step further to change the unreachable block reachable , and than take step back to continue.
#include <iostream>#include <cstring>#include <cstdio>#include <vector>#include <algorithm>#include <set>#include <map>#define rep(i, n) for(i;i<n;i++)#define inf 0x7f7f7f7f7fusing namespace std;int b[101][101];int vis[25][25]; //indicate ith number is markedint n,m;int ans =11;int dx[4] = {0,1,0,-1};int dy[4] = {1,0,-1,0};bool check(int x,int y){ //cout<<"checking:"<<x<<", "<<y<<endl; if(x<0||y<0||x>=n||y>=m) return false; else return true;}void dfs(int x,int y,int step){ //cout<<x<<" "<<y<<" "<<step<<endl; if(step>10) return ; int leap = 0; int xx,yy; for(int i=0;i<4;i++) { leap = 0; if(b[x+dx[i]][y+dy[i]] == 1) continue; xx = x; yy = y; while(1) { xx += dx[i]; yy += dy[i]; if(!check(xx,yy)) { leap = 1; break; } if(b[xx][yy] == 1) { break; } if(b[xx][yy] == 0) { continue; } if(b[xx][yy] == 3) { ans = min(ans,step); return ; } } if(leap) continue; b[xx][yy] = 0; dfs(xx-dx[i],yy-dy[i],step+1); b[xx][yy] = 1; }}int main(){ while(~scanf("%d%d",&m,&n)) { if(!n&&!m) break; int sx,sy; ans = 11; memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { scanf("%d",&b[i][j]); if(b[i][j] == 2) { sx = i; sy = j; b[i][j] = 0; } } } dfs(sx,sy,0); if(ans+1<11) printf("%d\n",ans+1); else printf("-1\n"); } return 0;}
阅读全文
0 0
- POJ 3009 Curling 2.0 DFS
- poj-3009-Curling 2.0-dfs
- poj 3009 Curling 2.0 DFS
- poj 3009 Curling 2.0 DFS
- POJ 3009Curling 2.0(DFS)
- POJ 3009 Curling 2.0 (DFS)
- poj-3009-Curling 2.0-dfs
- poj 3009 Curling 2.0 DFS
- POJ 3009 Curling 2.0 (DFS)
- poj 3009 Curling 2.0 【DFS】
- POJ 3009 Curling 2.0 DFS
- poj 3009 Curling 2.0 (dfs)
- Curling 2.0 (poj 3009 dfs)
- POJ 3009--Curling 2.0【DFS】
- POJ 3009 Curling 2.0(DFS)
- poj-3009 Curling 2.0-DFS
- POJ-3009-Curling 2.0- DFS
- POJ 3009 Curling 2.0 (DFS)
- 9.2 随机变量
- 新零售将更加关注以人为本,以用户为核心
- Python 基于语法提取音节个数的方法(可能不完美,欢迎指教)
- matlab快速入门
- 并行框架forkjion
- POJ 3009 Curling 2.0 (dfs)
- 树状数组--一维模板
- UVALive4671(FFT/NTT+后缀自动机/哈C)
- JVM——虚拟机字节码执行引擎(运行时栈帧结构、方法调用(解析,动静态分配)、字节码解释引擎)
- C# 和 Java的区别
- Informatica Schedule调用还是shell调用
- Python处理逆波兰表达式
- zookeeper服务器初始化的过程
- spring解决延迟加载问题--no session