poj3009(dfs搜索)
来源:互联网 发布:暴力美学电影 知乎 编辑:程序博客网 时间:2024/05/19 02:02
这题坑得我快哭了 吃了8个WA 怎样也找不出错误 都来发现早就改对了 freopen就是忘记删掉了思路很简单 具体看注释#include <iostream>#include "stdio.h"#include "stdlib.h"#include "string.h"#include "algorithm"using namespace std;int ss[21][21],dir[4][2]={1,0,0,1,-1,0,0,-1};int n,m,ans;int isin(int x,int y) //判断是否在地图内{ return x>=1&&x<=m&&y>=1&&y<=n;}void dfs(int x,int y,int choice,int cnt) //choice为当前行动的方向{ int tempx=x+dir[choice][0]; int tempy=y+dir[choice][1];//沿着行动方向对下一个位置进行判断 if(cnt>10||cnt>ans) return ; //剪枝 if(isin(tempx,tempy)){ if(ss[tempx][tempy]==3) ans=min(ans,cnt); else if(ss[tempx][tempy]==0) dfs(tempx,tempy,choice,cnt); else if(ss[tempx][tempy]==1){ //如果下一个位置是冰块 ss[tempx][tempy]=0; //将冰块置为空地 for(int i=0;i<4;i++) { //重新寻找方向 if(isin(x+dir[i][0],y+dir[i][1])){ if(ss[x+dir[i][0]][y+dir[i][1]]==1) continue; //如果当前方向是冰块就选择下一个方向,只有运动的时候才能撞冰块 else if(ss[x+dir[i][0]][y+dir[i][1]]==3) //如果当前方向是终点,那么更新ans的值 ans=min(ans,cnt+1); else dfs(x+dir[i][0],y+dir[i][1],i,cnt+1);//如果是空地,就继续前进 } } ss[tempx][tempy]=1;//回溯将撞裂的冰块重新变为原状,寻找其他答案 } } else return;}int main(){ int sx,sy; // freopen("t","r",stdin); while(scanf("%d%d",&n,&m)&&(n&&m)) { memset(ss,0,sizeof ss); for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) { scanf("%d",&ss[i][j]); if(ss[i][j]==2) { sx=i;sy=j; } } ans=100000; ss[sx][sy]=0;//将起点置为0方便之后可以继续走 for(int i=0;i<4;i++) { if(isin(sx+dir[i][0],sy+dir[i][1])&&ss[sx+dir[i][0]][sy+dir[i][1]]!=1) //为起点寻找一个方向,越界或者某个方向相邻的是石头的舍去 dfs(sx,sy,i,1); } if(ans>10) printf("-1\n"); else printf("%d\n",ans); } return 0;}
0 0
- poj3009(dfs搜索)
- poj3009 Curling 2.0 DFS搜索
- poj3009之深度优先搜索 dfs解法
- poj3009(dfs)
- DFS poj3009
- POJ3009-DFS
- POJ3009 dfs
- 搜索-POJ3009
- poj3009 dfs + 剪枝
- POJ3009:Curling 2.0(DFS)
- another DFS problem POJ3009
- POJ3009 Curling 2.0(DFS)
- poj3009 Curling2.0 (dfs)
- POJ3009 Curling 2.0 DFS
- POJ3009 Curling 2.0【DFS】
- poj3009-dfs深搜
- POJ3009 Curling 2.0(DFS)
- poj3009(dfs/bfs)
- Mahout 对推荐数据的抽象表示(上部分)
- 冒泡排序的优化
- 软件开发(1):序言
- ICA(Independent Component Analysis)算法入门
- 安卓中<TextView/>的作用
- poj3009(dfs搜索)
- 软考-信息安全
- 白盒测试和黑盒测试
- spring FactoryBean
- redis 主从切换
- django用apache+mod_wsgi部署后中文乱码的解决方法
- 图像质量评价:SSIM&PSNR
- superoj738 诸葛亮
- linux-inode(yfruan)