Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6905 | Accepted: 2315 |
Description
Input
Output
Sample Input
2 6 5 ##### #A#A## # # A# #S ## ##### 7 7 ##### #AAA### # A# # S ### # # #AAA### #####
Sample Output
8 11
Source
3026 | Accepted | 352K | 63MS | C++ | 2634B | 2013-07-31 14:44:02 |
#include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; const int maxn = 50+10; const int maxp = 100+10; int n, m; int map[maxn][maxn]; //输入的图 int w[maxp][maxp]; //路径 int v[maxn][maxn]; // 标记是否访问 int p[maxp]; // 父亲节点 int dir[4][2] = {0,1, 1,0, 0,-1, -1,0}; // 四个方向 struct Edge{ int u,v; int w; }edge[maxp*maxp]; struct Point{ int x,y; int step; }; void bfs(int x, int y) // 遍历第 map[x][y] 个点 { Point point; point.x = x; point.y = y; point.step = 0; //自己到自己距离为 0 queue<Point> q; q.push(point); //起点入队 memset(v, 0, sizeof(v)); v[x][y] = 1; //标记访问 int num = 1; //已经找的点数 while(!q.empty()) { Point now = q.front(); //取队首 q.pop(); // 出队 Point next; //找下一个点 for(int i = 0; i < 4; i++) // 遍历四个方向找下一个点 { next.x = now.x+dir[i][0]; next.y = now.y+dir[i][1]; next.step = now.step+1; // 步数+1 //如果可以走, 并且没有被访问过 if(map[next.x][next.y] >= 0 && !v[next.x][next.y]) { q.push(next); //入队 v[next.x][next.y] = 1; // 标记被访问 if(map[next.x][next.y] > 0) // 如果是要找的点 {//建图 edge[m].u = map[x][y]; edge[m].v = map[next.x][next.y]; edge[m++].w = next.step; num++; if(num == n) return; //所有的点都找完了 } } } } return; } bool cmp(Edge a, Edge b) { return a.w < b.w; } int find(int x) { return x == p[x] ? x : p[x] = find(p[x]); } int Kruskal() { int ans = 0; for(int i = 1; i <= n; i++) p[i] = i; sort(edge, edge+m, cmp); for(int i = 0; i < m; i++) { int u = find(edge[i].u); int v = find(edge[i].v); if(u != v) { p[v] = u; ans += edge[i].w; } } return ans; } int main() { int T; int row,col; char tmp[maxn]; char c; scanf("%d", &T); while(T--) { scanf("%d%d", &col,&row); gets(tmp);//坑【一串空格】 n = m = 0; for(int i = 1; i <= row; i++) { for(int j = 1; j <= col; j++) { scanf("%c", &c); if(c == '#') map[i][j] = -1; else if(c == ' ') map[i][j] = 0; else map[i][j] = ++n; } getchar(); } for(int i = 0; i <= row; i++) { for(int j = 0; j <= col; j++) if(map[i][j] > 0) bfs(i,j); } int ans = Kruskal(); printf("%d\n", ans); } return 0; }
3026 | Accepted | 264K | 63MS | C++ | 2543B | 2013-07-31 14:44:34 |
#include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; const int maxn = 50+10; const int maxp = 110; const int INF = maxn*maxn; int n; int map[maxn][maxn]; int w[maxp][maxp]; int d[maxp]; int vis[maxp]; int v[maxn][maxn]; struct Point{ int x,y; int step; }; int dir[4][2] = {0,1, 1,0, 0,-1, -1,0}; void bfs(int x, int y) { Point point; point.x = x; point.y = y; point.step = 0; memset(v,0,sizeof(v)); queue<Point> q; q.push(point); v[x][y] = 1; int num = 1; while(!q.empty()) { Point now = q.front(); q.pop(); Point next; for(int i = 0; i < 4; i++) { next.x = now.x+dir[i][0]; next.y = now.y+dir[i][1]; if(map[next.x][next.y] >= 0 && !v[next.x][next.y]) { next.step = now.step+1; v[next.x][next.y] = 1; q.push(next); if(map[next.x][next.y] > 0) { int u = map[x][y]; int v = map[next.x][next.y]; w[u][v] = next.step; num++; if(num == n) return; } } } } return; } int Prime() { int ans = 0; for(int i = 1; i <= n; i++) d[i] = INF; d[1] = 0; memset(vis, 0, sizeof(vis)); for(int i = 1; i <= n; i++) { int x, m = INF; for(int y = 1; y <= n; y++) if(!vis[y] && d[y] <= m) m = d[x=y]; vis[x] = 1; ans += d[x]; for(int y = 1; y <= n; y++) if(!vis[y]) d[y] = min(d[y], w[x][y]); } return ans; } int main() { int T; int row, col; scanf("%d", &T); while(T--) { n = 0; char c; scanf("%d%d", &col,&row); char tmp[51]; gets(tmp); for(int i = 1; i <= row; i++) { for(int j = 1; j <= col; j++) { scanf("%c", &c); if(c == '#') map[i][j] = -1; else if(c == ' ') map[i][j] = 0; else map[i][j] = ++n; } getchar(); } for(int i = 1; i <= row; i++) { for(int j = 1; j <= col; j++) if(map[i][j] > 0) bfs(i,j); } int ans = Prime(); printf("%d \n", ans); } return 0; }