图论 --- BFS + MST
Borg Maze
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7844 | Accepted: 2623 |
Description
The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to describe the group consciousness of the Borg civilization. Each Borg individual is linked to the collective by a sophisticated subspace network that insures each member is given constant supervision and guidance.
Your task is to help the Borg (yes, really) by developing a program which helps the Borg to estimate the minimal cost of scanning a maze for the assimilation of aliens hiding in the maze, by moving in north, west, east, and south steps. The tricky thing is that the beginning of the search is conducted by a large group of over 100 individuals. Whenever an alien is assimilated, or at the beginning of the search, the group may split in two or more groups (but their consciousness is still collective.). The cost of searching a maze is definied as the total distance covered by all the groups involved in the search together. That is, if the original group walks five steps, then splits into two groups each walking three steps, the total distance is 11=5+3+3.
Your task is to help the Borg (yes, really) by developing a program which helps the Borg to estimate the minimal cost of scanning a maze for the assimilation of aliens hiding in the maze, by moving in north, west, east, and south steps. The tricky thing is that the beginning of the search is conducted by a large group of over 100 individuals. Whenever an alien is assimilated, or at the beginning of the search, the group may split in two or more groups (but their consciousness is still collective.). The cost of searching a maze is definied as the total distance covered by all the groups involved in the search together. That is, if the original group walks five steps, then splits into two groups each walking three steps, the total distance is 11=5+3+3.
Input
On
the first line of input there is one integer, N <= 50, giving the
number of test cases in the input. Each test case starts with a line
containg two integers x, y such that 1 <= x,y <= 50. After this, y
lines follow, each which x characters. For each character, a space ``
'' stands for an open space, a hash mark ``#'' stands for an obstructing
wall, the capital letter ``A'' stand for an alien, and the capital
letter ``S'' stands for the start of the search. The perimeter of the
maze is always closed, i.e., there is no way to get out from the
coordinate of the ``S''. At most 100 aliens are present in the maze, and
everyone is reachable.
Output
For every test case, output one line containing the minimal cost of a succesful search of the maze leaving no aliens alive.
Sample Input
2 6 5 ##### #A#A## # # A# #S ## ##### 7 7 ##### #AAA### # A# # S ### # # #AAA### #####
Sample Output
8 11
#include<queue> #include<cstdio> #define INF 1<<30 #include<cstring> #include<algorithm> using namespace std; int n,m,ans,cnt; struct Node { int x,y,step; }; Node node[110]; int row,line; int dist[60]; char Map[60][60]; bool vis[60][60],vist[60]; //邻接矩阵存储 int mark[60][60],dis[110][110]; //给每个字母一个编号 int dir[4][2]={-1,0, 0,1, 1,0, 0,-1}; void bfs(int index) { queue<Node> que; memset(vis, false, sizeof(vis)); Node now, next; node[index].step = 0; que.push(node[index]); vis[node[index].x][node[index].y] = true; while(!que.empty()) { now = que.front(); que.pop(); int x = now.x, y = now.y; for(int i = 0; i < 4; ++i) { int tx = x + dir[i][0], ty = y +dir[i][1]; if(vis[tx][ty] == false && Map[tx][ty] != '#') //如果这一步可以走 { next.x = tx; next.y = ty; vis[tx][ty] = true; next.step = now.step + 1; que.push(next); if(Map[next.x][next.y] == 'A' || Map[next.x][next.y] == 'S') dis[ mark[ node[index].x ][ node[index].y ] ][ mark[next.x][next.y] ] = next.step; } } } } int prim() { for(int i = 0; i < cnt; ++i) { dist[i] = INF; vist[i] = false; } dist[0] = 0; while(1) { int min = INF, now = -1; for(int i = 0; i < cnt; ++i) { if(min > dist[i] && vist[i] == false) { min = dist[i]; now = i; } } if(now == -1) return ans; ans += min; vist[now] = true; for(int i = 0; i < cnt; ++i) if(vist[i] == false && dist[i] > dis[now][i]) dist[i] = dis[now][i]; } return ans; } int main() { int n_case; scanf("%d", &n_case); while(n_case--) { cnt = ans = 0; memset(mark, 0, sizeof(mark)); scanf("%d%d", &row, &line); char ch; while(ch = getchar(), ch != '\n'); for(int i = 0; i < line; ++i) { for(int j = 0; j < row; ++j) { Map[i][j] = getchar(); if(Map[i][j] == 'A' || Map[i][j] == 'S') { mark[i][j] = cnt; node[cnt].x = i; node[cnt++].y = j; } } while(ch = getchar(), ch != '\n'); } for(int i = 0; i < cnt; ++i) bfs(i); prim(); printf("%d\n", ans); } return 0; }
作者:北岛知寒
出处:https://www.cnblogs.com/crazyacking/p/3751838.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?