babadeleida

chl2023 2023-11-25 11:26:06

//AMAZE AMAZE HAZE
#include<bits/stdc++.h> 
using namespace std; 
int n,m,z,minn=INT_MAX;
int momo[1001][1001];
struct hazy{
	int x,y;
};
queue<hazy> misty;
int main(){ 
	cin>>n>>m>>z; 
	memset(momo,-1,sizeof(momo));
	momo[0][0]=0;
	misty.push({0,0});
	while(!misty.empty()){
		int x=misty.front().x,y=misty.front().y;
		if(x+y==z){
			if(momo[x][y]<=minn) minn=momo[x][y];
		}
		misty.pop();
		if(momo[n][y]==-1){
			momo[n][y]=momo[x][y]+1;
			misty.push({n,y});
		} 
		if(momo[0][y]==-1){
			momo[0][y]=momo[x][y]+1;
			misty.push({0,y});
		}
		if(momo[x][m]==-1){
			momo[x][m]=momo[x][y]+1;
			misty.push({x,m});
		}
		if(momo[x][0]==-1){
			momo[x][0]=momo[x][y]+1;
			misty.push({x,0});
		}
		int sum=min(x,m-y);
		if(momo[x-sum][y+sum]==-1){
			momo[x-sum][y+sum]=momo[x][y]+1;
			misty.push({x-sum,y+sum});
		}
		sum=min(n-x,y);
		if(momo[x+sum][y-sum]==-1){
			momo[x+sum][y-sum]=momo[x][y]+1;
			misty.push({x+sum,y-sum});
		}
	}
	if(minn!=INT_MAX)cout<<minn;
	else cout<<-1;
	return 0;
}
 

共 1 条回复

chl2023
#include<bits/stdc++.h>
using namespace std;
string a;
map<string,string>m;
queue<string>q;
void A(string x){
	string xx=x;
	for(int i=0;i<4;i++){
		char x1=x[i];
		x[i]=x[7-i];
		x[7-i]=x1;
	}
	if(m.count(x)==0){
		q.push(x);
		m[x]=m[xx]+'A';
	}
	return;
}
void B(string x){
	string xx=x;
	x[0]=xx[3],x[1]=xx[0],x[2]=xx[1],x[3]=xx[2],x[4]=xx[5],x[5]=xx[6],x[6]=xx[7],x[7]=xx[4];
	if(m.count(x)==0){
		q.push(x);
		m[x]=m[xx]+'B';
	}
	return;
}
void C(string x){
	string xx=x;
	x[1]=xx[6],x[2]=xx[1],x[5]=xx[2],x[6]=xx[5];
	if(m.count(x)==0){
		q.push(x);
		m[x]=m[xx]+'C';
	}
	return;
}
void bfs(){
	q.push("12345678");
	m["12345678"]="";
	while(q.empty()==false){
		A(q.front());
		B(q.front());
		C(q.front());
		if(m.count(a)!=0){
			cout<<m[a].size()<<endl<<m[a];
			return;
		}
		q.pop();
	}
	return;
}
int main(){
	for(int i=0;i<8;i++){
		char a1;
		cin>>a1;
		a+=a1;
		getchar();
	}
	bfs();
	return 0;
}