實驗三

電子老鼠闖迷宮

#include<iostream>
#include<queue>
using namespace std;
char map[15][15];
int sx,sy,ex,ey;
int INF=500;
int d[15][15];
int dx[4]={0,1,0,-1};
int dy[4]={-1,0,1,0};

struct point{
	int x;
	int y;
	point(int x,int y):x(x),y(y){}
};

queue<point> q;

int bfs();

int main(){
	cin>>sx>>sy>>ex>>ey;
	for(int i=1;i<=12;i++){
		for(int j=1;j<=12;j++){
			cin>>map[i][j];
			d[i][j]=INF;
		}
	}
	
	d[sx][sy]=0;
	q.push(point(sx,sy));
	
	cout<<bfs()<<endl;

	return 0;
} 

int bfs(){
	while(!q.empty()){
		point p=q.front();
		q.pop();
		int nx=p.x,ny=p.y;
		if(nx==ex&&ny==ey)break;
		
		for(int i=0;i<4;i++){
			int tx=nx+dx[i],ty=ny+dy[i];
			if(tx>0&&tx<=12
			&&ty>0&&ty<=12
			&&map[tx][ty]!='X'
			&&d[tx][ty]==500){
				q.push(point(tx,ty));
				d[tx][ty]=d[nx][ny]+1;
			}
		}
	}
	return d[ex][ey];
}

跳馬

#include<iostream>
#include<queue>
#include<cmath>
#include<cstring>
using namespace std;

int dx[8]={1,2,2,1,-1,-2,-2,-1};
int dy[8]={-2,-1,1,2,2,1,-1,-2};
int derta=3;
int n;

int sx,sy,ex,ey;
int bushu[210][210];//步數很大(0x3f)的話,就是還沒有到達過 

struct point{
	int x;
	int y;
	int step;
	point(int x=0,int y=0,int step=0):x(x),y(y),step(step){}

	/*bool operator < (const point &b) {
		int dis=abs(x-ex)+abs(y-ey)+derta*step;
		int disb=abs(b.x-ex)+abs(b.y-ey)+derta*b.step;
		return dis<disb;
	}*/

};

bool operator<(point a,point b){
	int disa=abs(a.x-ex)+abs(a.y-ey)+derta*a.step;
	int disb=abs(b.x-ex)+abs(b.y-ey)+derta*b.step;
	return disa>disb; 	
}

priority_queue<point> q;

int bfs();

int main(){
	cin>>n;
	for(int i=0;i<n;i++){
		memset(bushu,0x3f,sizeof(bushu));
		while(!q.empty())q.pop();
		cin>>sx>>sy>>ex>>ey;
		q.push(point(sx,sy,0));
		bushu[sx][sy]=0; 
		cout<<bfs()<<endl;
	}
	return 0;
}

int bfs(){
	while(!q.empty()){
		point p=q.top();
		q.pop();
		int nx=p.x,ny=p.y,nstep=p.step;
		if(nx==ex&&ny==ey)break;
		for(int i=0;i<8;i++){
			int tx=nx+dx[i],ty=ny+dy[i],tstep=nstep+1;
			if(tx>0&&tx<=200
			&&ty>0&&ty<=200
			&&bushu[tx][ty]==0x3f3f3f3f){
				q.push(point(tx,ty,tstep));
				bushu[tx][ty]=tstep;
			}
		}
	}
	return bushu[ex][ey];
}

獨輪車

#include<iostream>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
char Map[25][25];
int bushu[25][25][10][10];//x座標,y座標,顏色,方向 

int sx,sy,sc,sd;char ch_sc,ch_sd;
int ex,ey,ec;char ch_ec; 

struct state{
	int x;
	int y;
	int col;
	int dir;
	state(int x,int y,int col,int dir):x(x),y(y),col(col),dir(dir){}
};

queue<state> q;

int bfs();

