POJ 3026 Borg Maze(bfs+prim)

题目链接

题意也是挺难懂的。把所有的A,S看成一个点,求这些点的最小生成树。先BFS预处理出来然后prim,注意m,n后边的空格问题,然后数组开小了,也错了次。

  1 #include <cstdio>

  2 #include <cstring>

  3 #include <string>

  4 #include <iostream>

  5 using namespace std;

  6 int p[1001][1001],low[1001];

  7 char str[101][101];

  8 int o[301][301],key[301][301],n,m,kk[1001];

  9 int a[4] = {0,0,1,-1};

 10 int b[4] = {1,-1,0,0};

 11 int quer[20000],quec[20000];

 12 void bfs(int num,int x,int y)

 13 {

 14     int st,end,i,j,step,k;

 15     st = end = 1;

 16     for(i = 0;i < n;i ++)

 17     {

 18         for(j = 0;j < m;j ++)

 19         {

 20             if(str[i][j] == '#')

 21             key[i][j] = 1;

 22             else

 23             key[i][j] = 0;

 24         }

 25     }

 26     quer[1] = x;

 27     quec[1] = y;

 28     p[num][num] = 0;

 29     key[x][y] = 1;

 30     step = 1;

 31     while(st <= end)

 32     {

 33         j = 1;

 34         for(i = st;i <= end;i ++)

 35         {

 36             for(k = 0;k <= 3;k ++)

 37             {

 38                 int r,c;

 39                 r = quer[i]+a[k];

 40                 c = quec[i]+b[k];

 41                 if(r >= 0&&r < n&&c >= 0&&c < m&&!key[r][c])

 42                 {

 43                     key[r][c] = 1;

 44                     if(str[r][c] == 'A'||str[r][c] == 'S')

 45                     p[num][o[r][c]] = step;

 46                     quer[end+j] = r;

 47                     quec[end+j] = c;

 48                     j ++;

 49                 }

 50             }

 51         }

 52         step ++;

 53         st = end+1;

 54         end = end+j-1;

 55     }

 56     return ;

 57 }

 58 int main()

 59 {

 60     int t,i,j,num,ans,mi,z;

 61     char ch[101];

 62     scanf("%d",&t);

 63     while(t--)

 64     {

 65         memset(o,0,sizeof(o));

 66         memset(key,0,sizeof(key));

 67         memset(kk,0,sizeof(kk));

 68         scanf("%d%d",&m,&n);

 69         gets(ch);

 70         for(i = 0;i < n;i ++)

 71         {

 72             gets(str[i]);

 73         }

 74         num = 1;

 75         for(i = 0;i < n;i ++)

 76         {

 77             for(j = 0;j < m;j ++)

 78             {

 79                 if(str[i][j] == 'S'||str[i][j] == 'A')

 80                 o[i][j] = num++;

 81                 else

 82                 key[i][j] = 1;

 83             }

 84         }

 85         num --;

 86         for(i = 0;i < n;i ++)

 87         {

 88             for(j = 0;j < m;j ++)

 89             {

 90                 bfs(o[i][j],i,j);

 91             }

 92         }

 93         ans = 0;

 94         for(i = 1;i <= num;i ++)

 95         low[i] = p[1][i];

 96         kk[1] = 1;

 97         for(i = 1;i <= num-1;i ++)

 98         {

 99             mi = 100000;

100             for(j = 1;j <= num;j ++)

101             {

102                 if(mi > low[j]&&!kk[j])

103                 {

104                     mi = low[j];

105                     z = j;

106                 }

107             }

108             kk[z] = 1;

109             ans += mi;

110             for(j = 1;j <= num;j ++)

111             {

112                 if(low[j] > p[z][j]&&!kk[j])

113                 low[j] = p[z][j];

114             }

115         }

116         printf("%d\n",ans);

117     }

118     return 0;

119 }

 

你可能感兴趣的:(Prim)