C++ 2. Do you eat ice cream?

Insert picture description hereInsert picture description here
Based on the experience of the first question, I first try to complete the first step, find the minimum number of times, and apply what I have learned!
First attempt:

//冰箱开关
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int l[4][4];
int Min=0x7fffffff;
int check()
{
    
    
	int i,j;
	for(i=0;i<4;i++){
    
    
		for(j=0;j<4;j++){
    
    
			if(l[i][j]=='+'){
    
    
				return -1;
			}
		}
	}
	return 1;
}
void Switch(int m,int n){
    
    
	l[m][n]=!l[m][n];
	for(int i=0;i<4;i++){
    
    
		l[m][i]=!l[m][i];
	}
	for(int j=0;j<4;j++){
    
    
		l[j][n]=!l[j][n];
	}
}
void dfs(int s,int q){
    
    
	int x,y;
	if(s==16){
    
    
		if(check()==1){
    
    
			if(q<Min){
    
    
				Min=q;
			}
		}
		return;
	}
	else{
    
    
		x=s/4;
		y=s%4;
		dfs(s+1,q);
		Switch(x,y);
		dfs(s+1,q+1);
		Switch(x,y);
	}
}
int main()
{
    
    
	char p;
	for(int i=0;i<4;i++){
    
    
		for(int j=0;j<4;j++){
    
    
			cin>>p;
			if(p=='-'){
    
    
				l[i][j]=1;
			}
			else{
    
    
				l[i][j]=0;
			}
		} 
	}
	dfs(0,0);
	if(Min==0x7fffffff){
    
    
		cout<<"Impossible\n"<<endl;
	}
	else{
    
    
		cout<<Min<<endl;
	}
return 0;
}
 

Debugging and analysis:
1. At the beginning, switch() is defined as a flip function, but an error is reported. It is similar to the min situation in the first question. You cannot use the built-in function name to define a function or variable. Here is a look at the switch() function:
C++ switch
2. Variation 1: The check function only returns 1 if all'-' is in the check function
3. Variation 2: Complete the conversion of the ranks according to the meaning of the question
4. Deep search function: also perform an iterative traversal search that I don’t understand very well
5. See here, I seem to suddenly understand why this answer is wrong, ah, I forgot to say that this code can't get the answer, but it makes sense to record the process of exploration. This code can run, but:
Insert picture description here
why is it zero? ? ?
Haha, I was pleasantly surprised that I quickly discovered the problem. It turns out that we have converted all the'-' and non-'-' (this is more precise and rigorous) into 1 and 0. Pay attention to the nested call relationship between functions , So when the check() function is called like this:

if(l[i][j]=='+')

This sentence needs to be truly understood: it means that as long as all the elements in the 16 grids are not'+', then 1 can be returned! Therefore, the negative sentence must be used to return -1! ! change into:

if(l[i][j]!=1)

This solved the problem! !
Yay:
Insert picture description here

The next step is to solve the path problem, let me think about it. . . . . .
I suddenly remembered something! ! Depth-first search can only find the minimum number of times, but it is difficult to find the optimal solution. This has brought great inconvenience to our next operation. Let me learn more and prepare some goods! !
Knowledge point one structure
C++ structure
knowledge point two exclamation mark!
In one sentence: If a is True, then! a is False, and vice versa.
Let us analyze the final code:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct node{
    
    
	int x,y;	
}coor[16];
int main(){
    
    
	char l[4][4];
	int n[4][4]={
    
    0};
	for(int i=0;i<4;i++){
    
    
		for(int j=0;j<4;j++){
    
    
			cin>>l[i][j];
		}
	}
	for(int i=0;i<4;i++){
    
    
		for(int j=0;j<4;j++){
    
    
			if(l[i][j]=='-'){
    
    
				continue;
			}
			n[i][j]=!n[i][j];
			for(int m=0;m<4;m++){
    
    
				n[i][m]=!n[i][m];
				n[m][j]=!n[m][j];
			}
		} 
	}
	int MIN=0;
	for(int i=0;i<4;i++){
    
    
		for(int j=0;j<4;j++){
    
    
			if(n[i][j]){
    
    
				coor[MIN].x=i+1;
				coor[MIN].y=j+1;
				MIN++;
			}
		}
	}
	cout<<MIN<<endl;
	for(int i=0;i<MIN;i++){
    
    
		cout<<coor[i].x<<" "<<coor[i].y<<endl;
	}
}

Detailed analysis:
1. First, create a 16-bit array coor[] (short for English'coordinates'), let us first understand its inner meaning, here 16-bit means that there are 16 bits for each structure variable , Instead of their sum
2.l [i ][ j] Used to store the input plus and minus signs (the on/off state of the refrigerator)
3. (Important way of thinking, as natural as breathing) The idea of ​​this code is only Change the switch in the closed state, so a conditional judgment is given next to filter:

if(l[i][j]=='-'){
    
    
	continue;
}

In the process of traversal, if you encounter an open switch, you will skip this loop and enter the next loop (continue statement). If you encounter a closed switch, it will change the state of itself and the switches in the same column as it (the effect of !), Under this rule, all 16 switches are traversed
. 4. Next, find the minimum number of times, the code

if(n[i][j])

If it is judged to be true (non-zero), it means that the switch has been changed state. At the same time, MIN just recorded the number of changes, and also recorded the coordinates (plus one because it is consistent with the mathematical coordinates).
So I have some questions. , Analyze first: all positions at the beginning are marked as 0, assuming we finally get the answer (I still don’t understand how to determine that the traversal is complete and fully opened at the end of the traversal?), then all the positions that are 1 must be closed at the beginning That is to say,
during the traversal process, those switches that are in the open state in the same row must be flipped an even number of times together, while the switches that are in the closed state must be flipped an odd number of times after being flipped together. Now I feel that this is quite difficult to achieve, maybe I still I do not understand the code, let's go over the code it
//
after there has been some gains, we take note of the test sample and its output, on paper generally demonstrate what can be found, are taking the path traversed precisely all over again The position of the close switch! As for the truth, I can only say that for the time being, I can only understand that it will be unspeakable. Maybe it is a certain aspect of professional knowledge. Let me go and explore it slowly!
//
I tried to use the above method to turn the chess pieces again, but it didn't work as I wanted:

//翻棋子
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
    
    
	char p[4][4];
	int q[4][4]={
    
    1};
	for(int i=0;i<4;i++){
    
    
		for(int j=0;j<4;j++){
    
    
			cin>>p[i][j];
		}
	}
	for(int i=0;i<4;i++){
    
    
		for(int j=0;j<4;j++){
    
    
			if(p[i][j]=p[0][0]){
    
    
				continue;
			}
			else{
    
    
				q[i][j]=!q[i][j];
				if(i-1>=0){
    
    
					q[i-1][j]=!q[i-1][j];
				}
				if(j-1>=0){
    
    
					q[i][j-1]=!q[i][j-1];
				}
				if(i+1<4){
    
    
					q[i+1][j]=!q[i+1][j];
				}
				if(j+1<4){
    
    
					q[i][j+1]=!q[i][j+1];
				}
			}
		}
	}
	int MIN1=0,MIN2=0;
	for(int i=0;i<4;i++){
    
    
		for(int j=0;j<4;j++){
    
    
			if(q[i][j]){
    
    
				MIN1+=1; 
			}
			else{
    
    
				MIN2+=1;
			} 
		}
	}
	cout<<MIN1<<endl;
	cout<<MIN2<<endl;
	return 0;
}

Debugging report:
1. Practice typing more by yourself, don't use any copying methods, so that you can make progress!
2. I wrote it myself, but I forgot to add using namespace std, causing cin and cout to not be recognized!
3. Written by myself, mistyped Chinese characters
Output:

bwwb
bbwb
bwwb
bwww
1
15

Why output 1? ? ? Let's output the final q array:

1000
0000
0000
0000

It became like this. .
Judging from my naive worldview, it should be difficult to apply the methods of these two questions to each other. What was it in fact at that time? Wait until you have more knowledge of yourself before answering!

The harder, the more fortunate!
Come on duck with ZDZ!
The hard work is still strong,
let the east and the west wind!

Guess you like

Origin blog.csdn.net/interestingddd/article/details/113529212