Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
55126 | ZZQ | 最小生成树 (mst) | C++ | 解答错误 | 0 | 43 MS | 508 KB | 2720 | 2022-08-09 11:33:31 |
#include <bits/stdc++.h> using namespace std; char Map[51][51]; int Block[51][51]; int cnt; int Decition[4][2] = {{0,1},{0,-1},{-1,0},{1,0}}; int n,m; void DFS(int x,int y,int now) { Block[x][y] = now; for(int i = 0;i < 4;i++) { int xx = x + Decition[i][0],yy = y + Decition[i][1]; if(xx < 1 || xx > n || yy < 1 || yy > m) continue; if(Block[xx][yy]) continue; if(Map[xx][yy] != 'X') continue; DFS(xx,yy,now); } } int Min[4]; int vis[51][51]; bool ans[51][51]; int Ans; void Find(int x,int y,int from,int to,int cnt) { if(Block[x][y] == to) { Min[from + to - 2] = min(Min[from + to - 2],cnt - 2); return; } for(int i = 0;i < 4;i++) { int xx = x + Decition[i][0],yy = y + Decition[i][1]; if(xx < 1 || xx > n || yy < 1 || yy > m) continue; if(Map[xx][yy] != '.' && Block[xx][yy] != to) continue; if(++cnt >= vis[xx][yy]) continue; vis[xx][yy] = cnt; char c = Map[xx][yy]; Map[xx][yy] = 'a'; Find(xx,yy,from,to,cnt); cnt--; Map[xx][yy] = c; } } void Mark(int x,int y,int from,int to,int cnt,int R) { if(Block[x][y] == to && cnt - 2 == R) { for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) if(Map[i][j] == 'a') ans[i][j] = 1; return; } for(int i = 0;i < 4;i++) { int xx = x + Decition[i][0],yy = y + Decition[i][1]; if(xx < 1 || xx > n || yy < 1 || yy > m) continue; if(Map[xx][yy] != '.' && Block[xx][yy] != to) continue; if(++cnt >= vis[xx][yy]) continue; vis[xx][yy] = cnt; char c = Map[xx][yy]; Map[xx][yy] = 'a'; Mark(xx,yy,from,to,cnt,R); cnt--; Map[xx][yy] = c; } } int main() { memset(Min,63,sizeof(Min)); memset(vis,63,sizeof(vis)); scanf("%d%d%*c",&n,&m); for(int i = 1;i <= n;i++,getchar()) for(int j = 1;j <= m;j++) Map[i][j] = getchar(); for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) if((Map[i][j] == 'X') && (!Block[i][j])) DFS(i,j,++cnt); for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) if(Block[i][j] == 1) Find(i,j,1,2,0); for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) if(Block[i][j] == 1) Mark(i,j,1,2,0,Min[1]); for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) if(Block[i][j] == 1) Find(i,j,1,3,0); for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) if(Block[i][j] == 1) Mark(i,j,1,3,0,Min[2]); for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) if(Block[i][j] == 2) Find(i,j,2,3,0); for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) if(Block[i][j] == 2) Mark(i,j,2,3,0,Min[3]); for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) if(ans[i][j]) Ans++; printf("%d\n",Ans); return 0; }