https://www.luogu.org/problem/P1162
Digital 0 matrix consisting of 0, an arbitrary shape with a closed loop, a digital closed loop 1 is constituted only when the enclosure onto the left and right lower . 4 in four directions. Now requires all the spaces are filled into the closed circle 2 2 For example: 6 \ 6 Times 6 × matrix 6 ( n-6 = n- = 6), and the square before coloring the coloring follows:
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 0 0 1
1 1 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 2 2 1
1 1 2 2 2 1
1 2 2 2 2 1
1 1 1 1 1 1
Input Format
Each test line of an integer n-(. 1 \ n-Le \ Le 30) n- ( . 1 ≤ n- ≤ . 3 0 )
Next, n- n-row by 0 0 and . 1 1s of n-\ Times n- n- × n-square matrix.
Only a closed circle within a square, a circle at least 0 0
// thank U drinks Huang pointed out that this question is not the same data and data formats. Modified (input format)
Output Format
We have filled digital 2 complete phalanx 2.
Sample input and output
6 0 0 0 0 0 0 0 0 1 1 1 1 0 1 1 0 0 1 1 1 0 0 0 1 1 0 0 0 0 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 1 1 1 1 0 1 1 2 2 1 1 1 2 2 2 1 1 2 2 2 2 1 1 1 1 1 1 1
Description / Tips
1 \ n \ le 30 1 ≤ n ≤ 3 0
Solution: seeking direct circle 0 poor demand, we can mark out of the loop 0, 0 mark coming out of the loop completely, the rest is circle 0;
Starting from 0 for each vertical and horizontal boundary
AC Code:
#include<bits/stdc++.h> using namespace std; int arr[33][33]; struct stu{ int x,y; }; int n; int d[4][2]={1,0,0,1,-1,0,0,-1}; void bfs(int x,int y){ queue<stu >que; arr[x][y]=3; que.push({x,y}); while(que.size()){ stu s=que.front(); que.pop(); for(int i=0;i<4;i++){ int dx=s.x+d[i][0]; int dy=s.y+d[i][1]; if(dx>=1&&dy>=1&&dx<=n&&dy<=n&&arr[dx][dy]==0){ arr[dx][dy]=3; que.push({dx,dy}); } } } } int main(){ cin>>n; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&arr[i][j]); for(int i=1;i<=n;i++){ if(arr[i][1]==0) bfs(i,1); if(arr[i][n]==0) bfs(i,n); if(arr[1][i]==0) bfs(1,i); if(arr[n][i]==0) bfs(n,i); } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++) if(arr[i][j]==3) cout<<0<<" "; else if(arr[i][j]==0) cout<<2<<" "; else cout<<1<<" "; cout<<endl; } return 0; }
I want the same type of subject: https://www.luogu.org/problem/P1506