Rescue
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.)
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.
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
13
bfs + 优先队列
memset将每个字节置为00000001,而不是00000000。
优先队列:b.ccount放在a.ccount前。#include <iostream> #include <cstdio> #include<cstring> #include<queue> using namespace std; const int maxn = 205; char sta[maxn][maxn]; int vis[maxn][maxn]; int dir[4][2]= {1,0,-1,0,0,1,0,-1}; int n,m,x2,y2; struct state { int x; int y; int ccount; friend bool operator<(state a,state b) { return b.ccount < a.ccount; } }; int check(int x,int y) { if(x<0 || y<0 || x>=n || y>=m || !vis[x][y] || sta[x][y] == '#') return 1; else return 0; } int bfs(int x,int y) { int i; priority_queue<state>p; struct state next,t; t.x=x; t.y=y; t.ccount=0; p.push(t); vis[x][y]=0; while(!p.empty()) { t=p.top(); p.pop(); if(t.x==x2&&t.y==y2) { return t.ccount; } for(i=0; i<4; i++) { next=t; next.x+=dir[i][0]; next.y+=dir[i][1]; if(check(next.x,next.y)) continue; next.ccount++; if(sta[next.x][next.y]=='x') next.ccount++; if(vis[next.x][next.y]>=next.ccount) { vis[next.x][next.y]=next.ccount; p.push(next); } } } return -1; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { int i,j,x1,y1,ans; for(i=0; i<n; i++) { for(j=0; j<m; j++) { scanf(" %c",&sta[i][j]); } } for(i=0; i<n; i++) { for(j=0; j<m; j++) { if(sta[i][j]=='r') { x1=i; y1=j; } else if(sta[i][j]=='a') { x2=i; y2=j; } } } memset(vis,1,sizeof(vis)); ans=bfs(x1,y1); if(ans==-1) printf("Poor ANGEL has to stay in the prison all his life.\n"); else printf("%d\n",ans); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合终身会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 理解 .NET 结构体字段的内存布局
· .NET 9中的异常处理性能提升分析:为什么过去慢,未来快
· 字符集、编码的前世今生
· Web性能优化:从 2 秒到200毫秒
· WPF 使用GDI+提取图片主色调并生成Mica材质特效背景
· DeepSeek为什么现在感觉不火了?
· 【故障公告】博客主站遭遇很奇怪的疯狂攻击
· 我与博客园的20年
· 一个老程序员, 两个小时能用corsur做出什么样的东西
· 刚刚,Cursor 1.0炸裂发布!4大亮点实战