USACO 2.4 Overfencing(BFS)

简单的BFS,可是看错题了,从原来的基础上改的,改的很乱,边界什么的。错了好几次。

  1 /*

  2   ID: cuizhe

  3   LANG: C++

  4   TASK: maze1

  5 */

  6 #include <iostream>

  7 #include <cstdio>

  8 #include <cstring>

  9 #include <cmath>

 10 #include <algorithm>

 11 using namespace std;

 12 char p[501][501];

 13 int o[501][501];

 14 int quer[100000],quec[100000];

 15 int x[4] = {0,0,-1,1};

 16 int y[4] = {1,-1,0,0};

 17 int main()

 18 {

 19     int w,h,j,i,k,str,end,num;

 20     FILE *fp;

 21     fp = freopen("maze1.in","r",stdin);

 22     freopen("maze1.out","w",stdout);

 23     scanf("%d%d%*c",&w,&h);

 24     for(i = 0; i <= 2*h; i ++)

 25     {

 26         fgets(p[i],2*w+3,fp);

 27     }

 28     str = end = 1;

 29     for(i = 0; i <= 2*h; i ++)

 30     {

 31         if(p[i][0] == ' ')

 32         {

 33             quer[end] = i;

 34             quec[end] = 0;

 35             o[i+1][1] = 1;

 36             end++;

 37         }

 38         if(p[i][2*w] == ' ')

 39         {

 40             quer[end] = i;

 41             quec[end] = 2*w;

 42             o[i+1][2*w+1] = 1;

 43             end++;

 44         }

 45     }

 46     for(i = 0; i <= 2*w; i ++)

 47     {

 48         if(p[0][i] == ' ')

 49         {

 50             quer[end] = 0;

 51             quec[end] = i;

 52             o[1][i+1] = 1;

 53             end++;

 54         }

 55         if(p[2*h][i] == ' ')

 56         {

 57             quer[end] = 2*h;

 58             quec[end] = i;

 59             o[2*h+1][i+1] = 1;

 60             end++;

 61         }

 62     }

 63     end --;

 64     num = 0;

 65     while(str <= end)

 66     {

 67         j = 1;

 68         for(i = str; i <= end; i ++)

 69         {

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

 71             {

 72                 int rr,cc;

 73                 rr = quer[i]+x[k];

 74                 cc = quec[i]+y[k];

 75                 if(rr<=2*h&&rr>=0&&cc<=2*w&&cc>=0)

 76                 {

 77                     if(num != 0)

 78                     {

 79                         if(rr+x[k]<=2*h&&rr+x[k]>=0&&cc+y[k]<=2*w&&cc+y[k]>=0)

 80                         {

 81                             if(p[rr][cc] == ' '&&!o[rr+x[k]][cc+y[k]])

 82                             {

 83                                 o[rr+x[k]][cc+y[k]] = num+1;

 84                                 quer[end+j] = rr+x[k];

 85                                 quec[end+j] = cc+y[k];

 86                                 j ++;

 87                             }

 88                         }

 89                     }

 90                     else

 91                     {

 92                         if(p[rr][cc] == ' '&&!o[rr][cc])

 93                         {

 94                             o[rr][cc] = num+1;

 95                             quer[end+j] = rr;

 96                             quec[end+j] = cc;

 97                             j ++;

 98                         }

 99                     }

100                 }

101             }

102         }

103         str = end+1;

104         end = end+j-1;

105         num ++;

106     }

107     printf("%d\n",num-1);

108     return 0;

109 }

你可能感兴趣的:(USACO)