int main(){
	map<char,int> mc,md;
	//顏色
	mc['R']=0;mc['Y']=1;mc['B']=2;mc['W']=3;mc['G']=4; 
	//方向 
	md['E']=0;md['S']=1;md['W']=2;md['N']=3;
	
	memset(bushu,0x3f,sizeof(bushu));
	cin>>sx>>sy>>ch_sc>>ch_sd;
	cin>>ex>>ey>>ch_ec;
	for(int i=1;i<=20;i++){
		for(int j=1;j<=20;j++){
			cin>>Map[i][j]; 
		}
	}
	//將字符信息轉換爲數字信息表示顏色和方向
	sc=mc[ch_sc];
	sd=md[ch_sd];
	ec=mc[ch_ec];
	
	q.push(state(sx,sy,sc,sd));
	bushu[sx][sy][sc][sd]=0;
	cout<<bfs()<<endl;
	return 0;
}

int bfs(){
	int ans=0x3f3f3f3f;//0x3f3f3f3f表示無解 
	while(!q.empty()){
		state s=q.front();
		q.pop();
		int nx=s.x,ny=s.y,nc=s.col,nd=s.dir; 
		if(nx==ex&&ny==ey&&nc==ec){
			ans=bushu[nx][ny][nc][nd];
			break;
		}
		int tx,ty,tc,td;
		//一共有3種變化:向前走,向左轉一下,向右轉一下 
		//向前走
		tx=nx+dx[nd];
		ty=ny+dy[nd];
		tc=(nc+1)%5;
		td=nd;
		if(tx>=1&&tx<=20
		&&ty>=1&&ty<=20
		&&Map[tx][ty]!='X'
		&&bushu[tx][ty][tc][td]==0x3f3f3f3f){
			q.push(state(tx,ty,tc,td));
			bushu[tx][ty][tc][td]=bushu[nx][ny][nc][nd]+1;
		} 
		//左轉
		tx=nx;
		ty=ny;
		tc=nc;
		td=(nd+1)%4;
		if(bushu[tx][ty][tc][td]==0x3f3f3f3f){
			q.push(state(tx,ty,tc,td));
			bushu[tx][ty][tc][td]=bushu[nx][ny][nc][nd]+1;
		} 
		//右轉 
		tx=nx;
		ty=ny;
		tc=nc;
		td=(nd+3)%4;
		if(bushu[tx][ty][tc][td]==0x3f3f3f3f){
			q.push(state(tx,ty,tc,td));
			bushu[tx][ty][tc][td]=bushu[nx][ny][nc][nd]+1;
		} 
	}
	return ans;
}

 六數碼問題

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int visited[7][7][7][7][7][7];//爲0則沒有訪問過 
int s[6],e[6]={1,2,3,4,5,6};

struct state{
	int ar[6];
	state(int a[6]){
		for(int i=0;i<6;i++){
			ar[i]=a[i];
		}
	}
}; 

queue<state> q;

void swap_ar_a(int a[]){
	int t=a[0];
	a[0]=a[3];
	a[3]=a[4];
	a[4]=a[1];
	a[1]=t;
}
void swap_ar_b(int a[]){
	int t=a[1];
	a[1]=a[4];
	a[4]=a[5];
	a[5]=a[2];
	a[2]=t;
} 
bool bfs();

int main(){
	while(cin>>s[0]){
		for(int i=1;i<6;i++)cin>>s[i];
		//先初始化
		memset(visited,0,sizeof(visited));
		while(!q.empty())q.pop(); 
		 
		q.push(state(s));
		visited[s[0]][s[1]][s[2]][s[3]][s[4]][s[5]]=1;
		if(bfs()){
			cout<<"Yes"<<endl;
		}
		else{
			cout<<"No"<<endl;
		}
	}
	return 0;
}

bool bfs(){
	while(!q.empty()){
		state s=q.front();
		q.pop();
		int ns[6];
		int flag=1;//判斷是否到達目標狀態 
		for(int i=0;i<6;i++){
			ns[i]=s.ar[i];
			if(ns[i]!=e[i])flag=0; 
		}
		if(flag)return true;
		//考察兩種變換
		//a變換
		swap_ar_a(ns);
		if(visited[ns[0]][ns[1]][ns[2]][ns[3]][ns[4]][ns[5]]==0){
			q.push(state(ns));
			visited[ns[0]][ns[1]][ns[2]][ns[3]][ns[4]][ns[5]]=1;
		}
		swap_ar_a(ns);swap_ar_a(ns);swap_ar_a(ns);
		//b變換 
		swap_ar_b(ns);
		if(visited[ns[0]][ns[1]][ns[2]][ns[3]][ns[4]][ns[5]]==0){
			q.push(state(ns));
			visited[ns[0]][ns[1]][ns[2]][ns[3]][ns[4]][ns[5]]=1;
		}
		swap_ar_b(ns);swap_ar_b(ns);swap_ar_b(ns);
	}
	return false;
}

