ZOJ-1649 Rescue
来源:互联网 发布:昆明医科大学网络课程 编辑:程序博客网 时间:2024/06/03 09:26
#include <cstdio>#include <queue>#include <cstring>using namespace std;const int n_max = 200 + 5;const int m_max = 200 + 5;int n, m, ans;bool flag;char prison[n_max][m_max];int sign[n_max][m_max];struct node{ int x; int y; int time;} op, ed;queue <node> process;int step[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};void bfs(){ while(!process.empty()) { node now = process.front(); process.pop(); if(prison[now.x][now.y] == 'x') //判断是否有guard。(此题关键) { now.time ++; prison[now.x][now.y] = '.'; process.push(now); sign[now.x][now.y] = 1; continue; } for(int i = 0; i < 4; i ++) { node next = now; next.x += step[i][0]; next.y += step[i][1]; if(next.x >= 0 && next.x < n && next.y >= 0 && next.y < m && prison[next.x][next.y] != '#' && !sign[next.x][next.y]) { if(prison[next.x][next.y] == 'a') { ans = next.time + 1; flag = true; return; } next.time ++; process.push(next); sign[next.x][next.y] = 1; } } }}int main(){ while(~scanf("%d %d", & n, & m)) { getchar(); memset(sign, 0, sizeof(sign)); while(!process.empty()) process.pop(); for(int i = 0; i < n; i ++) { for(int j = 0; j < m; j ++) { scanf("%c", & prison[i][j]); if(prison[i][j] == 'r') { op.x = i; op.y = j; op.time = 0; process.push(op); sign[i][j] = 1; } } getchar(); } flag = false; bfs(); if(flag) printf("%d\n", ans); else printf("Poor ANGEL has to stay in the prison all his life.\n"); }}
题目:
给一个矩阵。x代表警卫。#代表墙。r代表天使的朋友。a代表天使的位置。天使的朋友要去救天使。走一步花费1单位的时间,遇到警卫可以杀死它,要多花1单位的时间。问最短的时间是多少。(注意:朋友可以多个。如果救不出天使 则输出一段英文。)
题解:
用BFS宽度搜索最短路。BFS是按宽度不断扩展搜索的,但是碰到警卫会破坏这种搜索制度(一开始我认为只要遇到警卫,时间+2就行,实则不然)。必须对遇到警卫这种情况进行特殊处理。就是遇到警卫,执行的操作是将警卫杀死,使x变成.,花费1单位时间,不进行移步,并将这种状态投入队尾,使其达到时间上的统一。其他的算法都是和普通的BFS一样。(一开始也没看到题目说朋友可以多个?)
0 0
- zoj 1649 Rescue
- ZOJ 1649 Rescue
- zoj 1649 Rescue
- ZOJ 1649 Rescue (BFS)
- zoj 1649 rescue (bfs)
- ZOJ-1649-Rescue
- zoj 1649 Rescue
- ZOJ:1649 Rescue
- ZOJ 1649 Rescue
- ACM ZOJ 1649 (Rescue)
- ZOJ(1649)Rescue
- ZOJ 1649 Rescue
- zoj 1649 Rescue
- zoj 1649 Rescue
- ZOJ 1649 : Rescue - BFS
- ZOJ 1649 Rescue
- zoj 1649 Rescue
- zoj 1649 Rescue bfs
- hdu 5135__Little Zu Chongzhi's Triangles
- Ember
- 黑马程序员—UNIX基本指令及第一个Objective-C程序
- UVA 11464 - Even Parity
- 京东股权众筹投后总结和反思
- ZOJ-1649 Rescue
- Session对象的特点
- 计算几何--简单多边形与圆面积交
- HDU 5489 Removed Interval (LIS,变形)
- 京东股权众筹投后总结和反思2(冲动不要紧,有后悔药)
- 0SX10.11 启动盘
- hdu 5131__Song Jiang's rank list
- 惠普EPM实训-选课管理系统项目总结。
- MySQL check the manual that corresponds to your MySQL server version for the right syntax错误