bfs之加强版
在之前的题目中,我们的 BFS 都是干货。直接上不就完事了?在今天的题目中,难度加大了,而且需要你找规律,从而找到搜索的奥秘。
P1379 八数码难题
题目描述
在
输入 #1
283104765
输出 #1
4
样例解释
图中标有
并且可以证明,不存在更优的策略。
解题思路:
- 首先看到本题,本题是让我们交换一些数字(和
0交换),使得最终的数字为123804765。 - 对于本题而言,我们不是迷宫。我们需要扩散的是一个字符串。所以,我们可以把字符串想象成一个九宫格迷宫,这样就好做了。
- 其次,我们可以用一个九维的
vis数组来表示状态。vis_{i,j,k,l,n,m,q,p,u} 表示的就是当这个九宫格里的数分别为i,j,k,l,n,m,q,p,u 时的情况是否出现过。我们就可以拿这个数字来标记状态。当然,这样会超内存,我们需要考虑别的方法。 - 对于一个
0而言,我们可以考虑和它相邻的格子进行交换。(如样例解释) 当然,它本来是一个字符串,现在我们把它当做九宫格看。比如我们的样例283104765。如果我们选取字符1。那么它在九宫格的位置就是(2,1) ,在原字符串中的位置是3 。再找出0。在九宫格的位置是(2,2) 。在字符串中的位置是4 。所以我们可以得出第一条结论:当字符0和左右相邻的两个字符交换,就是和下标为i+1,i-1 的字符交换。 - 那么,我们还可以和上下两个字符交换。还是以上面的来说。我们选取
0上面的字符8。那么它在九宫格的位置就是(1,2) ,在原字符串中的位置是1 。再找出0。在九宫格的位置是(2,2) 。在字符串中的位置是4 。所以我们又可以得出一条结论:当字符0和上下相邻的两个字符交换,就是和下标为i+3,i-3 的字符交换。 - 那么,如果仅仅用这两条结论,肯定是不行的。如果我们要和上一行的最有一个字符和下一行的第一个字符交换是不可以的。而我们的结论一就会被 hack 掉,同理,结论二也有这个缺点,所以我们在搜索时需要特判。
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e6+5;
struct node{
string s;
int step;
};
int f[]={3,-3,-1,1};
map<string,bool>vis;
string a;
int bfs(){
queue<node>q;
q.push({a,0});
vis[a]=true;
while(!q.empty()){
node t=q.front();
if(t.s=="123804765"){//找到答案
return t.step;
}
q.pop();
string b=t.s;
int xx=0;
for(int i=0;i<b.size();i++){
if(b[i]=='0'){//找出0出现的位置
xx=i;
break;
}
}
for(int i=0;i<4;i++){
int nx=f[i]+xx;
string tt=t.s;
swap(tt[nx],tt[xx]);//和0交换,出现新的字符串,相当于扩散操作。
//一坨不合法条件,恶心
if(xx==2&&f[i]==1){
continue;
}
if(xx==5&&f[i]==1){
continue;
}
if(xx==8&&f[i]==1){
continue;
}
if(xx==3&&f[i]==-1){
continue;
}
if(xx==6&&f[i]==-1){
continue;
}
if(nx>=0&&nx<9&&!vis[tt]){//判断是否符合要求
vis[tt]=true;
q.push({tt,t.step+1});
}
}
}
}
signed main(){
cin>>a;
cout<<bfs();
return 0;
}
P2730 [USACO3.2] 魔板 Magic Square
呜呜呜,这题太恶心了。
和上一题差不多。我们扩散的还是字符串。我们可以把题目给我们的三种操作进行模拟,如果没有出现过这种状态,就装进队列即可。
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e6+5;
struct node{
string s;
int step;
string ans;
};
map<string,bool>vis;
string a;
void bfs(){
queue<node>q;
q.push({"12345678",0,""});
vis["12345678"]=true;
while(!q.empty()){
node t=q.front();
if(t.s==a){//找到答案
cout<<t.step<<"\n";
cout<<t.ans;
return ;
}
string abc=t.s;
q.pop();
for(int i=0;i<3;i++){//这三种操作可以疯狂swap
if(i==0){//A操作
string tt=abc;
swap(tt[0],tt[7]);
swap(tt[1],tt[6]);
swap(tt[2],tt[5]);
swap(tt[3],tt[4]);
if(!vis[tt]){
vis[tt]=true;
q.push({tt,t.step+1,t.ans+'A'});
}
}
if(i==1){//B操作
string tt=abc;
swap(tt[3],tt[2]);
swap(tt[2],tt[1]);
swap(tt[1],tt[0]);
swap(tt[4],tt[5]);
swap(tt[5],tt[6]);
swap(tt[6],tt[7]);
if(!vis[tt]){
vis[tt]=true;
q.push({tt,t.step+1,t.ans+'B'});
}
}
if(i==2){//C 操作
string tt=abc;
swap(tt[2],tt[5]);
swap(tt[2],tt[1]);
swap(tt[1],tt[6]);
if(!vis[tt]){
vis[tt]=true;
q.push({tt,t.step+1,t.ans+'C'});
}
}
}
}
return ;
}
signed main(){
char ch;
while(cin>>ch){
if(ch!=' '){
a+=ch;
}
}
bfs();
return 0;
}
家庭作业
T582141 星际跃迁追踪协议
这题相比前面的题目,要简单一些,因为他是一维的,而且操作方式已经告诉你了。我们只需要每次取出队首后判断
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=4e5+6;
bool vis[maxn];
struct node{
int x;
int step;
};
int a,b;
int bfs(int x,int y){
queue<node>q;
q.push({x,0});
vis[x]=true;
while(!q.empty()){
node t=q.front();
q.pop();
if(t.x==y){
return t.step;
}
//能走的条件:在边界之内,没走过
if(t.x-1>=1&&t.x-1<=maxn&&!vis[t.x-1]){///不确定上界,所以用maxn
vis[t.x-1]=true;
q.push({t.x-1,t.step+1});
}
if(t.x+1>=1&&t.x+1<=maxn&&!vis[t.x+1]){//同上
vis[t.x+1]=true;
q.push({t.x+1,t.step+1});
}
if(t.x*2>=1&&t.x*2<=maxn&&!vis[t.x*2]){
vis[t.x*2]=true;
q.push({t.x*2,t.step+1});
}
}
return 0;
}
signed main(){
cin>>a>>b;
cout<<bfs(a,b);
return 0;
}
P1135 奇怪的电梯
这题和上一题思路一样!只是把
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e6+5;
int k[maxn];
struct node{
int x,step;
};
int n,a,b;
bool vis[maxn];
int ans;
bool check(int x){
if(x<0||x>maxn){
return false;
}
if(vis[x]){
return false;
}
return true;
}
int bfs(){
queue<node>q;
q.push({a,0});
while(!q.empty()){
node t=q.front();
if(t.x==b){
return t.step;
}
q.pop();
int x=t.x,y;
y=x-k[x];
if(check(y)){
vis[y]=true;
q.push({y,t.step+1});
}
y=x+k[x];
if(check(y)){
vis[y]=true;
q.push({y,t.step+1});
}
}
return -1;
}
signed main(){
cin>>n>>a>>b;
for(int i=1;i<=n;i++){
cin>>k[i];
}
cout<<bfs()<<endl;
return 0;
}
P1588 [USACO07OPEN] Catch That Cow S
不是,这题和第一题一样啊!代码也一样,这是加了多组测试数据而已。
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=4e5+6;
bool vis[maxn];
struct node{
int x;
int step;
};
int a,b;
int bfs(int x,int y){
memset(vis,0,sizeof(vis));
queue<node>q;
q.push({x,0});
vis[x]=true;
while(!q.empty()){
node t=q.front();
q.pop();
if(t.x==y){
return t.step;
}
if(t.x-1>=1&&t.x-1<=maxn&&!vis[t.x-1]){
vis[t.x-1]=true;
q.push({t.x-1,t.step+1});
}
if(t.x+1>=1&&t.x+1<=maxn&&!vis[t.x+1]){
vis[t.x+1]=true;
q.push({t.x+1,t.step+1});
}
if(t.x*2>=1&&t.x*2<=maxn&&!vis[t.x*2]){
vis[t.x*2]=true;
q.push({t.x*2,t.step+1});
}
}
return 0;
}
void work(){
cin>>a>>b;
cout<<bfs(a,b)<<"\n";
return ;
}
signed main(){
int T;
cin>>T;
while(T--){
work();
}
return 0;
}