加1乘2平方

#include<iostream>
#include<queue>
using namespace std;
int s,e;
int visited[10010];

struct state{
	int num;
	int step;
	state(int num,int step):num(num),step(step){} 
};

queue<state> q;

int bfs();

int main(){
	cin>>s>>e;
	visited[s]=1;
	q.push(state(s,0));
	cout<<bfs()<<endl;
	return 0;
}

int bfs(){
	while(!q.empty()){
		state ns=q.front();
		int n=ns.num,nstep=ns.step;
		q.pop();
		if(n==e)return nstep;
		if(n+1<=e&&visited[n+1]==0){
			visited[n+1]=1;
			q.push(state(n+1,nstep+1));
		}
		if(n*2<=e&&visited[n*2]==0){
			visited[n*2]=1;
			q.push(state(n*2,nstep+1));
		}
		if(n*n<=e&&visited[n*n]==0){
			visited[n*n]=1;
			q.push(state(n*n,nstep+1));
		}
	}
} 

找倍數

#include<iostream>
#include<string> 
#include<queue>
using namespace std;
string ans="1";
int n;

queue<string> q;

bool is_ok(string s,int n);
string bfs();

int main(){
	cin>>n;
	while(n!=0){
		ans="1";
		while(!q.empty())q.pop();
		q.push(ans);
		cout<<bfs()<<endl;
		cin>>n;
	}
	return 0;
}

string bfs(){
	while(!q.empty()){
		string s=q.front();
		//cout<<s<<endl;
		q.pop(); 
		if(!(n%2==0&&s[s.size()-1]=='1')
		&&is_ok(s,n))return s;
		else if(is_ok(s,n))return s; 
		string s1=s+'0';
		q.push(s1);
		string s2=s+'1';
		q.push(s2);
	}
}

bool is_ok(string s,int n){
	int sum=0;
	int size=s.size();
	for(int i=0;i<size;i++){
		sum*=10;
		sum+=s[i]-'0';
		sum%=n;
	}
	if(sum==0)return true;
	else return false; 
}

木乃伊迷宮(待完成)

#include<iostream>
#include<queue>
using namespace std;
int n;
int dx,dy,dtype;
int msx,msy;//木乃伊
int rsx,rsy;//人 
int ex,ey;//出口
int visited[6][6][6][6]; 
 
struct point{
	int left;
	int down;
	int right;
	int up;
	point(int a=0,int b=0,int c=0,int d=0):left(a),down(b),right(c),up(d){}
};

point map[6][6];

struct state{
	int mx,my;
	int rx,ry;
	state(int a,int b,int c,int d):mx(a),my(b),rx(c),ry(d){}
};

queue<state> q;

bool bfs();

int main(){
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>dx>>dy>>dtype;
		if(dtype==0){
			map[dx][dy].down=1;
			if(dx+1<6)map[dx+1][dy].up=1;
		}
		else if(dtype==1){
			map[dx][dy].right=1;
			if(dx+1<6)map[dx][dy+1].left=1;
		}
	}
	cin>>msx>>msy>>rsx>>rsy>>ex>>ey;
	q.push(state(msx,msy,rsx,rsy));
	visited[msx][msy][rsx][rsy]=1;
	if(bfs()){
		cout<<"Yes"<<endl;
	}
	else{
		cout<<"No"<<endl;
	}
	return 0;
}

bool bfs(){
	while(!q.empty()){
		state s=q.front();
		int mnx=s.mx,mny=s.my,rnx=s.rx,rny=s.ry;
		if(rnx==ex&&rny==ey)return true;
		//木乃伊的第一步和第二步
		//人的一步 
	}
	return false;
}

