C++: Guangsou basic example: fill in color

Breadth first search example: fill in color

1. Title

Rockwell P1162

Title Description In
the square matrix composed of the number 0, there is a closed circle of any shape. The closed circle is composed of the number 1. When enclosing the circle, it only goes up, down, left, and right. Now it is required to fill in all the spaces in the closed circle as 2. For example: 6×6 square matrix (n=6), the square matrix before and after painting is as follows:
Before filling
After filling
Input format The
first line of each group of test data is an integer (1≤n≤30) , the
next n lines, An n×n square matrix composed of 0 and 1 .
There is only one closed circle in the square matrix, and there is at least one 0 in the circle.
Output format
The complete square matrix of the number 22 has been filled in.

enter

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

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

2. Problem solution

The source code is as follows:

#include <iostream>
using namespace std;
int a[32][32], b[32][32];
int dx[5] = {
    
     0,-1,1,0,0 };
int dy[5] = {
    
     0,0,0,-1,1 };
int n, i, j;
void dfs(int p, int q) {
    
    
	int i;
	if (p<0 || p>n + 1 || q<0 || q>n + 1 || b[p][q] != 0) 
		return;
	b[p][q] = 1;
	for (i = 1; i <= 4; i++) 
		dfs(p + dx[i], q + dy[i]);
}
int main() {
    
    
	cin >> n;
	for (i = 1; i <= n; i++)
		for (j = 1; j <= n; j++) {
    
    
			cin >> a[i][j];
			if (a[i][j] == 0) 
				b[i][j] = 0;
			else b[i][j] = 1;
		}
	dfs(0, 0);
	for (i = 1; i <= n; i++) {
    
    
		for (j = 1; j <= n; j++)
			if (b[i][j] == 0) 
				cout << 2 << ' ';
			else 
				cout << a[i][j] << ' ';
		cout << '\n';
	}
}

Idea guide: It
should be easy to see that this is a basic breadth-first search problem, which is BFS. The essence of the problem is to separate the values ​​inside the wall . As for the wall and the outside, we can just output it according to the original. First, we store the two-dimensional array data in the two-dimensional array a. One thing to note is that you cannot start saving from (0,0). If you start saving from (0,0), it will appear when the search is performed later. For the problem of array out-of-bounds, another array b is defined to represent the state of each element of our 01 picture. If the position corresponding to a is a wall, the corresponding position of b is colored as 1, otherwise, it is colored as 0.

	for (i = 1; i <= n; i++)
		for (j = 1; j <= n; j++) {
    
    
			cin >> a[i][j];
			if (a[i][j] == 0) 
				b[i][j] = 0;
			else b[i][j] = 1;
		}

Then start from (0,0), start traversal search, and return only if it encounters a wall or reaches the boundary of the array.

if (p<0 || p>n + 1 || q<0 || q>n + 1 || b[p][q] != 0) 
		return;

If it does not return, that is, the position corresponding to the value of the array a is 0, that is, it is not a wall and not inside the wall, and then this position is colored, in fact, the position outside the wall is also changed to a wall. This step is very colored Important, if there is no coloring, after entering the next position, when the program traverses up, down, left, and right, there is no sign to indicate whether the previous position of the program has been traversed, and the program will once again enter the position traversed before. That is back to (0,0), which is also the problem my classmate encountered before, and I can't get out of the function.

void dfs(int p, int q) {
    
    
	int i;
	if (p<0 || p>n + 1 || q<0 || q>n + 1 || b[p][q] != 0) 
		return;
	b[p][q] = 1;
	for (i = 1; i <= 4; i++) 
		dfs(p + dx[i], q + dy[i]);
}

Next is the previous step. After the initial position is dyed, we traverse up and down (through two arrays) in four directions. Each time we traverse one direction, it is equivalent to jumping into the bfs function of a position again, recursively , Until all non-wall positions are traversed and colored, and changed to walls.
At this time, in the array b, except for the 0 point position, the other positions are changed to the wall, that is, except for the position inside the wall, the other positions are all 1, then the problem is solved, the output:

for (i = 1; i <= n; i++) {
    
    
		for (j = 1; j <= n; j++)
			if (b[i][j] == 0) 
				cout << 2 << ' ';
			else 
				cout << a[i][j] << ' ';
		cout << '\n';
	}

3. Summary

In general, the question is a comparative-based wide-search question, and the most important thing is to define another array to reproduce the original array, color the array, and distinguish where the value needs to be changed.
For those that do not need to be changed, output as-is according to the a array, and for those that need to be changed, through the identification and judgment of the b array, output the corresponding value required by the question, and it is complete.
The topic itself is not too difficult, I hope it can help students who are just about to learn breadth search, thank you for browsing!

Guess you like

Origin blog.csdn.net/qq_19265749/article/details/103124250