cross

  1 #include <iostream>
  2 #include <cstdlib>
  3 using namespace std;
  4 
  5 #define SIZE 1000
  6 static int map[10][SIZE][SIZE];
  7 int map2[SIZE][SIZE];
  8 int map1[SIZE][SIZE];
  9 int dx[4]={1,-1,0,0};
 10 int dy[4]={0,0,1,-1};
 11 struct point{
 12     int x;
 13     int y;
 14     int step;
 15 };
 16 void del();
 17 bool nosafe(int nx,int ny);
 18 int run_test(const int map[SIZE][SIZE]);
 19 point snake[1000];
 20 void build_map()
 21 {
 22     for(int c=0;c<10;c++)
 23     {
 24         for(int y=0;y<SIZE;y++)
 25             for(int x=0;x<SIZE;x++)
 26                 map[c][x][y]=0;
 27         for(int x=rand()%10;x<SIZE;x+=2+rand()%8)
 28             for(int sy=rand()%SIZE,ey=rand()%SIZE;sy<ey;)
 29                 map[c][x][sy++]=1;
 30         for(int y=rand()%10;y<SIZE;y+=2+rand()%8)
 31             for(int sx=rand()%SIZE,ex=rand()%SIZE;sx<ex;)
 32                 map[c][sx++][y]=1;
 33     }
 34 }
 35 
 36 int main()
 37 {
 38     
 39     build_map();
 40     for(int count=0;count<10;count++)
 41         cout <<run_test(map[count])<<endl;
 42 }
 43 
 44 
 45 int run_test(const int map[SIZE][SIZE])
 46 {
 47     int front=0;
 48     
 49     int rear=0;
 50     int nx,ny;
 51     int maxlen=0;
 52     for(int i=0;i<SIZE;i++)
 53         for(int j=0;j<SIZE;j++)
 54         {
 55             map1[i][j]=map[i][j];
 56             map2[i][j]=0;
 57         }
 58     del();
 59     for(int i=0;i<SIZE;i++)
 60         for(int j=0;j<SIZE;j++)
 61         {
 62             if(map1[i][j]==1)
 63             {
 64                 front =0;
 65                 rear=0;
 66                 snake[rear].x=i;
 67                 snake[rear].y=j;
 68                 snake[rear++].step=1;
 69                 while(rear>front)
 70                 {
 71                     for(int k=0;k<4;k++)
 72                     {
 73                         nx=snake[front].x+dx[k];
 74                         ny=snake[front].y+dy[k];
 75                         if(nosafe(nx,ny)&&map2[nx][ny]==0)
 76                         {
 77                             map2[nx][ny]=1;
 78                             snake[rear].x=nx;
 79                             snake[rear].y=ny;
 80                             snake[rear++].step=snake[front].step+1;
 81                         }
 82                     }
 83                     front++;
 84                 }
 85                 if(snake[rear-1].step>maxlen)
 86                     maxlen=snake[rear-1].step;
 87             }
 88         }
 89         return maxlen;    
 90 }
 91 
 92 
 93 void del()
 94 {
 95     int nx,ny;
 96     int visit[4];
 97     for(int i=0;i<SIZE;i++)
 98         for(int j=0;j<SIZE;j++)
 99         {
100             if(map1[i][j]==1)
101             {
102                 for(int k=0;k<4;k++)
103                 {
104                     nx=i+dx[k];
105                     ny=j+dy[k];
106                     if(nosafe(nx,ny))
107                         visit[k]=1;
108                     else visit[k]=0;
109                 }
110                 if((visit[0]||visit[1])&&(visit[2]||visit[3]))
111                     map1[i][j]=0;
112             }
113         }
114 }
115 
116 
117 bool nosafe(int nx,int ny)
118 {
119     if(nx>=0&&ny>=0&&nx<SIZE&&ny<SIZE&&map1[nx][ny]==1)
120         return true;
121     return false;
122 }

 

posted on 2017-01-23 11:28  霸王程  阅读(165)  评论(0编辑  收藏  举报