polygon

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int n,m;
double nxy[1010];
double ans=0; 
int main(){
	cin>>n>>m;
	//一共有n+m個座標
	//求出n個點每一個的座標
	for(int i=1;i<=n;i++){
		nxy[i]=(n+m)/double(n)*i;
	}
	//統計所有座標到最近位置的距離
	for(int i=1;i<=n;i++){
		ans+=min(  ceil(nxy[i])-nxy[i] , nxy[i]-floor(nxy[i])  );
	}
	//換算成標準距離,輸出 
	cout<<fixed<<setprecision(4)<<ans*10000.0/(n+m)<<endl;
	return 0;
}

推箱子

#include<iostream>
#include<queue>
#include<cmath>
#include<string>
using namespace std;
int map[20][20];
string s[11];
int ex,ey;
int rsx,rsy;
int bsx,bsy;
int visited[11][11][11][11];

struct state{
	int rx,ry;
	int bx,by;
	int step;
	state(int a,int b,int c,int d,int e):rx(a),ry(b),bx(c),by(d),step(e){}
};

queue<state> q;

int bfs();

int main(){
	for(int i=0;i<10;i++)cin>>s[i];
	for(int i=1;i<=10;i++){
		for(int j=1;j<=10;j++){
			map[i][j]=(s[i-1][j-1]-'0');
			if(map[i][j]==2){
				bsx=i;
				bsy=j;
			}
			else if(map[i][j]==3){
				ex=i;
				ey=j;
			}
			else if(map[i][j]==4){
				rsx=i;
				rsy=j;
			}
		}
	}
	q.push(state(rsx,rsy,bsx,bsy,0));
	visited[rsx][rsy][bsx][bsy]=1;
	cout<<bfs()<<endl;
	return 0;
}

int bfs(){
	while(!q.empty()){
		state s=q.front();
		int rnx=s.rx,rny=s.ry,bnx=s.bx,bny=s.by,nstep=s.step;
		//cout<<rnx<<' '<<rny<<' '<<bnx<<' '<<bny<<' '<<nstep<<endl; 
		q.pop();
		if(bnx==ex&&bny==ey)return nstep;
		//一下枚舉所有移動
		//人和箱子靠在一起且可以往另一邊移動 
		if(rnx==bnx&&rny+1==bny&&rny+2<=10&&visited[rnx][rny+1][rnx][rny+2]==0&&map[rnx][rny+2]!=1){
			q.push(state(rnx,rny+1,rnx,rny+2,nstep+1));
			visited[rnx][rny+1][rnx][rny+2]=1;
		}
		if(rnx==bnx&&rny-1==bny&&rny-2>=1&&visited[rnx][rny-1][rnx][rny-2]==0&&map[rnx][rny-2]!=1){
			q.push(state(rnx,rny-1,rnx,rny-2,nstep+1));
			visited[rnx][rny-1][rnx][rny-2]=1;
		}
		if(rny==bny&&rnx+1==bnx&&rnx+2<=10&&visited[rnx+1][rny][rnx+2][rny]==0&&map[rnx+2][rny]!=1){
			q.push(state(rnx+1,rny,rnx+2,rny,nstep+1));
			visited[rnx+1][rny][rnx+2][rny]=1;
		}
		if(rny==bny&&rnx-1==bnx&&rnx-2>=1&&visited[rnx-1][rny][rnx-2][rny]==0&&map[rnx-2][rny]!=1){
			q.push(state(rnx-1,rny,rnx-2,rny,nstep+1));
			visited[rnx-1][rny][rnx-2][rny]=1;
		} 
		//接下來就是人不推箱子,只是走一走的過程
		//按照左下右上順序
		//注意人不能穿過箱子 
		if(rny-1>=1&&visited[rnx][rny-1][bnx][bny]==0&&map[rnx][rny-1]!=1&&!(rnx-bnx==0&&rny-1-bny==0)){
			q.push(state(rnx,rny-1,bnx,bny,nstep+1));
			visited[rnx][rny-1][bnx][bny]=1;
		}
		if(rnx+1<=10&&visited[rnx+1][rny][bnx][bny]==0&&map[rnx+1][rny]!=1&&!(rnx+1-bnx==0&&rny-bny==0)){
			q.push(state(rnx+1,rny,bnx,bny,nstep+1));
			visited[rnx+1][rny][bnx][bny]=1;
		}
		if(rny+1<=10&&visited[rnx][rny+1][bnx][bny]==0&&map[rnx][rny+1]!=1&&!(rnx-bnx==0&&rny+1-bny==0)){
			q.push(state(rnx,rny+1,bnx,bny,nstep+1));
			visited[rnx][rny+1][bnx][bny]=1;
		}
		if(rnx-1>=1&&visited[rnx-1][rny][bnx][bny]==0&&map[rnx-1][rny]!=1&&!(rnx-1-bnx==0&&rny-bny==0)){
			q.push(state(rnx-1,rny,bnx,bny,nstep+1));
			visited[rnx-1][rny][bnx][bny]=1;
		}
	}
	return -1;
}

