kuangbin专题题目一
本文是kuangbin专题的简单搜索题解,题解可能写的不是很清晰,不过不重要,代码还是很容易理解的。
做题顺序
7 11 2 12 3 1 8 13 6 14 10 9 5 4
附上题目链接
kuangbin专题链接
这里先摆了第七题
7 Shuffle’m Up
#include<iostream>
#include<cstring>
#include<queue>
#include<string>
#include<map>
#define MAX 300
using namespace std;
int n;
string s,s1,s2,s3,str;
int flag;
int sum;
map<string,int>mp;
void dfs()
{
sum++;
str="";
for(int i=0;i<n;i++)
{
str+=s2[i];
str+=s1[i];
}
if(str==s)
{
flag=1;
return ;
}
if(mp[str]!=0)
{
flag=-1;
return ;
}
mp[str]++;
for(int i=0;i<2*n;i++)
{
if(i<n)
s1[i]=str[i];
else
s2[i-n]=str[i];
}
dfs();
return ;
}
int main()
{
int t;cin>>t;
for(int p=1;p<=t;p++)
{
flag=0;
cin>>n;
cin>>s1>>s2;
s3+=s1;
s3+=s2;
mp[s3]++;
cin>>s;
sum=0;
dfs();
cout<<p<<" ";
if(flag==1)
cout<<sum<<endl;
else
cout<<"-1"<<endl;
}
}
11 迷宫问题
#include<iostream>
#define MAX 105
using namespace std;
struct node{
int x;
int y;
int pre;
}fp[MAX];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int g[MAX][MAX];
int a[MAX][MAX];
int m=0;
int pos=0;
void bfs()
{
fp[0].x=0;fp[0].y=0;fp[0].pre=-1;
m++;
g[0][0]=1;
while(m>pos)
{
for(int i=0;i<4;i++)
{
int xi=fp[pos].x+dir[i][0];
int yi=fp[pos].y+dir[i][1];
if(xi>5||xi<0||yi>5||yi<0||g[xi][yi]==1||a[xi][yi]==1)
continue;
fp[m].x=xi;
fp[m].y=yi;
fp[m].pre=pos;
m++;
g[xi][yi]=1;
if(xi==4&&yi==4)
return ;
}
pos++;
}
}
void dfs(node now)
{
if(now.pre==-1)
cout<<"("<<now.x<<", "<<now.y<<")"<<endl;
else
{
dfs(fp[now.pre]);
cout<<"("<<now.x<<", "<<now.y<<")"<<endl;
}
return ;
}
int main()
{
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
cin>>a[i][j];
bfs();
dfs(fp[m-1]);
}
2 Dungeon Master
#include<iostream>
#include<queue>
#include<cstring>
#define MAX 105
using namespace std;
struct node{
int x;
int y;
int z;
int pre;
}fp;
int H,X,Y;
int dir[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
int g[MAX][MAX][MAX];
char a[MAX][MAX][MAX];
int m=0;
int pos=0;
int bfs()
{
queue<node>q;
q.push(fp);
while(!q.empty())
{
node t=q.front();
q.pop();
for(int i=0;i<6;i++)
{
int xi=t.x+dir[i][0];
int yi=t.y+dir[i][1];
int zi=t.z+dir[i][2];
int step=t.pre+1;
if(xi>X-1||xi<0||yi>Y-1||yi<0||zi>H-1||zi<0||g[xi][yi][zi]==1||a[xi][yi][zi]=='#')
continue;
node r={xi,yi,zi,step};
q.push(r);
g[xi][yi][zi]=1;
if(a[xi][yi][zi]=='E')
return step;
}
}
return -1;
}
int main()
{
while(cin>>H>>X>>Y)
{
memset(g,0,sizeof g);
if(H==0&&X==0&&Y==0)
break;
for(int i=0;i<H;i++)
for(int j=0;j<X;j++)
for(int k=0;k<Y;k++)
{
cin>>a[j][k][i];
if(a[j][k][i]=='S')
{
fp={j,k,i,0};
g[j][k][i]=1;
}
}
int p=bfs();
if(p==-1)
cout<<"Trapped!"<<endl;
else
cout<<"Escaped in "<<p<<" minute(s)."<<endl;
}
}
12 Oil Deposits
#include<iostream>
#include<queue>
#include<cstring>
#define MAX 105
using namespace std;
struct node{
int x;
int y;
};
int N,M;
char ch[MAX][MAX];
int g[MAX][MAX];
int dis[8][2]={{1,0},{-1,0},{0,1},{0,-1},{-1,-1},{1,-1},{-1,1},{1,1}};
int bfs()
{
int ans=0;
queue<node>q;
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
if(ch[i][j]=='@'&&g[i][j]==0)
{
g[i][j]=1;
node f{i,j};
q.push(f);
ans++;
while(!q.empty())
{
node r=q.front();
q.pop();
for(int i=0;i<8;i++)
{
int xi=r.x+dis[i][0];
int yi=r.y+dis[i][1];
if(xi<0||xi>=N||yi<0||yi>=M||g[xi][yi]==1||ch[xi][yi]!='@')
continue;
g[xi][yi]=1;
node f{xi,yi};
q.push(f);
}
}
}
}
}
return ans;
}
int main()
{
while(cin>>N>>M)
{
if(M==0)
break;
for(int i=0;i<N;i++)
for(int j=0;j<M;j++)
cin>>ch[i][j];
memset(g,0,sizeof g);
cout<<bfs()<<endl;
}
}
3 Catch That Cow
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int n,m;
int f[1000005];
int mmin=99999999;
int bfs()
{
pair<int,int>p;
queue<pair<int,int> >q;
p.first=n;
p.second=0;
q.push(p);
memset(f,0,sizeof f);
f[n]=1;
while(!q.empty())
{
pair<int,int>a;
a=q.front();
q.pop();
if(a.first==m)
return a.second;
if(a.first+1<=m&&f[a.first+1]==0)
{
pair<int,int>t;
t=a;
t.first++;
t.second++;
if(t.first==m)
return t.second;
q.push(t);
f[t.first]=1;
//cout<<t.first<<endl;
}
if(a.first-1>=0&&f[a.first-1]==0)
{
pair<int,int>t;
t=a;
t.first--;
t.second++;
if(t.first==m)
return t.second;
q.push(t);
f[t.first]=1;
//cout<<t.first<<endl;
}
if(a.first>0&&f[a.first*2]==0&&a.first*2<=200000)
{
pair<int,int>t;
t=a;
t.first*=2;
t.second++;
if(t.first==m)
return t.second;
q.push(t);
f[t.first]=1;
//cout<<t.first<<endl;
}
}
}
int main()
{
cin>>n>>m;
int f=bfs();
cout<<f<<endl;
}
1 棋盘问题
#include<iostream>
#include<cstring>
using namespace std;
char ch[10][10];
int f[10];
int n,m;
int ans=0;
void dfs(int x,int m)
{
if(x>=n)
{
if(m==0) ans++;
return ;
}
for(int i=0;i<n;i++)
{
if(ch[x][i]=='#'&&!f[i])
{
f[i]=1;
dfs(x+1,m-1);
f[i]=0;
}
}
dfs(x+1,m);
}
int main()
{
while(cin>>n>>m)
{
if(n==-1&&m==-1)
return 0;
memset(f,0,sizeof(f));
ans=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cin>>ch[i][j];
}
dfs(0,m);
cout<<ans<<endl;
}
}
8 Post
#include<iostream>
#include<queue>
#include<cstring>
#define MAX 1005
using namespace std;
int a,b,c;
int filla=1,fillb=2,dropa=3,dropb=4,pourab=5,pourba=6;
int num=0;
int abf[MAX][MAX];
int flag=0;
string str[]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
struct node{
int a1;
int b1;
string s;
};
void bfs()
{
queue<node>q;
q.push(node{a,0,"0"});
abf[a][0]=1;
q.push(node{0,b,"1"});
abf[0][b]=1;
while(!q.empty())
{
node r=q.front();
q.pop();
if(r.a1==c||r.b1==c)
{
flag=1;
cout<<(r.s).size()<<" "<<endl;
//cout<<r.a1<<" "<<r.b1<<endl;
for(int i=0;i<(r.s).size();i++)
{
if(i<(r.s).size()-1)
cout<<str[(r.s)[i]-'0']<<" "<<endl;
else
cout<<str[(r.s)[i]-'0']<<endl;
}
return ;
}
node f;
f={a,r.b1,r.s+"0"};
if(!abf[f.a1][f.b1])
{
q.push(f);
abf[f.a1][f.b1]=1;
}
f={r.a1,b,r.s+"1"};
if(!abf[f.a1][f.b1])
{
q.push(f);
abf[f.a1][f.b1]=1;
}
f={0,r.b1,r.s+"2"};
if(!abf[f.a1][f.b1])
{
q.push(f);
abf[f.a1][f.b1]=1;
}
f={r.a1,0,r.s+"3"};
if(!abf[f.a1][f.b1])
{
q.push(f);
abf[f.a1][f.b1]=1;
}
if(r.a1>b-r.b1)
f={r.a1-(b-r.b1),b,r.s+"4"};
else
f={0,r.b1+r.a1,r.s+"4"};
if(!abf[f.a1][f.b1])
{
q.push(f);
abf[f.a1][f.b1]=1;
}
if(r.b1>a-r.a1)
f={a,r.b1-(a-r.a1),r.s+"5"};
else
f={r.a1+r.b1,0,r.s+"5"};
if(!abf[f.a1][f.b1])
{
q.push(f);
abf[f.a1][f.b1]=1;
}
}
}
int main()
{
cin>>a>>b>>c;
memset(abf,0,sizeof abf);
bfs();
if(!flag)
cout<<"impossible"<<endl;
}
13 非常可乐
#include<iostream>
#include<queue>
#include<cstring>
#define MAX 105
using namespace std;
struct node{
int s;
int n;
int m;
int ans;
};
int fp[MAX][MAX][MAX];
int S,N,M;
int flag=0;
int bfs()
{
queue<node>q;
node t={S,0,0,0};
fp[S][0][0]=1;
q.push(t);
while(!q.empty())
{
node r;
r=q.front();
q.pop();
if((r.s==r.m&&r.s+r.m==S)||(r.s==r.n&&r.s+r.n==S)||(r.n==r.m&&r.n+r.m==S))
{
flag=1;
cout<<r.ans<<endl;
return 0;
}
for(int i=0;i<6;i++)
{
int xs,xn,xm,xans;
xs=r.s;
xn=r.n;
xm=r.m;
xans=r.ans+1;
if(i==0)
{
int temp;
temp=min(r.s,N-r.n);
xs-=temp;
xn+=temp;
}
if(i==1)
{
int temp;
temp=min(r.s,M-r.m);
xs-=temp;
xm+=temp;
}
if(i==2)
{
int temp;
temp=min(r.n,S-r.s);
xn-=temp;
xs+=temp;
}
if(i==3)
{
int temp;
temp=min(r.n,M-r.m);
xn-=temp;
xm+=temp;
}
if(i==4)
{
int temp;
temp=min(r.m,N-r.n);
xm-=temp;
xn+=temp;
}
if(i==5)
{
int temp;
temp=min(r.m,S-r.s);
xm-=temp;
xs+=temp;
}
node f={xs,xn,xm,xans};
if(!fp[f.s][f.n][f.m])
{
q.push(f);
fp[f.s][f.n][f.m]=1;
}
}
}
cout<<"NO"<<endl;
}
int main()
{
while(cin>>S>>N>>M)
{
if(S==0&&N==0&&M==0)
break;
memset(fp,0,sizeof fp);
if(S%2)
{
cout<<"NO"<<endl;
continue;
}
bfs();
}
}
6 Prime Path
#include<iostream>
#include<cstring>
#include<queue>
#include<map>
#define MAX 10005
using namespace std;
struct node{
int key;
int ans;
};
int N,M;
int f[MAX];
int _ok(int fp)
{
for(int i=2;i*i<=fp;i++)
{
if(fp%i==0)
return 0;
}
return 1;
}
void bfs()
{
queue<node>q;
node t={N,0};
f[N]=1;
q.push(t);
while(!q.empty())
{
node r=q.front();
q.pop();
//cout<<r.key<<endl;
if(r.key==M)
{
cout<<r.ans<<endl;
return ;
}
for(int i=0;i<4;i++)
{
if(i==0)
{
int g=r.key/10*10;
for(int i=0;i<=9;i++)
{
if(g+i<=9999&&_ok(g+i)==1&&f[g+i]==0)
{
node fp={g+i,r.ans+1};
q.push(fp);
f[g+i]=1;
}
}
}
if(i==1)
{
int a=r.key%10;
int g=r.key/100*100;
for(int i=0;i<=9;i++)
{
if(g+i*10+a<=9999&&_ok(g+i*10+a)==1&&f[g+i*10+a]==0)
{
node fp={g+i*10+a,r.ans+1};
q.push(fp);
f[g+i*10+a]=1;
}
}
}
if(i==2)
{
int a=r.key%100;
int g=r.key/1000*1000;
for(int i=0;i<=9;i++)
{
if(g+i*100+a<=9999&&_ok(g+i*100+a)==1&&f[g+i*100+a]==0)
{
node fp={g+i*100+a,r.ans+1};
q.push(fp);
f[g+i*100+a]=1;
}
}
}
if(i==3)
{
int a=r.key%1000;
for(int i=1;i<=9;i++)
{
if(i*1000+a<=9999&&_ok(i*1000+a)==1&&f[i*1000+a]==0)
{
node fp={i*1000+a,r.ans+1};
q.push(fp);
f[i*1000+a]=1;
}
}
}
}
}
cout<<"0"<<endl;
}
int main()
{
int T;cin>>T;
while(T--)
{
cin>>N>>M;
memset(f,0,sizeof f);
bfs();
}
}
14 Find a way
#include<iostream>
#include<queue>
#include<cstring>
#define MAX 305
using namespace std;
struct node {
int x;
int y;
int ans;
};
int N,M;
int fy[MAX][MAX];
int fm[MAX][MAX];
int px[MAX][MAX];
int x1,y1,x2,y2;
char ch[MAX][MAX];
int dis[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
void bfs()
{
queue<node>q;
node t={x1,y1,0};
fy[x1][y1]=-1;
q.push(t);
while(!q.empty())
{
node r=q.front();
q.pop();
if(ch[r.x][r.y]=='@')
{
px[r.x][r.y]+=r.ans;
}
for(int i=0;i<4;i++)
{
int xi=r.x+dis[i][0];
int yi=r.y+dis[i][1];
if(xi>=N||xi<0||yi>=M||yi<0||fy[xi][yi]!=0||ch[xi][yi]=='#')
continue;
node f={xi,yi,r.ans+1};
q.push(f);
fy[xi][yi]=-1;
}
}
t={x2,y2,0};
q.push(t);
fm[x2][y2]=1;
while(!q.empty())
{
node r=q.front();
q.pop();
if(ch[r.x][r.y]=='@')
{
px[r.x][r.y]+=r.ans;
}
for(int i=0;i<4;i++)
{
int xi=r.x+dis[i][0];
int yi=r.y+dis[i][1];
if(xi>=N||xi<0||yi>=M||yi<0||fm[xi][yi]==1||ch[xi][yi]=='#')
continue;
node f={xi,yi,r.ans+1};
q.push(f);
fm[xi][yi]=1;
}
}
return ;
}
int main()
{
while(cin>>N>>M)
{
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
cin>>ch[i][j];
if(ch[i][j]=='Y')
x1=i,y1=j;
if(ch[i][j]=='M')
x2=i,y2=j;
}
}
memset(fy,0,sizeof fy);
memset(fm,0,sizeof fm);
memset(px,0,sizeof px);
bfs();
int mmax=500;
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
//cout<<px[i][j]<<" ";
if(px[i][j]>0)
mmax=min(mmax,px[i][j]);
}
//cout<<endl;
}
cout<<mmax*11<<endl;
}
}
10 Fire!
#include<bits/stdc++.h>
#include<queue>
#include<cstring>
#define MAX 3005
using namespace std;
struct node{
int x;
int y;
int ans;
};
int N,M;
int fF[MAX][MAX];
int fJ[MAX][MAX];
char ch[MAX][MAX];
int xj,yj,xf,yf;
int dis[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
queue<node>q;
void bfs()
{
while(!q.empty())
{
node r=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int xi=r.x+dis[i][0];
int yi=r.y+dis[i][1];
if(xi<N&&xi>=0&&yi<M&&yi>=0&&ch[xi][yi]!='#'&&fF[xi][yi]==-1)
{
node f={xi,yi,r.ans+1};
q.push(f);
fF[xi][yi]=r.ans+1;
}
}
}
node t={xj,yj,0};
q.push(t);
fJ[xj][yj]=1;
while(!q.empty())
{
node r=q.front();
q.pop();
if(r.x==N-1||r.x==0||r.y==M-1||r.y==0)
{
cout<<r.ans+1<<endl;
return ;
}
for(int i=0;i<4;i++)
{
int xi=r.x+dis[i][0];
int yi=r.y+dis[i][1];
if(ch[xi][yi]!='#'&&fJ[xi][yi]==0&&(r.ans+1<fF[xi][yi]||fF[xi][yi]==-1))
{
node f={xi,yi,r.ans+1};
//cout<<xi<<" "<<yi<<endl;
q.push(f);
fJ[xi][yi]=1;
}
}
}
cout<<"IMPOSSIBLE"<<endl;
return ;
}
void bfs1()
{
while(!q.empty())
{
node r=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int xi=r.x+dis[i][0];
int yi=r.y+dis[i][1];
if(xi<N&&xi>=0&&yi<M&&yi>=0&&ch[xi][yi]!='#'&&fF[xi][yi]==-1)
{
node f={xi,yi,r.ans+1};
q.push(f);
fF[xi][yi]=r.ans+1;
}
}
}
}
void bfs2()
{
node m={xj,yj,0};
q.push(m);
fJ[xj][yj]=1;
while(!q.empty())
{
node r=q.front();
q.pop();
if(r.x==N-1||r.x==0||r.y==M-1||r.y==0)
{
cout<<r.ans+1<<endl;
return ;
}
for(int i=0;i<4;i++)
{
int xi=r.x+dis[i][0];
int yi=r.y+dis[i][1];
if(ch[xi][yi]!='#'&&xi<N&&xi>=0&&yi<M&&yi>=0&&fJ[xi][yi]==0&&(r.ans+1<fF[xi][yi]||fF[xi][yi]==-1))
{
node f={xi,yi,r.ans+1};
q.push(f);
fJ[xi][yi]=1;
}
}
}
cout<<"IMPOSSIBLE"<<endl;
return ;
}
int main()
{
int t;cin>>t;
while(t--)
{
cin>>N>>M;
while(!q.empty())
q.pop();
memset(fF,0,sizeof fF);
memset(fJ,0,sizeof fJ);
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
fF[i][j]=-1;
}
}
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
cin>>ch[i][j];
if(ch[i][j]=='J')
xj=i,yj=j;
if(ch[i][j]=='F')
{
xf=i,yf=j;
node f={xf,yf,0};
q.push(f);
fF[i][j]=0;
}
}
}
//bfs1();
//bfs2();
bfs();
/*
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
cout<<fF[i][j]<<" ";
}
cout<<endl;
}
*/
}
}
/*
5
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F
5 5
####.
.J#F.
###..
.....
.....
5 5
#####
.J#F.
.F#..
###..
.....
5 5
#####
##J##
..F..
#####
#####
*/
4 Fire Game
#include<iostream>
#include<queue>
#include<cstring>
#include<map>
#define MAX 15
using namespace std;
struct node{
int x;
int y;
int cnt;
};
int T,N,M;
int sum=0;
int fm=0;
int temp1;
int flag=0;
int anss=10000;
int fp[MAX][MAX];
int dis[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
char ch[MAX][MAX];
int vis[MAX*MAX][2];
queue<node>q;
int bfs()
{
node r;
while(!q.empty())
{
r=q.front();
q.pop();
node f;
for(int i=0;i<4;i++)
{
int xi=r.x+dis[i][0];
int yi=r.y+dis[i][1];
if(ch[xi][yi]=='#'&&fp[xi][yi]==0&&xi<N&&xi>=0&&yi<M&&yi>=0)
{
f.x=xi;f.y=yi;f.cnt=r.cnt+1;
fp[xi][yi]=1;
q.push(f);
fm++;
}
}
}
if(fm==sum)
{
flag=1;
return r.cnt;
}
return -1;
}
int main()
{
cin>>T;
for(int l=1;l<=T;l++)
{
cin>>N>>M;
sum=0;
temp1=0;
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
cin>>ch[i][j];
if(ch[i][j]=='#')
{
vis[temp1][0]=i;
vis[temp1++][1]=j;
sum++;
}
}
}
if(sum<=2)
{
cout<<"Case "<<l<<": 0"<<endl;
continue;
}
while(!q.empty())
q.pop();
anss=10000;
flag=0;
for(int i=0;i<temp1;i++)
{
for(int j=0;j<temp1;j++)
{
memset(fp,0,sizeof fp);
fm=0;
node a1={vis[i][0],vis[i][1],0};
node a2={vis[j][0],vis[j][1],0};
fp[a1.x][a1.y]=1;
fp[a2.x][a2.y]=1;
q.push(a1);
q.push(a2);
fm+=2;
int op=bfs();
if(op!=-1)
{
anss=min(op,anss);
}
}
}
if(flag)
cout<<"Case "<<l<<": "<<anss<<endl;
else
cout<<"Case "<<l<<": -1"<<endl;
}
}
5 Find The Multiple
#include<iostream>
#include<queue>
#include<cstring>
#include<map>
#define MAX 100005
using namespace std;
int N;
long long x,y;
void bfs()
{
queue<long long>q;
q.push(x);
while(!q.empty())
{
long long r=q.front();
q.pop();
if(r%N==0)
{
cout<<r<<endl;
return ;
}
x=r*10;
y=x+1;
q.push(x);
q.push(y);
}
return ;
}
int main()
{
while(cin>>N)
{
if(N==0)
break;
x=1;
bfs();
}
}