poj 3026 brog maze bfs+prim 好坑爹的数据= =

题目连接:http://poj.org/problem?id=3026

这题数据太坑了。。。不看解题报告不知道。。。尼玛,m,n后面不能加getchar(),得加gets(s);尼玛是有多少个空格啊~wa了N遍啊

思路 先求点,然后对每一个点进行一次bfs,然后存距离,之后直接最小生成树。一开始我是用两个for循环去的值,直接超时。。。

代码:

View Code
  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <string.h>
  4 
  5 using namespace std;
  6 struct node
  7 {
  8     int x,y;
  9 }p[5005];
 10 struct queue
 11 {
 12     int x,y,step;
 13 }q[100005];
 14 int m,n,to[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
 15 int map[505][505];
 16 char str[505][505];
 17 int hash[505][505] = {0};
 18 int is_in(struct queue temp)
 19 {
 20     if(temp.x >= 0 && temp.x < n && temp.y >= 0 && temp.y < m )
 21     {
 22         return 1;
 23     }
 24     return 0;
 25 }
 26 
 27 void bfs(int s)
 28 {
 29     int vis[505][505] = {0};
 30     int f,r,i;
 31     f = r = 0;
 32     q[f].x = p[s].x;
 33     q[f].y = p[s].y;
 34     q[f].step = 0;
 35     vis[p[s].x][p[s].y] = 1;
 36     r++;
 37     while( f<r )
 38     {
 39         struct queue temp;
 40         temp = q[f++];
 41         for(i = 0;i < 4;i++)
 42         {
 43             struct queue now;
 44             now.x = temp.x+to[i][0];
 45             now.y = temp.y+to[i][1];
 46             now.step = temp.step+1;
 47             if(is_in(now)&&str[now.x][now.y] != '#' &&!vis[now.x][now.y])
 48             {
 49                 q[r++] = now;
 50                 vis[now.x][now.y] = 1;
 51                 if(str[now.x][now.y] != ' ')
 52                 map[s][hash[now.x][now.y]] = map[hash[now.x][now.y]][s] = now.step;
 53             }
 54         }
 55     }
 56 }
 57  int prim(int sn)
 58  {
 59      int i,j,primer,ans,min;
 60      int vis[505] = {0};
 61      int d[505];
 62      ans = 0;
 63      for(i = 0;i < sn;i++)
 64      d[i] = map[0][i];
 65      vis[0] = 1;
 66 
 67      for(i = 1;i < sn;i++)
 68      {
 69          min = 9999999;
 70          for(j = 1;j < sn;j++)
 71          {
 72              if(min > d[j]&& !vis[j])
 73              min = d[j],primer = j;
 74          }
 75          ans += min;
 76          vis[primer] = 1;
 77          for(j = 1;j < sn;j++)
 78          if(d[j] > map[primer][j] && !vis[j])
 79          d[j] = map[primer][j];
 80      }
 81      return ans;
 82  }
 83 int main()
 84 {
 85     int t,i,j,count;
 86     scanf("%d",&t);
 87     while(t--)
 88     {
 89         memset(hash,0,sizeof(hash));
 90         scanf("%d %d",&m,&n);
 91         char s[500];
 92         gets(s);
 93         for(i = 0;i < n;i++)
 94         {
 95             gets(str[i]);
 96         }
 97         count = 0;
 98         for(i = 0;i < n;i++)
 99         {
100             for(j = 0;j < m;j++)
101             if(str[i][j] != '#' && str[i][j] != ' ' )
102             {
103                 p[count].x = i;
104                 p[count].y = j;
105                 hash[i][j] = count;
106                 count++;
107             }
108         }
109 
110         for(i = 0;i < count;i++)
111         {
112             bfs(i);
113         }
114         printf("%d\n",prim(count));
115     }
116     return 0;
117 }
posted @ 2012-11-15 00:32  某某。  阅读(207)  评论(0编辑  收藏  举报