特殊的二階魔方(未過)

#include<iostream>
#include<string>
#include<queue>
#include<map>
using namespace std;
string s[6];
int visited[16][16][16][16][16][16];

struct state{
	string f0,f1,f2,f3,f4,f5;
	int step;
	state(string a,string b,string c,string d,string e,string f,int step):f0(a),f1(b),f2(c),f3(d),f4(e),f5(f),step(step){}
};

queue<state> q;

void sw(char &a,char &b,char &c,char &d){char ch;ch=a;a=d;d=c;c=b;b=ch;}
void b1(state &t);
void b2(state &t);
void b3(state &t);
int bfs();

map<string,int> m;

int main(){
	
	m["0000"]=0;m["0001"]=1;m["0010"]=2;m["0011"]=3;
	m["0100"]=4;m["0101"]=5;m["0110"]=6;m["0111"]=7;
	m["1000"]=8;m["1001"]=9;m["1010"]=10;m["1011"]=11;
	m["1100"]=12;m["1101"]=13;m["1110"]=14;m["1111"]=15;
	/*map<string,int> m;
	m["0000"]=0;m["0001"]=1;m["0010"]=2;m["0011"]=3;
	m["0100"]=4;m["0101"]=5;m["0110"]=6;m["0111"]=7;
	m["1000"]=8;m["1001"]=9;m["1010"]=10;m["1011"]=11;
	m["1100"]=12;m["1101"]=13;m["1110"]=14;m["1111"]=15;*/
	
	for(int i=0;i<6;i++){
		string ts; 
		for(int j=0;j<2;j++){
			cin>>ts;
			s[i]+=ts;
		}	
	}
	q.push(state(s[0],s[1],s[2],s[3],s[4],s[5],0));
	visited[ m[s[0]] ] [ m[s[1]] ] [ m[s[2]] ] [ m[s[3]] ] [ m[s[4]] ] [ m[s[5]] ]=1;
	cout<<bfs()<<endl;
	return 0;
}

void b1(state &t){
	string& s0=t.f0,s1=t.f1,s2=t.f2,s3=t.f3,s4=t.f4,s5=t.f5;
	sw(s2[2],s5[3],s3[3],s4[2]);
	sw(s2[3],s5[2],s3[2],s4[3]);
	sw(s1[2],s1[0],s1[1],s1[3]);
}

void b2(state &t){
	string& s0=t.f0,s1=t.f1,s2=t.f2,s3=t.f3,s4=t.f4,s5=t.f5;
	sw(s1[1],s4[3],s0[3],s5[1]);
	sw(s1[3],s4[1],s0[1],s5[3]);
	sw(s3[2],s3[3],s3[1],s3[0]);
}

void b3(state &t){
	string& s0=t.f0,s1=t.f1,s2=t.f2,s3=t.f3,s4=t.f4,s5=t.f5;
	sw(s2[0],s0[1],s3[2],s1[0]);
	sw(s2[2],s0[0],s3[0],s1[1]);
	sw(s5[2],s5[0],s5[1],s5[3]);
}

