poj 3009 Curling 2.0 DFS
来源:互联网 发布:手机性能检测软件 编辑:程序博客网 时间:2024/06/05 01:17
http://poj.org/problem?id=3009
思路: 就是在搜的过程中维护pos[][] 的变化;
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<set>#include<queue>#include<map>#include<vector>using namespace std;bool pos[30][30];int n,m;struct node{ int x,y; node(int x=0,int y=0):x(x),y(y){}};node st,ed;int dir[][2]={0,1,1,0,0,-1,-1,0};int ans;bool ck1(int x){ if(x>n||x<0) return 1; else return 0;}bool ck2(int y){ if(y>m||y<0) return 1; else return 0;}void dfs(int curx,int cury,int num){// printf("cx=%d cy=%d,num=%d\n",curx,cury,num); if(curx==ed.x&&cury==ed.y){ ans=min(ans,num); return ; } if(ck1(curx)||ck2(cury)) return ; if(num>ans) return ; for(int i=0;i<4;i++){ int x=curx+dir[i][0]; int y=cury+dir[i][1]; if(ck1(x)||ck2(y)||pos[x][y]) continue; while(1){ if(ck1(x)||ck2(y))break; if(x==ed.x&&y==ed.y) {dfs(x,y,num+1);break;} else if(pos[x][y]==1) { pos[x][y]=0; x-=dir[i][0];y-=dir[i][1]; dfs(x,y,num+1); pos[x+dir[i][0]][y+dir[i][1]]=1; break; } else{ x+=dir[i][0]; y+=dir[i][1]; } } }}int main(){// freopen("in.in","r",stdin); while(~scanf("%d%d",&m,&n)&&(n+m)){ int t; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){ scanf("%d",&t); if(t==1) pos[i][j]=1; else if(t==2) st=node(i,j),pos[i][j]=0; else if(t==3) ed=node(i,j),pos[i][j]=0; else if(t==0) pos[i][j]=0; } ans=11; dfs(st.x,st.y,0); if(ans==11) ans=-1; printf("%d\n",ans); } 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)
- kinect for Windows SDK——NuiSensorChooser
- 【ps】教你用ps制作木质字母
- Android 之Matrix用法
- 数据库备份、恢复之mysqldump
- sql翻页
- poj 3009 Curling 2.0 DFS
- bootstrap按钮和图像
- 广东间谍案嫌犯现身受访 谈被策反泄密详情
- Android中解析XML
- mySQl数据库中不能插入中文的处理办法
- 哈希算法
- seo优化技巧之让网站出现在移动搜索引擎首页。
- ACE在windows平台下的编译安装
- Android学习笔记进阶19之给图片加边框