WAWAWAWAWAWAWAWAWAWAWAWAWAWAWAWAWA
查看原帖
WAWAWAWAWAWAWAWAWAWAWAWAWAWAWAWAWA
1246271
sshhgggddsbf楼主2024/11/30 11:01
#include<bits/stdc++.h>

#include<iomanip>

#include <iostream>

#include <cstdio>

#include <fstream>

#include <algorithm>

#include <cmath>

#include <deque>

#include <vector>

#include <queue>

#include <string>

#include <cstring>

#include <map>

#include <stack>

#include <set>

#define I using
#define FUCK namespace
#define CCF std;
I FUCK CCF;string st,ed;
char g[1010][1010];
unordered_map<string, int> dist;int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
void mset(string s){
	for(int i=0;i<4;i++){
		for(int j=0;j<4;j++){
			g[i][j]=st[i*4+j];
		}
	}
}
string mget(){
	string s;
	for(int i=0;i<4;i++)
		for(int j=0;j<4;j++)s+=g[i][j];
		return s;
}
void bfs(string st,string ed){
	queue<string>q;
	q.push(st);
	dist[st]=0;
	while(q.size()){
		string t=q.front();
		q.pop();
		for(int i=0;i<4;i++){
			for(int j=0;j<4;j++){
				if(t[i*4+j]=='1'){
					for(int k=0;k<4;k++){
						mset(t);
						int nx=i+dx[k],ny=j+dy[k];
						if(t[nx*4+ny]=='0'){
							swap(g[i][j],g[nx][ny]);
							string ns=mget();
							if(dist.count(ns)==0){
								dist[ns]=dist[t]+1;
								q.push(ns);
							}	
						}
					}
				}
			}
		}
	}
}


signed main(){
for(int i=0;i<4;i++){
		string s;cin>>s;
		st+=s;
}
for(int i=0;i<4;i++){
	string s;
	cin>>s;
	ed+=s;
}
bfs(st,ed);
cout<<dist[ed]<<endl;
return 0;
}

2024/11/30 11:01
加载中...