【杭电】[1242]Rescue
来源:互联网 发布:淘宝网水一星家纺 编辑:程序博客网 时间:2024/05/28 03:01
虽然学长把这题取名为BFS……
不过一时脑抽没分清BFS是深搜还是广搜
所以用的DFS做的
不过对于这种水题其实都一样啦
没什么坑点
用一个二维数组保存每个点到终点的距离
然后递归搜索
遇见x就+2
遇见.就+1
#include<stdio.h>int inf=9999999;char map[220][220];int dis[220][220];int n,m;int mn,mm;int en,em;void bfs(int tn,int tm) { if(tm+1<m&&((map[tn][tm+1]=='.'&&dis[tn][tm+1]>=dis[tn][tm]+1)||(map[tn][tm+1]=='x'&&dis[tn][tm+1]>=dis[tn][tm]+2))) { dis[tn][tm+1]=dis[tn][tm]+1; if(map[tn][tm+1]=='x') dis[tn][tm+1]++; bfs(tn,tm+1); } if(tm-1>=0&&((map[tn][tm-1]=='.'&&dis[tn][tm-1]>=dis[tn][tm]+1)||(map[tn][tm-1]=='x'&&dis[tn][tm-1]>=dis[tn][tm]+2))) { dis[tn][tm-1]=dis[tn][tm]+1; if(map[tn][tm-1]=='x') dis[tn][tm-1]++; bfs(tn,tm-1); } if(tn-1>=0&&((map[tn-1][tm]=='.'&&dis[tn-1][tm]>=dis[tn][tm]+1)||(map[tn-1][tm]=='x'&&dis[tn-1][tm]>=dis[tn][tm]+2))) { dis[tn-1][tm]=dis[tn][tm]+1; if(map[tn-1][tm]=='x') dis[tn-1][tm]++; bfs(tn-1,tm); } if(tn+1<n&&((map[tn+1][tm]=='.'&&dis[tn+1][tm]>=dis[tn][tm]+1)||(map[tn+1][tm]=='x'&&dis[tn+1][tm]>=dis[tn][tm]+2))) { dis[tn+1][tm]=dis[tn][tm]+1; if(map[tn+1][tm]=='x') dis[tn+1][tm]++; bfs(tn+1,tm); }}int main() { while(scanf("%d %d",&n,&m)!=EOF) { getchar(); for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { dis[i][j]=inf; scanf("%c",&map[i][j]); if(map[i][j]=='r') { mn=i; mm=j; dis[i][j]=0; } else if(map[i][j]=='a') { en=i; em=j; map[i][j]='.'; } } getchar(); } bfs(mn,mm); if(dis[en][em]==inf) printf("Poor ANGEL has to stay in the prison all his life.\n"); else printf("%d\n",dis[en][em]); } return 0;}
题目记录:【杭电】[1242]Rescue
0 0
- 杭电1242---Rescue
- 杭电1242 rescue
- 杭电1242 Rescue
- 【杭电】[1242]Rescue
- 杭电 HDU 1242 Rescue
- hdu 杭电 1242 Rescue
- 杭电1242Rescue(bfs)
- 杭电 oj Rescue 1242
- 杭电1242-Rescue-DFS
- 杭电1242--Rescue--DFS 详解
- 杭电 1242 Rescue(广搜)
- 杭电1242Rescue(深搜)
- 【杭电oj1242】Rescue
- 【搜索之BFS + 优先队列】杭电 hdu 1242 Rescue
- 杭电1242--Rescue--BFS 广度优先算法详解
- 杭电1242Rescue题(bfs+优先队列)
- 杭电1242--Rescue(BFS+优先队列)
- Rescue HDU杭电1242【广搜+优先队列】
- 严格径向基网络RBF网络,概率神经网络PNN
- hdoj2013-递归入门
- 静态库介绍及报错
- 【视频处理】YUV格式说明
- Jquery hover方法的效果延时处理
- 【杭电】[1242]Rescue
- struts2实现Ajax实例
- Android对中文url编码
- 极光推送
- mysql 综合技术点
- android 从相机获取图片然后裁剪
- Java 实现工厂设计模式的三种方式
- 安装Nginx+Lua开发环境
- 自动交互式脚本--expect简介