国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

poj 3026 Borg Maze(bfs+prim)

2019-11-14 12:50:03
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

http://blog.csdn.net/lyy289065406/article/details/6645991題目翻譯的很好,反正我是看不懂題目

#include <iostream>#include <cstdio>#include <utility>#include <algorithm>#include <queue>#include <cstring>using namespace std;char g[100][100];int n,m;int a[100][100];int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};int cost[110][110];int t[100][100];void bfs(int sx, int sy){ queue<pair<int, int> > q; while(!q.empty()) q.pop(); memset(t,-1,sizeof(t)); t[sx][sy] = 0; q.push(make_pair(sx,sy)); while(!q.empty()) { pair<int, int> now = q.front(); q.pop(); if(a[now.first][now.second] != -1) 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 + dir[i][0]; int ty = now.second + dir[i][1]; if(g[tx][ty] == '#' || t[tx][ty] != -1) continue; t[tx][ty] = t[now.first][now.second] + 1; q.push(make_pair(tx,ty)); } }}const int INF = 99999999;int book[110];int dis[110];int PRim(int n){ int res = 0; memset(book,0,sizeof(book)); book[0] = 1; for(int i = 0; i < n; ++i) dis[i] = cost[0][i]; int cnt = 1,minn = INF,t; while(cnt < n) { minn = INF; for(int i = 0; i < n; ++i) { if(book[i] == 0 && minn > dis[i]) { minn = dis[i]; t = i; } } book[t] = 1; cnt++; res += dis[t]; for(int i = 0; i < n; ++i) { if(book[i] == 0 && dis[i] > cost[t][i]) dis[i] = cost[t][i]; } } return res;}int main(){ int t; scanf("%d",&t); while(t--) { //后邊的/n是用來(lái)吃掉換行符的,很納悶用getchar()竟然會(huì)wa scanf("%d %d/n",&m,&n); memset(a,-1,sizeof(a)); int tol = 0; for(int i = 0; i < n; ++i) { gets(g[i]); for(int j = 0; j < m; ++j) { if(g[i][j] == 'A' || g[i][j] == 'S') a[i][j] = tol++; } } for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) if(a[i][j] != -1) bfs(i,j); printf("%d/n",prim(tol)); } return 0;}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 定边县| 会昌县| 龙岩市| 类乌齐县| 禹城市| 比如县| 香河县| 梧州市| 财经| 大理市| 资中县| 淳化县| 紫阳县| 丹东市| 思南县| 桂东县| 博白县| 合山市| 呼图壁县| 简阳市| 瑞昌市| 和林格尔县| 博客| 新密市| 鸡泽县| 商丘市| 天峻县| 万源市| 百色市| 天门市| 渭源县| 安宁市| 岱山县| 开江县| 新乐市| 武鸣县| 信阳市| 韶山市| 科技| 临泉县| 汉沽区|