【POJ3009】Curling 2.0(深搜)
来源:互联网 发布:万德数据 编辑:程序博客网 时间:2024/06/02 03:12
题目大意:
题目解释完毕(~( TロT)σ)
题解:
深搜各个方向,细节见代码。
代码:
#include<cstdio>#include<cstring>#define MAXN 23#define inside(x,y) (x>0&&y>0&&x<=n&&y<=m)#define success(x,y) (x==gx&&y==gy)const int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};int n,m,sx,sy,gx,gy,ans;int map[MAXN][MAXN];void dfs(int x,int y,int step){ if(step>=10||step>=ans) return; int tx,ty; for(int d=0;d<4;d++) { if(map[x+dir[d][0]][y+dir[d][1]])continue; tx=x;ty=y; while(inside(tx,ty)&&map[tx][ty]==0&&!success(tx,ty)) tx+=dir[d][0],ty+=dir[d][1]; if(!inside(tx,ty))continue; if(success(tx,ty)) { ans=step+1; continue; } if(map[tx][ty]) { map[tx][ty]=0; dfs(tx-dir[d][0],ty-dir[d][1],step+1); map[tx][ty]=1; } }}int main(){ while(1) { scanf("%d%d",&m,&n); if(n==0&&m==0)break; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { scanf("%d",&map[i][j]); if(map[i][j]==2) {sx=i;sy=j;map[i][j]=0;} if(map[i][j]==3) {gx=i;gy=j;map[i][j]=0;} } ans=0x7FFFFFFF; dfs(sx,sy,0); if(ans==0x7FFFFFFF) printf("-1\n"); else printf("%d\n",ans); } return 0;}
阅读全文
1 0
- 【POJ3009】Curling 2.0(深搜)
- POJ3009 Curling 2.0 DFS 深搜
- 深搜+回溯 poj3009 Curling 2.0
- POJ3009 Curling 2.0(DFS)
- POJ3009 Curling 2.0(DFS)
- POJ3009 Curling 2.0 (DFS)
- POJ3009 Curling 2.0 (dfs入门题)
- poj3009--Curling 2.0(搜索练习1)
- POJ3009:Curling 2.0(DFS)
- POJ3009--Curling 2.0
- poj3009 Curling 2.0
- poj3009 Curling 2.0---bfs
- poj3009 Curling 2.0
- poj3009 Curling 2.0
- POJ3009 Curling 2.0(DFS)
- POJ3009 Curling 2.0
- POJ3009---Curling 2.0
- POJ3009 Curling 2.0
- leetcode解题报告22. Generate Parentheses
- POJ 3422 Kaka's Martix Travels 费用流模板
- php 实现信息采集(网页内容抓取)程序代码
- 关于Unity在2D的UGUI和3D模型交错时渲染顺序的一点探讨
- 资源
- 【POJ3009】Curling 2.0(深搜)
- 最小生成树Prim算法理解
- JZOJ 3766. 【BJOI2014】大融合
- 当国家选择是香港时运输方式:EUB、EUB2、SHEUB 中的邮编栏置灰
- bitset位集合容器
- aizu/aoj-0118-Property Distribution
- AES加密算法(加密字符串):Rijndael
- 用到qsort的一道题(+qsort模板)
- Python 中的单例模式