noi寒假刷题之旅_ 2.5基本算法之搜索(36题)
»2.5基本算法之搜索(36题)
1159:Maze
#include<iostream>
#include<cstring>
#define MAX 22
/*
用dfs会出现先到达终点,但是钥匙没找完的情况
*/
using namespace std;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int n,M;
char t[MAX][MAX];
int vis[MAX][MAX];
struct Pos
{
int x,y;
} s,m[5];
int key[5],r[5],locx[5],locy[5],flag=0;
bool check()
{
for(int i=0; i<5; i++)
if(key[i]!=r[i])
return 1;
return 0;
}
void DFS(int posi,int posj)
{
if(t[posi][posj]=='G') { flag=1;return; }
if(('A'<=t[posi][posj]&&t[posi][posj]<='E')||t[posi][posj]=='X'||posi<=0||posi>M||posj<=0||posj>n){ return; }
if('a'<=t[posi][posj]&&t[posi][posj]<='e')
{
key[t[posi][posj]-'a']--;
if(!key[t[posi][posj]-'a']) t[locx[t[posi][posj]-'a']][locy[t[posi][posj]-'a']]='.';
t[posi][posj]='.';
// if(!key[t[posi][posj]-'a']) t[m[t[posi][posj]-'a'].x][m[t[posi][posj]-'a'].y]='.';
}
for(int i=0;i<4;++i)
{
if(vis[dx[i]+posi][dy[i]+posj]) continue;
else vis[dx[i]+posi][dy[i]+posj]=1;
DFS(dx[i]+posi,dy[i]+posj);
if(flag) return;
}
}
void inti()
{
memset(t,'X',sizeof(t));
memset(vis,0,sizeof(vis));
memset(key,0,sizeof(key));
memset(r,0,sizeof(r));
memset(m,0,sizeof(m));
flag=0;
}
int main()
{
while(~scanf("%d%d",&M,&n))
{
if(M==0&&n==0) break;
for(int i=1;i<=M;++i)
{
getchar();
for(int j=1;j<=n;++j)
{
cin>>t[i][j];
if(t[i][j]=='S'){ s.x=i;s.y=j; }
if('a'<=t[i][j]&&t[i][j]<='e'){ key[t[i][j]-'a']++; }//保存一下每种钥匙的数量
if(t[i][j]>='A'&&t[i][j]<='E') locx[t[i][j]-'A']=i,locy[t[i][j]-'A']=j; //存放每一个门的位置
// if('A'<=t[i][j]&&t[i][j]<='E'){ m[t[i][j]-'A'].x=i; m[t[i][j]-'A'].y=j; }//保存一下门的位置
}
}
do
{
memcpy(r,key,sizeof(r));
memset(vis,0,sizeof(vis));
DFS(s.x,s.y);
}while(check());
if(flag){ cout<<"YES\n"; }
else { cout<<"NO\n"; }
inti();
}
return 0;
}
1253:Dungeon Master_三维
#include<iostream>
#include<queue>
#include<stdio.h>
#include<cstring>
#define MAX 35
using namespace std;
int l,r,c,re;
char mep[MAX][MAX][MAX];
int vis[MAX][MAX][MAX];
int dir[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
struct node
{
int x,y,z,step;
};
struct node e,start;
bool check(node t)
{
if(t.x<0||t.x>=l||t.y<0||t.z<0||t.y>=r||t.z>=c) return true;
else if(vis[t.x][t.y][t.z]) return true;
else if(mep[t.x][t.y][t.z]=='#') return true;
return false;
}
int bfs()
{
queue<node> Q;
start.step = 0;
Q.push(start);
vis[start.x][start.y][start.z]=1;
while(!Q.empty())
{
node t=Q.front();Q.pop();
if((t.x == e.x)&&(t.y == e.y)&&(t.z == e.z)) return t.step;
for(int i=0;i<6;++i)
{
node temp;temp=t;
temp.x = t.x+dir[i][0],temp.y = t.y+dir[i][1],temp.z =t.z+ dir[i][2];
if(!check(temp))
{
vis[temp.x][temp.y][temp.z]=1;
temp.step++;
Q.push(temp);
}
}
}
return 0;
}
int main()
{
while(scanf("%d%d%d",&l,&r,&c)&&(l||r||c))
{
memset(vis,0,sizeof(vis));
memset(mep,'#',sizeof(mep));
for(int i=0;i<l;++i)
for(int j=0;j<r;++j)
{
scanf("%s",mep[i][j]);
for(int k=0;k<c;++k)
{
if(mep[i][j][k]=='E') { e.x=i;e.y=j;e.z=k; }
if(mep[i][j][k]=='S') { start.x=i;start.y=j;start.z=k; }
}
}
re = bfs();
if(re) cout<<"Escaped in "<<re<<" minute(s).\n";
else cout<<"Trapped!\n";
}
}
131:Channel Allocation
#include<iostream>
#include<string.h>
#include<string>
#include<vector>
#define MAX 27
using namespace std;
typedef struct s
{
int num;//数字
int color;//数字对应的颜色
}Color;
vector<Color>c[MAX];
Color mark[MAX];
int n=0;
string temp;
int vis[MAX];
void intial()
{
memset(vis,0,sizeof(int)*4);
memset(mark,0,sizeof(Color)*MAX);
for(int i=0;i<MAX;++i)c[i].clear();
temp="";
}
bool End()
{
for(int i=0;i<n;++i)
{
if(!mark[i].color)return false;
}
return true;
}
int max;
void dfs(int index)
{
if(index==n)return;//如果遍历完最后一个就结束
if(End())return;//是否涂色完毕
if(c[index].size()!=1)//只有头
{
int Color_num[5]={};
for(int i=1;i<c[index].size();++i)
if(mark[c[index][i].num].color)
{
Color_num[mark[c[index][i].num].color]=1;
}
//找一下周围的颜色有哪些,然后把它们去除
for(int i=1;i<=4;++i)
{
if(!Color_num[i])
{
if(End())return;
mark[c[index][0].num].color=i;//涂色
dfs(index+1);
}
}
}
else
{
if(End())return;
dfs(index+1);
}
return;
}
int main()
{
while(cin>>n&&n)
{
intial();
for(int j=0;j<n;++j)
{
cin>>temp;
Color now;now.num=temp[0]-'A';
c[j].push_back(now);//数组第一位放头
mark[now.num].num=now.num;
for(int i=2;i<temp.length();++i)
{
now.num=temp[i]-'A';
c[j].push_back(now);
}
}
dfs(0);//传入数组第一个
int count=mark[0].color;
for(int i=1;i<n;++i)
{
if(count<mark[i].color) count=mark[i].color;
}
if(count)
{
if(count==1)cout<<"1 channel needed."<<endl;
else cout<<count<<" channels needed."<<endl;
}
else
{
if(n==2)cout<<"1 channel needed."<<endl;
if(n==3)cout<<"3 channels needed."<<endl;
if(n>=4) cout<<"4 channels needed."<<endl;
}
}
return 0;
}
1388:Lake Counting
#include<iostream>
#include<string.h>
#include<string>
#include<queue>
#define MAX 102
using namespace std;
string mark[MAX];
int vis[MAX][MAX],ans,n,m,flag;
int dir[8][2]={{0,1},{0,-1},{1,0},{-1,0},{1,1},{-1,-1},{1,-1},{-1,1}};
struct node
{
int x,y;
};
bool Check(node t)
{
if(t.x<0||t.y<0||t.x>=n||t.y>=m) return false;
else if(vis[t.x][t.y]) return false;
else if(mark[t.x][t.y]=='W') return true;
else return false;
}
void bfs(node start)
{
queue<node> Q;
Q.push(start);
vis[start.x][start.y]=1;
while(!Q.empty())
{
node t = Q.front();Q.pop();
for(int i=0;i<8;++i)
{
node newt;
newt.x = t.x+dir[i][0],newt.y=t.y+dir[i][1];
if(Check(newt))
{
vis[newt.x][newt.y]=1;
mark[newt.x][newt.y]='.';
Q.push(newt);
}
}
}
}
int main()
{
cin>>n>>m;
memset(vis,0,sizeof(vis));
for(int j=0;j<n;++j) cin>>mark[j];
for(int i=0;i<n;++i)
for(int j=0;j<m;++j)
if(mark[i][j]=='W')
{
node s;ans++;
s.x=i,s.y=j;
bfs(s);
}
cout<<ans;
return 0;
}
1490:A Knight's Journey
#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<queue>
#define MAX 102
using namespace std;
struct node{
int x,y;
};
node re[MAX];//放坐标
int vis[MAX][MAX],len=1,ans,n,flag,a,b;
int dir[8][2]={{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2} };
bool Check(node t)
{
if(t.x<1||t.y<1||t.x>a||t.y>b) return false;
else if(vis[t.x][t.y]) return false;
else return true;
}
void dfs(node start,int num)
{
if(!flag)
{
re[num].x=start.x,re[num].y=start.y;
if(num==a*b)
{
flag=1;return ;
}
}
node t;
for(int i=0;i<8;++i)
{
t.x = start.x+dir[i][0],t.y = start.y+dir[i][1];
if(Check(t))
{
vis[start.x][start.y]=1;
dfs(t,num+1);
vis[start.x][start.y]=0;
}
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;++i)
{
memset(vis,0,sizeof(vis));
flag=0;
cin>>a>>b;
re[1].x=1,re[1].y=1;
cout<<"Scenario #"<<i<<":\n";
dfs(re[1],1);
if(flag)
{
for(int j=1;j<=a*b;++j) printf("%c%d",re[j].y+64,re[j].x);
cout<<"\n\n";
}
else
{
cout<<"impossible\n\n";
}
}
return 0;
}
156:LETTERS
#include<iostream>
#include<queue>
#include<string>
#define MAX 25
using namespace std;
int n,k;
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int v[MAX][MAX];
int vv[26];
string m[MAX];
int ans;
bool Check(int x,int y)
{
if(x<0||x>=n||y<0||y>=k) return false;
else if(v[x][y]) return false;
else if(vv[m[x][y]-'A']) return false;
else return true;
}
void dfs(int x,int y,int step)
{
// cout<<"\t\t\t\t>>("<<x<<","<<y<<")\n";
// for(int i=0;i<n;++i)
// {
// for(int j=0;j<k;++j)
// {
// cout<<v[i][j];
// }
// cout<<endl;
// }
if(step>ans) ans=step;
for(int i=0;i<4;++i)
{
int newx,newy;
newx = x+dir[i][0],newy = y+dir[i][1];
if(Check(newx,newy))
{
v[newx][newy]=1;
vv[m[newx][newy]-'A']=1;
step++;
dfs(newx,newy,step);
step--;
v[newx][newy]=0;
vv[m[newx][newy]-'A']=0;
}
}
// cout<<"\t\t\t\t<<("<<x<<","<<y<<")\n";
}
int main()
{
cin>>n>>k;
for(int i=0;i<n;++i)cin>>m[i];
v[0][0]=1;vv[m[0][0]-'A']=1;
dfs(0,0,1);
cout<<ans;
return 0;
}
166:The Castle
#include<iostream>
#define MAX 55
using namespace std;
int n,m,t;
int dir[4][2]={
{0,1},
{1,0},
{0,-1},
{-1,0}
};//西北东南
struct node
{
int w[4];
};
int v[MAX][MAX];
node mep[MAX][MAX];
int coun=0,maxs=1;
bool Check(int x,int y)
{
if(x<1||x>n||y<1||y>m)return false;
else if(v[x][y]) return false;
else return true;
}
int spare;
void dfs(int x,int y)
{
if(spare>maxs) maxs=spare;
// cout<<"空间大小:"<<spare<<endl;
// for(int i=1;i<=n;++i)
// {
// for(int j=1;j<=m;++j)
// {
// cout<<v[i][j];
// }
// cout<<endl;
// }
for(int i=0;i<4;++i)
{
int newx,newy;
newx=x+dir[i][0],newy=y+dir[i][1];
if(Check(newx,newy)&&!mep[newx][newy].w[i])
{
v[newx][newy]=1;spare+=1;
dfs(newx,newy);
}
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
{
cin>>t;
if(t&1) mep[i][j].w[0]=1;//西
if(t&2) mep[i][j].w[1]=1;//北
if(t&4) mep[i][j].w[2]=1;//东
if(t&8) mep[i][j].w[3]=1;//南
}
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
if(!v[i][j])
{
v[i][j]=1;
++coun;spare=1;
// cout<<" >>"<<"("<<i<<","<<j<<")"<<endl;
dfs(i,j);
// cout<<" <<"<<"("<<i<<","<<j<<")"<<endl;
}
cout<<coun<<endl<<maxs;
return 0;
}
1700:八皇后问题
#include<iostream>
#include<cmath>
#include<string.h>
using namespace std;
int n=8,tot;
int col[8]={0};
bool check(int c,int r)
{
for(int i=0;i<r;++i)
{
if(col[i]==c||(abs(col[i]-c)==abs(i-r)))
{
return false;
}
}
return true;
}
void DFS(int r)
{
if(r==n)
{
cout<<"No. "<<++tot<<endl;
int i,j;
for(i=0;i<8;++i)
{
for(j=0;j<8;++j)
{
if(i==col[j])
{
if(j==7)cout<<1;
else cout<<1<<" ";
}
else
{
if(j==7)cout<<0;
else cout<<0<<" ";
}
}
cout<<endl;
}
return;
}
for(int c=0;c<n;++c)
{
if(check(c,r))
{
col[r]=c;
DFS(r+1);
}
}
}
int main()
{
DFS(0);
return 0;
}
1756:八皇后
#include<iostream>
#include<cmath>
#include<string.h>
using namespace std;
int n=8,m,nn,tot;
int col[8]={0};
string s[93];
bool check(int c,int r)
{
for(int i=0;i<r;++i)
{
if(col[i]==c||(abs(col[i]-c)==abs(i-r)))
{
return false;
}
}
return true;
}
void DFS(int r)
{
if(r==n)
{
for(int i=0;i<8;++i)s[tot]+=col[i]+1+'0';
tot++;
// cout<<tot<<":"<<s[tot]<<endl;
return;
}
for(int c=0;c<n;++c)
{
if(check(c,r))
{
col[r]=c;
DFS(r+1);
}
}
}
int main()
{
cin>>nn;
DFS(0);
while(nn--)
{
cin>>m;
cout<<s[m-1]<<endl;
}
// for(int i=0;i<8;++i)cout<<i<<","<<col[i]<<endl;
return 0;
}
1789:算24
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
double a[5];
int v[5];
int dfs(int t)
{
if(t==4)
{
for(int i=1;i<5;++i)
{
if(v[i])continue;
if(fabs(a[i]-24.0)<1e-7) return 1;
}
}
for(int i=1;i<5;++i)
{
if(v[i])continue;
for(int j=i+1;j<5;++j)
{
if(v[j]) continue;
v[j]=1;
double aa=a[i],b=a[j];
a[i] = aa+b; if(dfs(t+1)) return 1;
a[i] = aa-b; if(dfs(t+1)) return 1;
a[i] = b-aa; if(dfs(t+1)) return 1;
a[i] = aa*b; if(dfs(t+1)) return 1;
a[i] = aa/b; if(dfs(t+1)) return 1;
a[i] = b/aa; if(dfs(t+1)) return 1;
a[i] = aa;
v[j] = 0;
}
}
return 0;
}
int main()
{
while(cin>>a[1]>>a[2]>>a[3]>>a[4]&&(a[1]||a[2]||a[3]||a[4]))
{
memset(v,0,sizeof(v));
if(dfs(1))cout<<"YES\n";
else cout<<"NO\n";
}
return 0;
}
1792:迷宫
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#define MAX 102
using namespace std;
string m[MAX];
int v[MAX][MAX],n,flag;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
struct node
{
int x,y;
};
node s,e;
bool Check(node now)
{
if(now.x<0||now.y<0||now.x>=n||now.y>=n)return false;
else if(v[now.x][now.y]) return false;
else if(m[now.x][now.y]=='#')return false;
else return true;
}
void bfs(node ss)
{
queue<node> Q;
Q.push(ss);
v[ss.x][ss.y]=1;
while(!Q.empty())
{
node f = Q.front();Q.pop();
if(f.x==e.x&&f.y==e.y){ flag=1; return ; }
if(flag) return ;
for(int i=0;i<4;++i)
{
node t;
t.x=f.x+dir[i][0],t.y=f.y+dir[i][1];
if(Check(t))
{
v[t.x][t.y]=1;
Q.push(t);
}
}
}
}
int main()
{
int k;cin>>k;
while(k--)
{
memset(v,0,sizeof(v));flag=0;
cin>>n;
for(int i=0;i<n;++i) cin>>m[i];
cin>>s.x>>s.y>>e.x>>e.y;
if(!Check(s)){ cout<<"NO\n"; continue; }
bfs(s);
if(flag)cout<<"YES\n";
else cout<<"NO\n";
}
return 0;
}
1818:红与黑
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#define MAX 102
using namespace std;
string m[MAX];
int v[MAX][MAX],w,h,flag;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
struct node
{
int x,y,c;
};
node s,e;
bool Check(node now)
{
if(now.x<0||now.y<0||now.x>=w||now.y>=h)return false;
else if(v[now.x][now.y]) return false;
else if(m[now.x][now.y]=='#')return false;
else return true;
}
void bfs(node ss)
{
queue<node> Q;
Q.push(ss);
v[ss.x][ss.y]=1;ss.c=1;
while(!Q.empty())
{
node f = Q.front();Q.pop();
for(int i=0;i<4;++i)
{
node t=f;
t.x=f.x+dir[i][0],t.y=f.y+dir[i][1];
if(Check(t))
{
v[t.x][t.y]=1;t.c++;
Q.push(t);
}
}
}
}
int main()
{
while(cin>>h>>w&&(w||h))
{
memset(v,0,sizeof(v));flag=0;
for(int i=0;i<w;++i)
{
cin>>m[i];
for(int j=0;j<h;++j)
if(m[i][j]=='@') { s.x=i,s.y=j;}
}
bfs(s);
for(int i=0;i<w;++i)for(int j=0;j<h;++j) if(v[i][j]) flag++;
cout<<flag<<"\n";
}
return 0;
}
2727:仙岛求药
#include<iostream>
#include<queue>
#include<string>
#include<cstring>
#define MAXX 10000
#define MAX 25
using namespace std;
int n,M,ans;
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int v[MAX][MAX];
string m[MAX];
struct node
{
int x,y,step;
};
node s,e,F;
bool Check(int x,int y)
{
if(x<0||x>=M||y<0||y>=n) return false;
else if(v[x][y]) return false;
else if(m[x][y]=='#') return false;
else return true;
}
void bfs(node S)
{
queue<node> Q;
Q.push(S);
v[S.x][S.y]=1;
while(!Q.empty())
{
F = Q.front();Q.pop();
// if(F.x==e.x&&F.y==e.y) cout<<"现在的步数:"<<F.step<<endl;
for(int i=0;i<4;++i)
{
node news=F;
news.x = F.x+dir[i][0],news.y = F.y+dir[i][1];
if(Check(news.x,news.y))
{
v[news.x][news.y]=1;news.step++;
if(news.x==e.x&&news.y==e.y) { ans = news.step;return; }
Q.push(news);
}
}
}
}
int main()
{
while(cin>>M>>n&&(M||n))
{
memset(v,0,sizeof(v));ans=0;
for(int i=0;i<M;++i)
{
cin>>m[i];
for(int j=0;j<n;++j)
{
if(m[i][j]=='@') s.x=i,s.y=j;
if(m[i][j]=='*') e.x=i,e.y=j;
}
}
bfs(s);
if(ans) cout<<ans<<endl;
else cout<<-1<<endl;
}
return 0;
}
2753:走迷宫
#include<iostream>
#include<queue>
#include<string>
#include<cstring>
#define MAXX 10000
#define MAX 45
using namespace std;
int R,C,ans;
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int v[MAX][MAX];
string m[MAX];
struct node
{
int x,y,step;
};
node s,e,F;
bool Check(int x,int y)
{
if(x<0||x>=R||y<0||y>=C) return false;
else if(v[x][y]) return false;
else if(m[x][y]=='#') return false;
else return true;
}
void bfs(node S)
{
queue<node> Q;
Q.push(S);
v[S.x][S.y]=1;
while(!Q.empty())
{
F = Q.front();Q.pop();
for(int i=0;i<4;++i)
{
node news=F;
news.x = F.x+dir[i][0],news.y = F.y+dir[i][1];
if(Check(news.x,news.y))
{
v[news.x][news.y]=1;news.step++;
if(news.x==e.x&&news.y==e.y) { ans = news.step;return; }
Q.push(news);
}
}
}
}
int main()
{
cin>>R>>C;
memset(v,0,sizeof(v));ans=0;
for(int i=0;i<R;++i) cin>>m[i];
s.x=0,s.y=0,e.x=R-1,e.y=C-1;
bfs(s);
cout<<ans+1<<endl;
return 0;
}
2971:抓住那头牛
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
int to[2]={1,-1};
int a,b,sum;
int vis[100000];
struct place
{
int x,time;
};
int check(place k)
{
if(k.x<0||k.x>100000||vis[k.x]==1)
return 0;
return 1;
}
int bfs(place n)
{
place m,next;
queue<place>w;
w.push(n);
while(!w.empty())
{
m=w.front();
w.pop();
if(m.x==b)
return m.time;
for(int i=0;i<2;i++)
{
next.x=m.x+to[i];
next.time=m.time+1;
if(next.x==b)
return next.time;
if(check(next))
{
w.push(next);
vis[next.x]=1;
}
}
next.x=m.x*2;
next.time=m.time+1;
if(next.x==b)
return next.time;
if(check(next))
{
w.push(next);
vis[next.x]=1;
}
}
return 0;
}
int main()
{
int i,j,t;
place x1;
while(~scanf("%d %d",&a,&b))
{
x1.x=a;
x1.time=0;
vis[x1.x]=1;
sum=0;
sum=bfs(x1);
printf("%d\n",sum);
}
return 0;
}
8465:马走日
#include<iostream>
#include<queue>
#include<string>
#include<cstring>
#define MAXX 10000
#define MAX 45
using namespace std;
int n,T,m,ans;
int dir[8][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
int v[MAX][MAX];
struct node
{
int x,y,step;
};
node s,e,F;
bool Check(int x,int y)
{
if(x<0||x>=n||y<0||y>=m) return false;
else if(v[x][y]) return false;
else return true;
}
void dfs(node S)
{
if(S.step==n*m){ ans++; return;}
for(int i=0;i<8;++i)
{
node news=S;
news.x = S.x+dir[i][0],news.y = S.y+dir[i][1];
if(Check(news.x,news.y))
{
v[news.x][news.y]=1;
news.step++;
dfs(news);
v[news.x][news.y]=0;
news.step--;
}
}
}
int main()
{
cin>>T;
while(T--)
{
cin>>n>>m>>s.x>>s.y;ans=0;
memset(v,0,sizeof(v));
v[s.x][s.y]=1;s.step=1;
dfs(s);
cout<<ans<<endl;
}
return 0;
}
6264:走出迷宫
#include<iostream>
#include<queue>
#include<string>
#include<cstring>
#define MAXX 10000
#define MAX 205
using namespace std;
int M,N,T,ans=0;
int dir[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
int v[MAX][MAX];
string m[MAX];
struct node
{
int x,y,step,t;
};
node s,e;
bool Check(node ne)
{
if(ne.x<0||ne.x>=N||ne.y<0||ne.y>=M) return false;
else if(v[ne.x][ne.y]) return false;
else if(m[ne.x][ne.y]=='#') return false;
else return true;
}
void bfs(node S)
{
S.t=T;queue<node>Q;
Q.push(S);v[S.x][S.y]=T;
while(!Q.empty())
{
node F = Q.front();Q.pop();
if(F.x==e.x&&F.y==e.y) { ans=F.step; return; }
for(int i=0;i<4;++i)
{
node ne=F;
ne.x=F.x+dir[i][0],ne.y=F.y+dir[i][1];
if(!Check(ne)) continue;
v[ne.x][ne.y]=1;
ne.step++;
Q.push(ne);
}
}
}
int main()
{
cin>>N>>M;
for(int i=0;i<N;++i)
{
cin>>m[i];
for(int j=0;j<M;++j)
{
if(m[i][j]=='S') s.x=i,s.y=j;
if(m[i][j]=='T') e.x=i,e.y=j;
}
}
bfs(s);
cout<<ans<<endl;
return 0;
}
6266取石子游戏[博弈论]
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int t;
bool dfs(int a,int b)//先手
{
if((a%b==0)||(a/b>=2)) return true;
else if(a/b==1) return !dfs(max(b,a-b),min(b,a-b));//后手
else return false;
}
int main()
{
int a,b;
while(~scanf("%d%d",&a,&b)&&(a||b))
{
if(dfs(max(a,b),min(a,b))) printf("win\n");
else printf("lose\n");
}
return 0;
}
666:放苹果[排列组合]
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int dfs(int m,int n)
{
if(m==1 || n==1) return 1;
else if(m<n) return dfs(m,m);
else if(m==n) return dfs(m,m-1)+1;
else if(m>n) return dfs(m-n,n)+dfs(m,n-1);
}
int main()
{
int a,b,t;scanf("%d",&t);
while(t--)
{
scanf("%d%d",&a,&b);
cout<<dfs(a,b)<<endl;
}
return 0;
}
7084:迷宫问题[优先队列]
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<queue>
#include<cstring>
#include<sstream>
#define MAX 100
#include<algorithm>
using namespace std;
int dir[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
struct point
{
int x,y;
};
struct node
{
int x,y,k;
point step[MAX];
bool operator < (const node&a)const{ return k>a.k; }
};
int m[MAX][MAX];
bool v[MAX][MAX];
bool Check(node s)
{
if(s.x<0||s.y<0||s.x>=5||s.y>=5) return true;
else if(v[s.x][s.y]) return true;
else if(m[s.x][s.y]) return true;
else return false;
}
void Print(node f)
{
for(int i=0;i<=f.k;++i)
cout<<"("<<f.step[i].x<<", "<<f.step[i].y<<")"<<endl;
}
void bfs(node s)
{
priority_queue<node>Q;
v[s.x][s.y]=1;s.k=0;s.step[0].x=0,s.step[0].y=0;
Q.push(s);
while(!Q.empty())
{
node f=Q.top();Q.pop();
if(f.x==4&&f.y==4) { Print(f);return;}
for(int i=0;i<4;++i)
{
node ne=f;
ne.x=f.x+dir[i][0],ne.y=f.y+dir[i][1];
if(Check(ne)) continue;
v[ne.x][ne.y]=1;
ne.k=f.k+1;
ne.step[ne.k].x=ne.x,ne.step[ne.k].y=ne.y;
Q.push(ne);
}
}
}
int main()
{
for(int i=0;i<5;++i)
for(int j=0;j<5;++j)
cin>>m[i][j];
node s;
s.x=0,s.y=0;
bfs(s);
return 0;
}
7218:献给阿尔吉侬的花束[优先队列]
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<queue>
#include<cstring>
#include<sstream>
#define MAX 205
#include<algorithm>
using namespace std;
int dir[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
struct node
{
int x,y,k;
bool operator < (const node&a)const{ return k>a.k; }
};
int R,C,ans;
node S,E;
string m[MAX];
bool v[MAX][MAX];
bool Check(node s)
{
if(s.x<0||s.y<0||s.x>=R||s.y>=C) return true;
else if(v[s.x][s.y]) return true;
else if(m[s.x][s.y]=='#') return true;
else return false;
}
void bfs(node s)
{
priority_queue<node>Q;
v[s.x][s.y]=1;
Q.push(s);
while(!Q.empty())
{
node f=Q.top();Q.pop();
// cout<<"\t\t>>"<<f.x<<","<<f.y<<endl;
if(f.x==E.x&&f.y==E.y) { ans = f.k;return;}
for(int i=0;i<4;++i)
{
node ne=f;
ne.x=f.x+dir[i][0],ne.y=f.y+dir[i][1];
if(Check(ne)) continue;
v[ne.x][ne.y]=1;
ne.k=f.k+1;
// cout<<ne.x<<","<<ne.y<<endl;
Q.push(ne);
}
}
}
int main()
{
int t;scanf("%d",&t);
while(t--)
{
cin>>R>>C;
memset(v,0,sizeof(v));ans=0;
for(int i =0;i<R;++i)
{
cin>>m[i];
for(int j=0;j<C;++j)
{
if(m[i][j]=='S') S.x=i,S.y=j;
if(m[i][j]=='E') E.x=i,E.y=j;
}
}
bfs(S);
if(ans==0) cout<<"oop!"<<endl;
else cout<<ans<<endl;
}
return 0;
}
4980:拯救行动
#include<iostream>
#include<queue>
#include<string>
#include<cstring>
#define MAXX 10000
#define MAX 220
using namespace std;
int M,N,T,ans=0;
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int v[MAX][MAX];
string m[MAX];
struct node
{
int x,y,step;
bool operator < (const node&a)const{return step>a.step;}
};
node s,e;
bool Check(node ne)
{
if(ne.x<0||ne.x>=N||ne.y<0||ne.y>=M) return false;
else if(m[ne.x][ne.y]=='#') return false;
else return true;
}
void bfs(node S)
{
priority_queue<node>Q;
Q.push(S);m[S.x][S.y]='#';
while(!Q.empty())
{
node F = Q.top();Q.pop();
for(int i=0;i<4;++i)
{
node ne=F;
ne.x=F.x+dir[i][0],ne.y=F.y+dir[i][1];
if(!Check(ne)) continue;
ne.step++;
if(m[ne.x][ne.y]=='@') Q.push(ne);
if(m[ne.x][ne.y]=='x') { ne.step++;Q.push(ne);}
if(m[ne.x][ne.y]=='a') { ans=ne.step; return; }
m[ne.x][ne.y]='#';
}
}
}
int main()
{
int SS;cin>>SS;
while(SS--)
{
memset(v,0,sizeof(v));ans=0;
cin>>N>>M;
for(int i=0;i<N;++i)
{
cin>>m[i];
for(int j=0;j<M;++j)
{
if(m[i][j]=='r') s.x=i,s.y=j;
if(m[i][j]=='a') e.x=i,e.y=j;
}
}
bfs(s);
if(ans) cout<<ans<<endl;
else cout<<"Impossible"<<endl;
}
return 0;
}
6044:鸣人和佐助
#include<iostream>
#include<queue>
#include<string>
#include<cstring>
#define MAXX 10000
#define MAX 205
using namespace std;
int M,N,T,ans=-1;
int dir[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
int v[MAX][MAX];
string m[MAX];
struct node
{
int x,y,step,t;
};
node s,e;
bool Check(node ne)
{
if(ne.x<0||ne.x>=M||ne.y<0||ne.y>=N) return false;
else if(ne.t<=v[ne.x][ne.y]) return false;
else return true;
}
void bfs(node S)
{
S.t=T;queue<node>Q;
Q.push(S);v[S.x][S.y]=T;
while(!Q.empty())
{
node F = Q.front();Q.pop();
if(F.x==e.x&&F.y==e.y) { ans=F.step; return; }
for(int i=0;i<4;++i)
{
node ne=F;
ne.x=F.x+dir[i][0],ne.y=F.y+dir[i][1];
if(!Check(ne)) continue;
if(m[ne.x][ne.y]=='#'&&ne.t>0)
{
ne.t--;ne.step++;
v[ne.x][ne.y]=ne.t;
Q.push(ne);
}
else if(m[ne.x][ne.y]=='*'||m[ne.x][ne.y]=='+')
{
v[ne.x][ne.y]=ne.t;
ne.step++;
Q.push(ne);
}
}
}
}
int main()
{
cin>>M>>N>>T;
memset(v,-1,sizeof(v));
for(int i=0;i<M;++i)
{
cin>>m[i];
for(int j=0;j<N;++j)
{
if(m[i][j]=='@') s.x=i,s.y=j;
if(m[i][j]=='+') e.x=i,e.y=j;
}
}
bfs(s);
cout<<ans<<endl;
return 0;
}
7834:分成互质组[dfs+涂色]🚩
323:棋盘问题[跟八皇后类似]🚩
#include<iostream>
#include<string>
#include<cstring>
#include<stdio.h>
#define MAX 100
using namespace std;
int n,k,ans;
string m[MAX];
int v[MAX];
void dfs(int r,int cs)//第r行,第cs个棋子
{
if(n-r<k-cs) return;
dfs(r+1,cs);//这一行放不棋子
for(int c=0;c<n;++c)//这一行放棋子
{
//如果是棋盘区并且没有放过棋子
if(m[r][c]=='#'&&!v[c])
{
if(cs==k)
{
++ans;
}
else
{
v[c]=1;
dfs(r+1,cs+1);
v[c]=0;
}
}
}
}
int main()
{
while(~scanf("%d%d",&n,&k))
{
if(n==-1&&k==-1)break;
for(int i=0;i<n;++i)cin>>m[i];
memset(v,0,sizeof(v));ans=0;
dfs(0,1);
cout<<ans<<endl;
}
return 0;
}
8783:单词接龙🚩
#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
string a[21];
int n,maxn,used[21],l=1;
void dfs(int c)//a[c]已定单词,a[i]接龙单词
{
int lc,li;
maxn=max(maxn,l);
for(int i=1;i<=n;i++)//遍历一下单词表
if(used[i]<2)
for(int j=0;j<a[c].length();j++)//将已定单词的每一个字母逐一与接龙单词
if(a[c][j]==a[i][0])//的首字母比较,从相同的地方开始接
{
lc=j;
li=0;//记录接龙起点
while(a[c][lc]==a[i][li]&&lc<a[c].length())//逐个比对,如果相同且长度没超过已定单词的长度就累加长度
{
lc++;
li++;
}
if(lc==a[c].length())//如果比对到了,就接上长度(到达目标)
{
l+=a[i].length()-li;
used[i]++;
dfs(i);
l-=a[i].length()-li;
used[i]--;
}
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
cin>>a[i];
cin>>a[n+1];
l=a[n+1].length();
dfs(n+1);//把头字母也当成一个单词,从头字母开始搜
printf("%d",maxn);
}
917:Knight Moves
#include<iostream>
#include<queue>
#include<string>
#include<cstring>
#define MAX 330
using namespace std;
int n,T,m,ans;
int dir[8][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
struct node
{
int x,y,step;
bool operator < (const node&a) const{ return step>a.step;}
};
int v[MAX][MAX];
node s,e;
bool Check(node t)
{
if(t.x<0||t.x>=n||t.y<0||t.y>=n) return false;
else if(v[t.x][t.y]) return false;
else return true;
}
void bfs(node S)
{
priority_queue<node>Q;
v[S.x][S.y]=1;
Q.push(S);
while(!Q.empty())
{
node f=Q.top();Q.pop();
if(f.x==e.x&&f.y==e.y) { ans=f.step;return; }
for(int i=0;i<8;++i)
{
node news=f;
news.x = f.x+dir[i][0],news.y = f.y+dir[i][1];
if(Check(news))
{
v[news.x][news.y]=1;
news.step=f.step+1;
Q.push(news);
}
}
}
}
int main()
{
cin>>T;
while(T--)
{
cin>>n>>s.x>>s.y>>e.x>>e.y;ans=0;
memset(v,0,sizeof(v));
if(s.x==e.x&&s.y==e.y)
{
cout<<0<<endl;
}
else
{
bfs(s);
cout<<ans<<endl;
}
}
return 0;
}
2152:Pots

浙公网安备 33010602011771号