当前位置:网站首页>Borg maze (bfs+ minimum spanning tree) (problem solving report)
Borg maze (bfs+ minimum spanning tree) (problem solving report)
2022-07-06 16:02:00 【It's Xiao Zhang, ZSY】
Borg Maze
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.
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
Have you guys explained , Why eliminate line breaks here gets(g[0]);
and getchar()
no way , look for bug For a long time , Just found it ,qaq
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
int const INF=0x3f3f3f3f;
char g[100][100];
int n,m,tol,k;
int a[100][100],cost[1100][1100],t[1000][1000];
int mo[][2]={
{
-1,0},{
1,0},{
0,1},{
0,-1}};
bool vis[1100];
int dis[1100];
void bfs(int sx,int sy)
{
queue<pair<int,int> >q;
while(!q.empty())
q.pop(); // Clear queue ;pop Pop up the element in the column header ;
memset(t,-1,sizeof(t));//t[][] Mark whether to walk , And record the distance ;
t[sx][sy]=0;
q.push(make_pair(sx,sy));//push Push the element into the tail
while(!q.empty())
{
pair<int,int> now=q.front();// Visit team leader elements
q.pop();
if(a[now.first][now.second]!=-1)// encounter A or S character , Update distance cost;
{
cost[a[sx][sy]][a[now.first][now.second]]=t[now.first][now.second];
}
for(int i=0;i<4;i++)
{
int tx=now.first+mo[i][0];// Up, down, left and right of the current point ;
int ty=now.second+mo[i][1];
if(g[tx][ty]=='#'||t[tx][ty]!=-1)//t[][] Mark whether to walk ;
continue;
else
{
t[tx][ty]=t[now.first][now.second]+1;// Record the distance ;
q.push(make_pair(tx,ty));
}
}
}
}
int Prim(int tol)// It's familiar again prim Algorithm ;
{
int ans=0,p;
memset(vis,0,sizeof(vis));
vis[0]=1;
for(int i=1;i<tol;i++)
dis[i]=cost[0][i];
for(int i=1;i<tol;i++)
{
int minn=INF;
p=-1;
for(int j=0;j<tol;j++)
{
if(vis[j]==0&&minn>dis[j])
{
p=j;
minn=dis[j];
}
}
if(minn==INF)
return -1;
vis[p]=1;
ans+=minn;
for(int j=0;j<tol;j++)
{
if(vis[j]==0&&dis[j]>cost[p][j])
dis[j]=cost[p][j];
}
}
return ans;
}
int main()
{
int k;
scanf("%d",&k);
while(k--)
{
scanf("%d%d",&m,&n);
gets(g[0]);
tol=0;
memset(a,-1,sizeof(a));
for(int i=0;i<n;i++)
{
gets(g[i]);// The input character ,gets There must be clear spaces ;
for(int j=0;j<m;j++)
{
if(g[i][j]=='A'||g[i][j]=='S')
a[i][j]=tol++;// use a Array tags A and S,( From the first few A/S To the first few A/S);
// A total of tol individual A and S, Be careful tol from 0 Start ;
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[i][j]!=-1)// Let each point be a starting point ;
bfs(i,j);
}
}
printf("%d\n",Prim(tol));
}
return 0;
}
边栏推荐
- mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
- 信息安全-安全专业名称|CVE|RCE|POC|VUL|0DAY
- 差分(一维,二维,三维) 蓝桥杯三体攻击
- MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’
- F - Birthday Cake(山东省赛)
- [exercise-9] Zombie's Treasury test
- 【练习-6】(Uva 725)Division(除法)== 暴力
- Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
- Gartner: five suggestions on best practices for zero trust network access
- Common configuration files of SSM framework
猜你喜欢
【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)
D - Function(HDU - 6546)女生赛
Nodejs+vue网上鲜花店销售信息系统express+mysql
Penetration test (8) -- official document of burp Suite Pro
信息安全-威胁检测引擎-常见规则引擎底座性能比较
渗透测试 ( 8 ) --- Burp Suite Pro 官方文档
Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
STM32 how to use stlink download program: light LED running light (Library version)
Information security - Analysis of security orchestration automation and response (soar) technology
Nodejs+vue online fresh flower shop sales information system express+mysql
随机推荐
Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool
初入Redis
【练习-6】(PTA)分而治之
[exercise-6] (PTA) divide and conquer
信息安全-威胁检测-NAT日志接入威胁检测平台详细设计
Cost accounting [22]
SSM框架常用配置文件
信息安全-史诗级漏洞Log4j的漏洞机理和防范措施
X-forwarded-for details, how to get the client IP
Cost accounting [14]
7-1 懂的都懂 (20 分)
Record of force deduction and question brushing
Accounting regulations and professional ethics [3]
洛谷P1102 A-B数对(二分,map,双指针)
【练习-8】(Uva 246)10-20-30==模拟
[exercise-8] (UVA 246) 10-20-30== simulation
Research Report on market supply and demand and strategy of China's earth drilling industry
nodejs爬虫
C 基本语法
Research Report on market supply and demand and strategy of geosynthetics industry in China