题目大意:friends用最短的时间去救angel '.'表示通道 '#'表示墙壁 'x'表示guard.走一格要一单位时间,杀死一个guard要一个单位时间.

如果可以救求最短时间,否则按要求输出

解题思路:BFS

1)其实这一题主要是对BFS种的标记数组visited[][]。如果路上只有两种情况如:路、墙壁。那么用这种数组就足够了(它用来标记访问过还是没有访问过。其实,我们可以这样理解,当我们访问过以后,我们就把访问过的点理解墙壁——不可访问。。)。但是如果对于那种路上存在多种情形的情况,我们在使用visited[][]来标记的话,那么,可能就不那么方便了(因为 bool数组的返回值只有两种情况)。这是我们可以定义一个map[][]作为对visited数组的扩充。。。。用来标记路上可能遇到的各种情况。

代码如下:

/** 1242_3.cpp**  Created on: 2013年8月16日*      Author: Administrator*      我喜欢章泽天。。。。。。*/#include <iostream>
#include <queue>using namespace std;/*** n : 用来记录行数* m : 用来记录列数* x_e,y_e :用来记录结束位置* x_s,y_s :用来记录开始位置*/
int n,m;
int x_e,y_e;
int x_s,y_s;//map[201][201]:用来存储可能遇到的各种情况
int map[201][201];
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};struct State{int x;int y;int step_counter;
};bool checkState(State st){if(map[st.x][st.y] == 1 || st.x < 0 || st.x >= n || st.y < 0 || st.y >= m ){return true;}return false;}int bfs(){queue<State> q;State st,now,next;st.x = x_s;st.y = y_s;st.step_counter = 0;q.push(st);map[st.x][st.y] = 1;while(!q.empty()){now = q.front();if(now.x == x_e && now.y == y_e){return now.step_counter;}int i;for(i = 0 ; i < 4 ; ++i){next.x = now.x + dir[i][0];next.y = now.y + dir[i][1];if(checkState(next)){//排除了墙壁以及越界的情况continue;}if(map[next.x][next.y] == -1){//处理碰到的是敌人的情况next.step_counter = now.step_counter + 2;}else{//处理是道路的情况next.step_counter = now.step_counter + 1;}q.push(next);map[next.x][next.y] = 1;}q.pop();}return -1;
}
int main(){while(scanf("%d%d",&n,&m)!=EOF){memset(map,0,sizeof(map));int i,j;char str[205];for( i = 0 ; i < n ; ++i){scanf("%s",str);//注意这种写法for(j = 0 ; str[j] ; ++j){//当遇到'\0'的时候循环结束if(str[j] == 'r'){x_s = i;y_s = j;}else if(str[j] == 'a'){x_e = i;y_e = j;}else if(str[j] == '.'){map[i][j] = 0;}else if(str[j] == '#'){map[i][j] = 1;}else if(str[j] == 'x'){map[i][j] = -1;}}}int ans = bfs();if(ans == -1){printf("Poor ANGEL has to stay in the prison all his life.\n");}else{printf("%d\n",ans);}}
}

(step4.2.3)hdu 1242(Rescue——BFS)相关推荐

  1. HDU 1242 Rescue BFS+优先队列

    题目链接:点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=1242 #include <stdio.h> #include <stri ...

  2. 1242 Rescue BFS

    #include<iostream> #include<string> #include<string.h> #include<stdio.h> #in ...

  3. HDU 1242 Rescue

    bfs问题. Angel有被关在监狱,她有非常多朋友要去救她. #表示墙,.表示路,x表示警卫,r表示她的朋友. 因为可能有非常多朋友,可是Angel仅仅有一个,所以搜索起点设为Angel.仅仅要找到 ...

  4. *【HDU - 1242 】 Rescue (反向dfs,或bfs)

    题干: Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N ...

  5. BFS HDOJ 1242 Rescue

    题目传送门 题意:从r走到a,遇到x多走一步,问最小走到a的步数 分析:因为r有多个,反过来想从a走到某个r的最小步数,简单的BFS.我对这题有特殊的感情,去年刚来集训队时肉鸽推荐了这题,当时什么都不 ...

  6. hdu - 1072 Nightmare(bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1072 遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造 ...

  7. HDU - 1043 Eight(bfs打表)

    题目链接:点击查看 题目大意:八数码经典问题,给出一个3*3的矩阵,其中随机分布着1~8的数字以及一个空位(我们用x来表示空位),在整个矩阵中,每一次操作都可以将x和他附近的方块互换,问经过多少次操作 ...

  8. hdu 1241Oil Deposits(BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Othe ...

  9. 1242 Rescue

    题目详情: Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

最新文章

  1. Openfiler开启iSCSI服务(iSCSI Target)
  2. java中extends ,implements的顺序问题
  3. MoCo论文中的Algorithm 1伪代码解读
  4. Faster R-CNN
  5. 新建文件注释_PDF汇总注释原来如此简单
  6. python刷阅读_用Python汇集并生成每日教育动态
  7. 黑马ee在职进阶视频_进阶– Java EE 7前端5强
  8. c# oracle datasource,C# 连接Oracle 数据库 示例源码下载
  9. [CQOI2014]数三角形 题解(找规律乱搞)
  10. phpstrpos不存在_深入理解PHP之strpos
  11. Android seekbar进度正负值
  12. xmp文件格式怎么导入ps?ACR预设安装方法
  13. 微信小程序文件大小限制
  14. H264/AVC-NALU解析
  15. 人工智能(AI)第一节课总结
  16. c语言不报错但输不出正确结果,正则表达式,c++_C语言 正则表达式结果不对?,正则表达式,c++ - phpStudy...
  17. java queue toarray_Java PriorityBlockingQueue toArray()用法及代码示例
  18. HDu--我要拿走你的蜡烛
  19. 三角公式以及常见关系
  20. Springboot宠物医院系统

热门文章

  1. Quartus II下载器件库(新)
  2. RDD.repartition
  3. PCB设计中的的3W规则和20H原则
  4. kali安装步骤失败 选择并安装软件_手机软件安装失败?吉米柚教你几招!
  5. 嵌入式入门学习笔记1:资料收集
  6. 外贸如何找客户邮箱?
  7. 计算机二级c语言考点解析,2017年计算机二级C语言考点解析
  8. 零基础 学 python开发 (Genius套餐A) 四十一
  9. DHU Matlab Experiment【2】作业记录_第三章、第四章
  10. Google Chrome 源码下载