Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<algorithm>
- using namespace std;
- int board[1001][1001];
- void getBoard(int n, int m)
- {
- char c = getchar();
- for (int i=0;i<n;i++)
- {
- c = getchar();
- for (int j=0; j<m;j++)
- {
- if (c=='1')
- board[i][j]=1;
- else board[i][j]=0;
- c = getchar();
- }
- }
- }
- void findSea(int n, int m, int &x, int &y)
- {
- for (int i=0;i<m;i++)
- {
- if (board[0][i]==0)
- {
- x=0; y=i;
- return;
- }
- if (board[n-1][i]==0)
- {
- x=n-1; y=i;
- return;
- }
- }
- for (int i=0;i<n;i++)
- {
- if (board[i][0]==0)
- {
- x=i; y=0;
- return;
- }
- if (board[i][m-1]==0)
- {
- x=i; y=m-1;
- return;
- }
- }
- x=-1; y=-1;
- }
- typedef struct pos
- {
- int x,y;
- }pos;
- void BFS(int n, int m, int x, int y)
- {
- pos *queue = new pos[1000000];
- queue[0].x=x; queue[0].y=y;
- board[x][y]=2;
- int start=0, end=1;
- while (start < end)
- {
- int x1 = queue[start].x, y1= queue[start].y;
- if (x1-1>=0 && board[x1-1][y1]==0)
- {
- queue[end].x=x1-1;
- queue[end].y=y1;
- end++;
- board[x1-1][y1]=2;
- }
- if (x1+1<n && board[x1+1][y1]==0)
- {
- queue[end].x=x1+1;
- queue[end].y=y1;
- end++;
- board[x1+1][y1]=2;
- }
- if (y1-1>=0 && board[x1][y1-1]==0)
- {
- queue[end].x=x1;
- queue[end].y=y1-1;
- end++;
- board[x1][y1-1]=2;
- }
- if (y1+1<m && board[x1][y1+1]==0)
- {
- queue[end].x=x1;
- queue[end].y=y1+1;
- end++;
- board[x1][y1+1]=2;
- }
- start++;
- }
- }
- int find(int n, int m)
- {
- int count=0;
- for (int i=0;i<n;i++)
- for (int j=0; j<m;j++)
- {
- if (board[i][j]==2)
- {
- int x, y, c2=0;
- x=i-1; y=j;
- if (x>=0 && board[x][y]==1)
- {
- c2++;
- }
- x=i+1; y=j;
- if (x<n && board[x][y]==1)
- {
- c2++;
- }
- x=i; y=j-1;
- if (y>=0 && board[x][y]==1)
- {
- c2++;
- }
- x=i; y=j+1;
- if (y<m && board[x][y]==1)
- {
- c2++;
- }
- count += c2;
- }
- }
- for (int i=0;i<m;i++)
- {
- if (board[0][i]==1)
- count++;
- if (board[n-1][i]==1)
- count++;
- }
- for (int i=0;i<n;i++)
- {
- if (board[i][0]==1)
- count++;
- if (board[i][m-1]==1)
- count++;
- }
- return count;
- }
- int main()
- {
- int n,m;
- cin >> n >> m;
- getBoard(n, m);
- int x,y;
- findSea(n,m,x,y);
- while (x!=-1 && y!=-1)
- {
- BFS(n,m,x,y);
- findSea(n,m,x,y);
- }
- cout << find(n, m);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment