日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【POJ - 3026】Borg Maze(bfs预处理 + 最小生成树,建图)

發布時間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ - 3026】Borg Maze(bfs预处理 + 最小生成树,建图) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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

題目大意:

這個題意是真難懂啊、、、

在一個迷宮中有很多外星人A,某個人在S點開始去抓捕,抓捕到外星人后這個人可以分身為任意個(也可以不分身),求把所有外星人抓完所用的最小距離,距離包括分身走的距離(即所有的距離都算上,比如:兩個分身同時走三步,那么走的距離就是6,而不是3),在迷宮之中只能向東南西北四個方向移動。

換種說法:

給你一個地圖,地圖中A代表外星人,S代表出發點,從S點出發,在起點S 或是 每遇到一個A點 便可不分裂或是分裂成多個。求把所有A都吃完需要多少步。注意不要以為當前點隨時都能分裂,而是只有在S或是遇到A才能分裂。

解題報告:

? 注意這題樣例 n m 和字符串之間可能有一行空行(而不一定是一個空格)真tm格式、、所以getchar會WA的。。

因為不難發現,既然這個題意的話,那么起點在哪并沒有怎么所謂了,因為S的所有特點和A的所有特點是完全相同的,所以可以把S點也看成是一個A點,那么題意轉化成:從一個A點出發,在所有A點相連的路徑中選擇一些路徑,使得走過的路最小。

那么解法很顯然了,假設有tot個S和A,那么用bfs構造出這tot個點之間的距離,然后求個MST就好,因為稠密圖而且是完全圖所以首選prim。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 200 + 5; const int INF = 0x3f3f3f3f; int n,m; char s[MAX][MAX]; int bk[MAX][MAX],cost[MAX][MAX],tot; bool vis[MAX][MAX]; int nx[4] = {0,1,0,-1}; int ny[4] = {1,0,-1,0}; struct Node {int x,y,t;Node(int x=0,int y=0,int t=0):x(x),y(y),t(t){} }; void bfs(int stx,int sty) {int id1 = bk[stx][sty];queue<Node> q;q.push(Node(stx,sty,0));memset(vis,0,sizeof vis);vis[stx][sty]=1;while(!q.empty()) {Node cur = q.front();q.pop();int id2 = bk[cur.x][cur.y];if(bk[cur.x][cur.y]) cost[id1][id2] = cur.t;for(int k = 0; k<4; k++) {int tx = cur.x+nx[k];int ty = cur.y+ny[k];if(tx<0||tx>n||ty<1||ty>m) continue;if(vis[tx][ty] || s[tx][ty] == '#') continue;vis[tx][ty] = 1;q.push(Node(tx,ty,cur.t+1));}} } int VIS[MAX*MAX],dis[MAX*MAX]; int prim() {int res = 0;for(int i = 1; i<=tot; i++) dis[i] = INF,VIS[i] = 0;dis[1] = 0;while(1) {int minv,minw=INF;for(int i = 1; i<=tot; i++) {if(VIS[i] == 0 && dis[i] < minw) minw = dis[i],minv = i;}if(minw == INF) break;VIS[minv] = 1;for(int i = 1; i<=tot; i++) {if(VIS[i] == 0 && dis[i] > cost[minv][i]) dis[i] = cost[minv][i];}res += dis[minv];}return res; } int main() {int t;cin>>t;while(t--) {tot=0;memset(bk,0,sizeof bk);memset(cost,0,sizeof cost);scanf("%d%d",&m,&n);string qq;getline(cin,qq);for(int i = 1; i<=n; i++) {cin.getline(s[i]+1,1005);for(int j = 1; j<=m; j++) {if(s[i][j] == 'S' || s[i][j] == 'A') bk[i][j] = ++tot;}}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(bk[i][j]) bfs(i,j);}}printf("%d\n",prim());} return 0 ; }

剛開始WA在數組就開了50*50這么大,導致cost數組也開了[50][50],但是其實應該[2501][2501],但是其實這題說了最多100個外星人,所以可以開[105][105]就夠了。

總結

以上是生活随笔為你收集整理的【POJ - 3026】Borg Maze(bfs预处理 + 最小生成树,建图)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产一区二区在线精品 | 亚洲国产久 | 国产免费观看久久黄av片 | 国产高清视频在线播放 | 清纯唯美亚洲 | 久久久久三级 | 男人的天堂亚洲 | 91原创视频在线观看 | 国产精品91av | 日韩高清免费观看 | 日韩性生活大片 | 免费看成人啪啪 | 日本大尺度床戏揉捏胸 | 91视频在线观看 | 亚洲蜜臀av乱码久久精品蜜桃 | 中文字幕影院 | 亚洲无码精品在线播放 | 97伊人| 精品人妻在线播放 | 日本五十肥熟交尾 | www.色妞| 日日夜夜伊人 | 91嫩草在线 | xxx视频网站 | 国产伦精品一区二区三区照片91 | 免费黄色成人 | 成人在线观看av | 精品一区不卡 | 国产又猛又黄又爽 | 成年女人免费视频 | 爱情岛论坛亚洲自拍 | 国产精品99久久久久久大便 | 性插视频在线观看 | 91精品国产91久久久久久久久久久久 | 国产日批视频在线观看 | 五月激情综合 | 一区二区国产精品精华液 | 干干操操 | 黄色xxxxxx | www.精品一区 | 国产在线www | 在线观看免费看片 | av在线不卡播放 | 制服丝袜手机在线 | 泰剧19禁啪啪无遮挡 | 91 高清 在线 制服 偷拍 | 一本一道av | 国产免费又黄又爽又色毛 | 日韩成人在线影院 | 欧美成人免费在线 | 中文字幕二区在线观看 | 美女扒开内裤让男人桶 | 天堂在线视频网站 | mm1313亚洲精品 | 三级爱爱 | 国产免费啪啪 | 西比尔在线观看完整视频高清 | 欧美激情成人在线 | 官场艳妇疯狂性关系 | www.成人| 999精品视频| 不卡视频免费在线观看 | 住在隔壁的她动漫免费观看全集下载 | 久久精品国产亚洲AV黑人 | 97国产精品人人爽人人做 | 综合五月天 | 亚洲精品日韩在线观看 | 黑丝一区二区三区 | 哪里可以看免费毛片 | 91精品国产一区二区无码 | 久久久无码18禁高潮喷水 | 亚洲精品第一页 | 青青草草视频 | 亚洲 欧美 视频 | 日韩中文免费 | 91天堂在线观看 | 男人的天堂2018 | 91在线精品一区二区三区 | 日韩少妇一区二区 | 婷婷91| 日韩久久久久久久久久 | 午夜视频福利网站 | 黄色高清在线观看 | 99亚洲国产精品 | 亚洲系列中文字幕 | 日本视频黄 | 桃色视频在线 | 日韩伦理一区 | 五月色婷婷综合 | 国产一区二区在线视频 | 狠狠婷婷| 狠狠干伊人网 | 欧美久久久久久 | av在线看片 | 国产一区二区三区视频在线 | 毛片在线网 | 国产性―交一乱―色―情人 | 欧美一区二区三区免费视频 | 亚洲精品图区 |