fuliver123

Coast Length

Aug 4th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include<iostream>
  2. #include<algorithm>
  3.  
  4. using namespace std;
  5.  
  6. int board[1001][1001];
  7.  
  8. void getBoard(int n, int m)
  9. {
  10.     char c = getchar();
  11.     for (int i=0;i<n;i++)
  12.     {
  13.         c = getchar();
  14.         for (int j=0; j<m;j++)
  15.         {
  16.             if (c=='1')
  17.                 board[i][j]=1;
  18.             else board[i][j]=0;
  19.             c = getchar();
  20.         }  
  21.     }
  22. }
  23.  
  24. void findSea(int n, int m, int &x, int &y)
  25. {
  26.     for (int i=0;i<m;i++)
  27.     {
  28.         if (board[0][i]==0)
  29.         {
  30.             x=0; y=i;
  31.             return;
  32.         }
  33.         if (board[n-1][i]==0)
  34.         {
  35.             x=n-1; y=i;
  36.             return;
  37.         }
  38.     }
  39.     for (int i=0;i<n;i++)
  40.     {
  41.         if (board[i][0]==0)
  42.         {
  43.             x=i; y=0;
  44.             return;
  45.         }
  46.         if (board[i][m-1]==0)
  47.         {
  48.             x=i; y=m-1;
  49.             return;
  50.         }
  51.     }
  52.     x=-1; y=-1;
  53. }
  54.  
  55. typedef struct pos
  56. {
  57.     int x,y;
  58. }pos;
  59.  
  60. void BFS(int n, int m, int x, int y)
  61. {
  62.     pos *queue = new pos[1000000];
  63.     queue[0].x=x; queue[0].y=y;
  64.     board[x][y]=2;
  65.     int start=0, end=1;
  66.     while (start < end)
  67.     {
  68.         int x1 = queue[start].x, y1= queue[start].y;
  69.         if (x1-1>=0 && board[x1-1][y1]==0)
  70.         {
  71.             queue[end].x=x1-1;
  72.             queue[end].y=y1;
  73.             end++;
  74.             board[x1-1][y1]=2;
  75.         }
  76.         if (x1+1<n && board[x1+1][y1]==0)
  77.         {
  78.             queue[end].x=x1+1;
  79.             queue[end].y=y1;
  80.             end++;
  81.             board[x1+1][y1]=2;
  82.         }
  83.         if (y1-1>=0 && board[x1][y1-1]==0)
  84.         {
  85.             queue[end].x=x1;
  86.             queue[end].y=y1-1;
  87.             end++;
  88.             board[x1][y1-1]=2;
  89.         }
  90.         if (y1+1<m && board[x1][y1+1]==0)
  91.         {
  92.             queue[end].x=x1;
  93.             queue[end].y=y1+1;
  94.             end++;
  95.             board[x1][y1+1]=2;
  96.         }
  97.         start++;
  98.     }
  99. }
  100.  
  101. int find(int n, int m)
  102. {
  103.     int count=0;
  104.     for (int i=0;i<n;i++)
  105.         for (int j=0; j<m;j++)
  106.         {
  107.             if (board[i][j]==2)
  108.             {
  109.                 int x, y, c2=0;
  110.                 x=i-1; y=j;
  111.                 if (x>=0 && board[x][y]==1)
  112.                 {
  113.                     c2++;
  114.                 }
  115.                 x=i+1; y=j;
  116.                 if (x<n && board[x][y]==1)
  117.                 {
  118.                     c2++;
  119.                 }
  120.                 x=i; y=j-1;
  121.                 if (y>=0 && board[x][y]==1)
  122.                 {
  123.                     c2++;
  124.                 }
  125.                 x=i; y=j+1;
  126.                 if (y<m && board[x][y]==1)
  127.                 {
  128.                     c2++;
  129.                 }
  130.                 count += c2;
  131.             }
  132.         }
  133.     for (int i=0;i<m;i++)
  134.     {
  135.         if (board[0][i]==1)
  136.             count++;
  137.         if (board[n-1][i]==1)
  138.             count++;
  139.     }
  140.     for (int i=0;i<n;i++)
  141.     {
  142.         if (board[i][0]==1)
  143.             count++;
  144.         if (board[i][m-1]==1)
  145.             count++;
  146.     }
  147.     return count;
  148. }
  149.  
  150. int main()
  151. {
  152.     int n,m;
  153.     cin >> n >> m;
  154.     getBoard(n, m);
  155.     int x,y;
  156.     findSea(n,m,x,y);
  157.     while (x!=-1 && y!=-1)
  158.     {
  159.         BFS(n,m,x,y);
  160.         findSea(n,m,x,y);
  161.     }
  162.     cout << find(n, m);
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment