Luo Gu P1162 Tiantu color solution to a problem

Title Description

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

Input # 1
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
Output # 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


 

answer

This question has a simple algorithm is to input all the data are to fill 0 to 2, and then from four sides to conduct internal BFS, if the search will change it to 0 to 2, and continue the search, if the search to 1 or 0 to stop the search.

  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <math.h>
  4 #include <algorithm>
  5 #include <string.h>
  6 
  7 using namespace std;
  8 
  9 const int MAXN = 105;
 10 int n, map[MAXN][MAXN], vis[MAXN][MAXN];
 11 int pos[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
 12 
 13 struct Node
 14 {
 15     int x, y;
 16 };
 17 
 18 Node q[MAXN];
 19 int front, rear;
 20 int a, b;
 21 
 22 void bfs()
 23 {
 24     Node now;
 25     now.x = a;
 26     now.y = b;
 27     if(map[a][b] != 2)
 28     {
 29         return;
 30     }
 31     front = rear = 0;
 32     q[rear] = now;
 33     rear++; 
 34     while(front < rear)
 35     {
 36         now = q[front++]; 
 37         if(map[now.x][now.y] == 2)
 38         {
 39             map[now.x][now.y] = 0;
 40         }
 41         if(now.x == 7)
 42         {
 43             now.x = 7;
 44         }
 45         for(int i = 0; i < 4; i++) 
 46         {
 47             int nx = now.x + pos[i][0]; 
 48             int ny = now.y + pos[i][1]; 
 49             if(nx <= n && nx > 0 && ny <= n && ny > 0  
 50                 && vis[nx][ny] == false 
 51                 && map[nx][ny] == 2) 
 52             {
 53                 map[nx][ny] = 0;
 54                 vis[nx][ny] = true;
 55                 q[rear].x = nx;
 56                 q[rear].y = ny;
 57                 rear++;
 58             }
 59         } 
 60     }
 61 }
 62 
 63 
 64 int main()
 65 {
 66     cin >> n;
 67     for(int i = 1; i <= n; i++)
 68     {
 69         for(int j = 1; j <= n; j++)
 70         {
 71             cin >> map[i][j];
 72             if(map[i][j] == 0)
 73             {
 74                 map[i][j] = 2;
 75             }
 76         }
 77     }
 78     for(int i = 1; i <= n; i++)
 79     {
 80         a = 1;
 81         b = i;
 82         bfs();
 83     }
 84     for(int i = 1; i <= n; i++)
 85     {
 86         a = n;
 87         b = i;
 88         bfs();
 89     }
 90     for(int i = 1; i <= n; i++)
 91     {
 92         a = i;
 93         b = 1;
 94         bfs();
 95     }
 96     for(int i = 1; i <= n; i++)
 97     {
 98         a = i;
 99         b = n;
100         bfs();
101     }
102     for(int i = 1; i <= n; i++)
103     {
104         for(int j = 1; j <= n; j++)
105         {
106             cout << map[i][j] << " ";
107         }
108         cout << endl;
109     }
110     return 0;
111 }

The BFS is not difficult to write, but then made a small mistake, resulting in a sample are 2/3/4 WA, especially the first two consistent results and sample output from this unit standard answer, but total submission It is to say a position should be 0, 2 output. Search for a long time, and finally found the pos [4] [2] written pos [2] [4]. Array definition is wrong, resulting in movement of the traversing position error, and the memory corresponding to different native tester data and so on in the machine is passed, but not through the tester.

Guess you like

Origin www.cnblogs.com/zealsoft/p/11404420.html