Morass

Killing Aliens in Borg Maze

Mar 26th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define PB push_back
  4. #define ZERO (1e-10)
  5. #define INF (1<<29)
  6. #define CL(A,I) (memset(A,I,sizeof(A)))
  7. #define DEB printf("DEB!\n");
  8. #define D(X) cout<<"  "<<#X": "<<X<<endl;
  9. #define EQ(A,B) (A+ZERO>B&&A-ZERO<B)
  10. typedef long long ll;
  11. typedef long double ld;
  12. typedef pair<ll,ll> pll;
  13. typedef vector<int> vi;
  14. typedef pair<int,int> ii;
  15. typedef vector<ii> vii;
  16. #define IN(n) int n;scanf("%d",&n);
  17. #define FOR(i, m, n) for (int i(m); i < n; i++)
  18. #define REP(i, n) FOR(i, 0, n)
  19. #define F(n) REP(i, n)
  20. #define FF(n) REP(j, n)
  21. #define FT(m, n) FOR(k, m, n)
  22. #define aa first
  23. #define bb second
  24. void ga(int N,int *A){F(N)scanf("%d",A+i);}
  25. #define MX (55)
  26. #define GL(S) fgets(S,MX,stdin)
  27. #define in(X,Y) (~X&&~Y&&X<N&&Y<M)
  28. char s[MX][MX];
  29. int N,M,L,E,sx[]={1,-1,0,0},sy[]={0,0,-1,1},A[MX][MX];
  30. struct nd{
  31.    int a,b,w,is;
  32.    bool operator<(const nd&r)const{return w<r.w;}
  33. }g[1<<14];
  34. int C[128];
  35. int geco(int a){
  36.     return C[a]=(a==C[a]?a:geco(C[a]));
  37. }
  38. bool con(int a,int b){
  39.     if(geco(a)==geco(b))return 0;
  40.     return C[C[b]]=C[a],1;
  41. }
  42. ll spa(int N,int M){
  43.     ll S(0);
  44.     sort(g,g+M);
  45.     iota(C,C+N,0);
  46.     F(M)if(con(g[i].a,g[i].b))S+=g[i].w,g[i].is=1;
  47.     return S;
  48. }
  49. int qx[2500],qy[2500],qp[2500],cn[MX][MX];
  50. void bfs(int x,int y,int l){
  51.     int b(-1),e(1),tx,ty,p;
  52.     CL(cn,-1),cn[*qx=x][*qy=y]=0;
  53.     while(++b<e){
  54.         x=qx[b],y=qy[b],p=qp[b]+1;
  55.         F(4){
  56.             tx=x+sx[i],ty=y+sy[i];
  57.             if(!in(tx,ty)||~cn[tx][ty]||s[tx][ty]==35)continue;
  58.             if(A[tx][ty])g[E++]={l-1,A[tx][ty]-1,p,0};
  59.             cn[tx][ty]=qp[e]=p,qx[e]=tx,qy[e++]=ty;
  60.         }
  61.     }
  62. }
  63. int main(void){
  64.     IN(tt)F(tt){
  65.         CL(A,E=L=0);
  66.         scanf("%d%d",&M,&N),GL(*s);
  67.         F(N)GL(s[i]);
  68.         F(N)FF(M)if(isupper(s[i][j]))A[i][j]=++L;
  69.         F(N)FF(M)if(A[i][j])bfs(i,j,A[i][j]);
  70.         printf("%d\n",(int)spa(L,E));
  71.     }
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment