Flip a coin (wide search, regular) Blue Bridge Cup Provincial

Resource constraints

Time limit: 1.0s Memory limit: 256.0MB

Problem Description

Xiao Ming is playing a "coin flip" game.

There are several coins arranged in a row on the table. We use * for heads and o for tails (lowercase, not zero).

For example, possible scenarios are:**oo***oooo

If you flip the two coins on the left at the same time, it becomes:oooo***oooo

Now Xiaoming's question is: If the initial state and the target state to be reached are known, and only two adjacent coins can be flipped at the same time at a time, what is the minimum number of flips for a specific situation?

We agreed that flipping two adjacent coins is called a one-step operation, then requires:

input format

Two lines of equal-length strings represent the initial state and the target state to be achieved, respectively. length of each line < 1000

output format

An integer representing the minimum number of operation steps.

Sample input 1

**********
o****o****

Sample output 1

5

Sample input 2

*o**o***o***
*o***o**o***

Sample output 2

1

I simulated it by hand, and found that if the flip is the minimum number of times, it only needs to flip from the first different position, flip to the next different position and it will be successful [第一个不同位置,第二个不同位置],[第三个不同位置,第四个不同位置]..... , because reversing is also a redundant operation, and multiple such intervals add the number of flips of each interval.

The process of flipping does not need to be simulated, and the law can be found. The number of flips of an interval = right endpoint - left endpoint

#include<cstdio>
#include<iostream>
using namespace std;
string s1,s2;
int ans=0;
int main(){
    
    
	cin>>s1>>s2;
	int len = s1.length();
	int strat=-1;
	for(int i=0;i<len;i++){
    
    
		if(s1[i]!=s2[i]){
    
    //找到不同的位置 
			if(strat==-1){
    
    //区间起始点 
				strat=i;
				//cout<<i<<endl;
			}else{
    
    //区间结束点 
				ans+=(i-strat);//加和反转次数 
				strat=-1;//恢复准备下一个区间的开始 
			}
		}
		
	}
	cout<<ans<<endl;
	return 0;
}

You can also use wide search to solve it, but pay attention to record the status of each search to prevent redundant searches

#include<iostream>
#include<cstdio>
#include<queue>
#include<set>
using namespace std;
string s1,s2;
struct node{
    
    
	string s;
	int step;
	node(string ss,int pp){
    
    
		this->s=ss;
		this->step=pp;
	}
};
queue<node> que;
set<string> se;//记录出现过的状态 
int main(){
    
    
	cin>>s1>>s2;
	que.push(node(s1,0));
	se.insert(s1);
	while(!que.empty()){
    
    
		string str = que.front().s;
		if(str==s2){
    
    //变到了 
			cout<<que.front().step;
			return 0;
		}
		for(int i=0;i<s1.length()-1;++i){
    
    
				string stro(str);
				if(stro[i]=='*')stro[i]='o';
				else stro[i]='*';
				if(stro[i+1]=='*')stro[i+1]='o';
				else stro[i+1]='*';
				if(!se.count(stro)){
    
    //只搜索没出现过的状态 
					que.push(node(stro,que.front().step+1));
					se.insert(stro);
				} 
		}
		que.pop();
	}
	return 0;
} 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324934000&siteId=291194637