USACO2.42Overfencing(bfs)

刚开始想写最短路 没写出来 TLE了在最后一组数据上 加了点优化过了 从两个出口出 像各点BFS 取最小中的最大值

View Code
  1 /*

  2     ID: shangca2

  3     LANG: C++

  4     TASK: maze1

  5 */

  6 #include <iostream>

  7 #include<cstdio>

  8 #include<cstring>

  9 #include<algorithm>

 10 #include<stdlib.h>

 11 #include<queue>

 12 using namespace std;

 13 char str[300][210];

 14 int n,m,f[300][210],dis[4][2] = {0,1,0,-1,1,0,-1,0};

 15 struct node

 16 {

 17     int x,y,num;

 18 };

 19 int bfs(int x,int y,int xx,int yy,int o)

 20 {

 21     int i;

 22     queue<node>q;

 23     memset(f,0,sizeof(f));

 24     node t1;

 25     t1.x = x;

 26     t1.y = y;

 27     t1.num = 0;

 28     f[x][y] = 1;

 29     q.push(t1);

 30     while(!q.empty())

 31     {

 32         node t2 = q.front();

 33         if(t2.num>o)

 34         return o;

 35         q.pop();

 36         if(t2.x==xx&&t2.y == yy)

 37         {

 38             int kk = t2.num;

 39             return kk;

 40         }

 41         for(i = 0 ; i < 4 ; i++)

 42         {

 43             int tx = t2.x+dis[i][0];

 44             int ty = t2.y+dis[i][1];

 45             if(str[tx][ty]==' '&&!f[tx][ty]&&(tx>0&&tx<2*m&&ty>0&&ty<2*n))

 46             {

 47                 f[tx][ty] = 1;

 48                 tx = tx+dis[i][0];

 49                 ty = ty+dis[i][1];

 50                 f[tx][ty] = 1;

 51                 t1.num = t2.num+1;

 52                 t1.x = tx;

 53                 t1.y = ty;

 54                 q.push(t1);

 55             }

 56         }

 57     }

 58 }

 59 int main()

 60 {

 61     freopen("maze1.in","r",stdin);

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

 63     int i,j,k,g=0,e[10];

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

 65     for(i = 0; i < 2*m+1 ; i++)

 66     {

 67         getchar();

 68         for(j = 0 ; j < 2*n+1 ; j++)

 69         {

 70             scanf("%c",&str[i][j]);

 71             if(i==0&&str[i][j]==' ')

 72             {

 73                 e[++g] = i+1;

 74                 e[++g] = j;

 75             }

 76             if(i==2*m&&str[i][j]==' ')

 77             {

 78                 e[++g] = i-1;

 79                 e[++g] = j;

 80             }

 81             if(j==0&&str[i][j]==' ')

 82             {

 83                 e[++g] = i;

 84                 e[++g] = j+1;

 85             }

 86             if(j==2*n&&str[i][j]==' ')

 87             {

 88                 e[++g] = i;

 89                 e[++g] = j-1;

 90             }

 91         }

 92     }

 93     int m1=0,m2=0;

 94     for(i = 0 ; i < 2*m+1 ; i++)

 95         for(j = 0 ; j < 2*n+1 ;j++)

 96         {

 97             if(i%2==1&&j%2==1)

 98             {

 99                 int k1 = bfs(e[1],e[2],i,j,100000000);

100                 if(k1<m2)

101                 continue ;

102                 int k2 = bfs(e[3],e[4],i,j,k1);

103                 m1 = min(k2,k1);

104                 m2 = max(m1,m2);

105             }

106         }

107     printf("%d\n",m2+1);

108     return 0;

109 }

 

 

你可能感兴趣的:(USACO)