博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu Problem 1242 Rescue bfs + 优先队列
阅读量:6886 次
发布时间:2019-06-27

本文共 4453 字,大约阅读时间需要 14 分钟。

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24233    Accepted Submission(s): 8550


Problem Description
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
 

Input
First line contains two integers stand for N and M.
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend. 
Process to the end of the file.
 

Output
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life." 
 

Sample Input
 
7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
 

Sample Output
这是刚写的, 因为题中说有多个朋友,所以原来的就不太对了
 
#include <bits/stdc++.h> using namespace std; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; bool vis[205][205]; char mp[205][205]; int ex, ey, n, m; struct node{     int x, y, step;     friend bool operator < (node a, node b) {         return a.step > b.step;     } }temp, a, b; int bfs(node x) {     priority_queue<node> que;     x.step = 0;     vis[x.x][x.y] = true;     while (!que.empty()) que.pop();     que.push(x);     while (que.size()) {         a = que.top();         que.pop();         //printf("%d %d %d\n", a.x, b.y, a.step);         if (a.x == ex && a.y == ey) return a.step;         for (int i = 0; i < 4; i++) {             b.x = a.x + dx[i];             b.y = a.y + dy[i];             b.step = a.step + 1;             if (b.x < 0 || b.x >= n || b.y < 0 || b.y >= m || mp[b.x][b.y] == '#')                 continue;             if (vis[b.x][b.y]) continue;             if (mp[b.x][b.y] == 'x') b.step++;             que.push(b);             vis[b.x][b.y] = true;         }     }     return -1; } int main() {     while (scanf("%d%d", &n, &m) != EOF) {         for (int i = 0; i < n; i++) {             scanf("%s", mp[i]);             for (int j = 0; j < m; j++) {                 if (mp[i][j] == 'a') {                     ex = i, ey = j;                   //  printf("%d %d\n", ex, ey);                 }             }         }         int ans = 1e9;         for (int i = 0; i < n; i++) {             for (int j = 0; j < m; j++) {                 if (mp[i][j] == 'r') {                     memset(vis, false, sizeof(vis));                     temp.x = i, temp.y = j;                     int t = bfs(temp);                     if (t != -1)                         ans = min(ans, t);                 }             }         }         if (ans != 1e9)              printf("%d\n", ans);         else printf("Poor ANGEL has to stay in the prison all his life.\n");     }     return 0; }
这是原来的:
#include <cstdio>
#include 
#include
#include
#include
#define MIN(a, b) (a < b)? a: b#define MAX_N 1000using namespace std;typedef pair
P;const int INF = 99999;char maze[MAX_N][MAX_N];bool vis[MAX_N][MAX_N];int dx[4] = {0, 1, 0, -1};int dy[4] = {1, 0, -1, 0};int w, h;struct node { int x; int y; int step; friend bool operator < (node a,node b) { return a.step > b.step; }}temp, Next;int bfs(int x, int y ,int x2, int y2){ memset(vis, false, sizeof(vis)); priority_queue
que; if (!que.empty()) que.pop(); temp.x = x, temp.y = y; temp.step = 0; que.push(temp); while (que.size()) { temp = que.top(); que.pop(); if (temp.x == x2 && temp.y == y2) return temp.step; for (int i = 0; i < 4; i++) { Next.x = dx[i] + temp.x; Next.y = dy[i] + temp.y; if (Next.x >= h || Next.y >= w || Next.x < 0 || Next.y < 0 || maze[Next.x][Next.y] == '#' || vis[Next.x][Next.y]) continue; Next.step = temp.step + 1; vis[Next.x][Next.y] = true; if (maze[Next.x][Next.y] == 'x') { Next.step++; } que.push(Next); } } return -1;}int main(){ int sy, sx, ex, ey; while (scanf("%d %d", &h, &w) != EOF) { int ans; for (int i = 0; i < h; i++) { scanf("%s", &maze[i]); } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if(maze[i][j] == 'r') { sx = i; sy = j; } if(maze[i][j] == 'a') { ex = i; ey = j; } } } ans = bfs(sx, sy, ex, ey); if (ans == -1) printf("Poor ANGEL has to stay in the prison all his life.\n"); else printf("%d\n", ans); } return 0;}

转载于:https://www.cnblogs.com/cniwoq/p/6770961.html

你可能感兴趣的文章
喜获Lync MVP感想
查看>>
SFB 项目经验-53-Outlook 2016-连接-Exchange 2016-不成功
查看>>
编程实现自动填充Outlook 2010登录密码并登录
查看>>
一个编译失败引起的问题
查看>>
SQL 将一列多行数据合并为一行 FOR XML PATH
查看>>
zabbix3.x.x升级教程
查看>>
Go 标准库介绍六: log
查看>>
Dhcp Policy(转)
查看>>
arccatalog连接arcgis server时,提示拒绝访问
查看>>
Android Service与Activity之间通信的几种方式
查看>>
表格模板
查看>>
git reset
查看>>
我的友情链接
查看>>
linux内核和发行版本介绍
查看>>
Linux下网络启动服务器安装和配置方法(pxe+tftp+dhcpd)
查看>>
salt 安装脚本
查看>>
获取Spring容器中的Bean
查看>>
ORA-01210: data file header is media corrupt
查看>>
Aerospike开发指南【中文】
查看>>
Python批量修改一个目录文件名
查看>>