poj 3009——Curling 2.0
来源:互联网 发布:java怎么调字体大小 编辑:程序博客网 时间:2024/05/17 23:27
题意:冰壶在一个平面上面。开始冰壶静止,可以向四个方向运动。在运动过程中不能改变方向。当碰到障碍物的时候停止,障碍物消失,这个时候可以改变方向。当到达终点的时候也会停止求冰壶从起点到达终点的最短运动的次数
思路:dfs
#include<iostream>#include<cstdio>#include<cstring>using namespace std;struct pos{ int x; int y; pos(int x,int y):x(x),y(y){} pos(){}};bool operator == (const pos &a,const pos &b){ return a.x==b.x&&a.y==b.y;}const int dir[4][2]={-1,0,0,1,1,0,0,-1};int fig[25][25];int ans;pos start;pos goal;int h,w;bool judge(const pos a){ if(a.x<0||a.x>=h)return 0; if(a.y<0||a.y>=w)return 0; return 1;}void dfs(pos now,int step){ if(step>10)return ; if(now==goal) { ans=min(ans,step); return; } if(!judge(now))return ; for(int d=0;d<4;++d) { pos tmp=now; if(fig[tmp.x+dir[d][0]][tmp.y+dir[d][1]]==1)continue; while(1) { tmp.x=tmp.x+dir[d][0]; tmp.y=tmp.y+dir[d][1]; if(!judge(tmp))break; if(fig[tmp.x][tmp.y]==3) { ans=min(ans,step+1); return ; } else if(fig[tmp.x][tmp.y]==1) { tmp.x=tmp.x-dir[d][0]; tmp.y=tmp.y-dir[d][1];//这里不小心把y写成了x,找了好长时间。。。。 break; } } if(!judge(tmp))continue; bool change=0; if(fig[tmp.x+dir[d][0]][tmp.y+dir[d][1]]==1) { fig[tmp.x+dir[d][0]][tmp.y+dir[d][1]]=0; change=1; } dfs(tmp,step+1); if(change) { fig[tmp.x+dir[d][0]][tmp.y+dir[d][1]]=1; } }}int main(){// freopen("data.txt","r",stdin); while(scanf("%d%d",&w,&h)!=EOF) { if(!h&&!w)break; for(int i=0;i<h;++i) { for(int j=0;j<w;++j) { scanf("%d",&fig[i][j]); if(fig[i][j]==2){ start.x=i; start.y=j; } if(fig[i][j]==3) { goal.x=i; goal.y=j; } } }// for(int i=0;i<h;++i)// {// for(int j=0;j<w;++j)// {// cout<<fig[i][j]<<' ';// }// cout<<endl;// } ans=15; dfs(start,0); if(ans>10) { puts("-1"); } else printf("%d\n",ans); } return 0;}
0 0
- poj 3009——Curling 2.0
- BFS——Poj 3009 Curling 2.0
- Poj 3009 Curling 2.0
- POJ: 3009 Curling 2.0
- poj 3009 Curling 2.0
- poj 3009 Curling 2.0
- POJ 3009 Curling 2.0
- poj 3009 Curling 2.0
- POJ 3009 Curling 2.0
- POJ-3009-Curling 2.0
- POJ 3009 Curling 2.0
- poj 3009 Curling 2.0
- POJ-3009-Curling 2.0
- POJ 3009 Curling 2.0
- POJ 3009 Curling 2.0
- POJ 3009 Curling 2.0
- poj 3009 Curling 2.0
- poj 3009 Curling 2.0
- 查询课程编号以'c05'开头,被3名及以上学生选修且期末成绩的平均分高于75分的课程号、选修人数和期末成绩平均分,并按平均分降序排序
- C#中的DataSet、string、DataTable 、对象转换成Json
- 解决Android的ListView控件滚动时背景变黑
- cisco交换机配置命令
- nginx关闭日志
- poj 3009——Curling 2.0
- C++11 auto
- LKT4101初探
- UIBezierPath
- Epoll归纳总结
- 2012年Oracle技术嘉年华后写的“作业”
- JVM类加载机制
- 多进程文件锁
- iptables详解