int bfs(){
	while(!q.empty()){
		state n=q.front();
		q.pop();
		string n0=n.f0,n1=n.f1,n2=n.f2,n3=n.f3,n4=n.f4,n5=n.f5;
		int nstep=n.step;
		if(m[n0]==0||m[n1]==0||m[n1]==0||m[n1]==0||m[n1]==0||m[n1]==0)return nstep;
		//考察六種可能轉法 
		state tn=n;
		//1
		b1(tn);
		if(visited[ m[ tn.f0 ] ] [ m[ tn.f1 ] ] [ m[ tn.f2 ] ] [ m[ tn.f3 ] ] [ m[ tn.f4 ] ] [ m[ tn.f5 ] ]==0){
			q.push(state(tn.f0,tn.f1,tn.f2,tn.f3,tn.f4,tn.f5,nstep+1));
			visited[ m[ tn.f0 ] ] [ m[ tn.f1 ] ] [ m[ tn.f2 ] ] [ m[ tn.f3 ] ] [ m[ tn.f4 ] ] [ m[ tn.f5 ] ]=1;	
		}
		//2
		b1(tn);b1(tn);
		if(visited[ m[ tn.f0 ] ] [ m[ tn.f1 ] ] [ m[ tn.f2 ] ] [ m[ tn.f3 ] ] [ m[ tn.f4 ] ] [ m[ tn.f5 ] ]==0){
			q.push(state(tn.f0,tn.f1,tn.f2,tn.f3,tn.f4,tn.f5,nstep+1));
			visited[ m[ tn.f0 ] ] [ m[ tn.f1 ] ] [ m[ tn.f2 ] ] [ m[ tn.f3 ] ] [ m[ tn.f4 ] ] [ m[ tn.f5 ] ]=1;	
		}
		b1(tn);
		//3
		b2(tn);
		if(visited[ m[ tn.f0 ] ] [ m[ tn.f1 ] ] [ m[ tn.f2 ] ] [ m[ tn.f3 ] ] [ m[ tn.f4 ] ] [ m[ tn.f5 ] ]==0){
			q.push(state(tn.f0,tn.f1,tn.f2,tn.f3,tn.f4,tn.f5,nstep+1));
			visited[ m[ tn.f0 ] ] [ m[ tn.f1 ] ] [ m[ tn.f2 ] ] [ m[ tn.f3 ] ] [ m[ tn.f4 ] ] [ m[ tn.f5 ] ]=1;	
		}
		//4
		b2(tn);b2(tn);
		if(visited[ m[ tn.f0 ] ] [ m[ tn.f1 ] ] [ m[ tn.f2 ] ] [ m[ tn.f3 ] ] [ m[ tn.f4 ] ] [ m[ tn.f5 ] ]==0){
			q.push(state(tn.f0,tn.f1,tn.f2,tn.f3,tn.f4,tn.f5,nstep+1));
			visited[ m[ tn.f0 ] ] [ m[ tn.f1 ] ] [ m[ tn.f2 ] ] [ m[ tn.f3 ] ] [ m[ tn.f4 ] ] [ m[ tn.f5 ] ]=1;	
		}
		b2(tn);
		//5
		b3(tn);
		if(visited[ m[ tn.f0 ] ] [ m[ tn.f1 ] ] [ m[ tn.f2 ] ] [ m[ tn.f3 ] ] [ m[ tn.f4 ] ] [ m[ tn.f5 ] ]==0){
			q.push(state(tn.f0,tn.f1,tn.f2,tn.f3,tn.f4,tn.f5,nstep+1));
			visited[ m[ tn.f0 ] ] [ m[ tn.f1 ] ] [ m[ tn.f2 ] ] [ m[ tn.f3 ] ] [ m[ tn.f4 ] ] [ m[ tn.f5 ] ]=1;	
		}
		//6 
		b3(tn);b3(tn);
		if(visited[ m[ tn.f0 ] ] [ m[ tn.f1 ] ] [ m[ tn.f2 ] ] [ m[ tn.f3 ] ] [ m[ tn.f4 ] ] [ m[ tn.f5 ] ]==0){
			q.push(state(tn.f0,tn.f1,tn.f2,tn.f3,tn.f4,tn.f5,nstep+1));
			visited[ m[ tn.f0 ] ] [ m[ tn.f1 ] ] [ m[ tn.f2 ] ] [ m[ tn.f3 ] ] [ m[ tn.f4 ] ] [ m[ tn.f5 ] ]=1;	
		}
		b3(tn);
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章