(。・∀・)ノ゙嗨飞竹小课堂第课开课喽!
咳咳,今天广搜~
咱们挑重点讲!
逃离地下城
这是经典的三维广搜,因为是长,是宽,我们可以增加一个维度,表示第几层,需要注意我们输入时,最外面一层是层数,如果你确定了的顺序,在其他地方也不要改变。
#include<bits/stdc++.h>
#define int long long
using namespace std;
char a[35][35][35];
bool vis[35][35][35];
int head=0,tail=1;
int n,m,h;
int qx,qy,qz,mx,my,mz;
struct node
{
int x,y,z,step;
}q[1000005];
bool check(int x,int y,int z)
{
if(x<1||x>n||y<1||y>m||z<1||z>h||vis[z][x][y]==true) return false;
else return true;
}
bool work(int x,int y,int z,int step)
{
if(check(x,y,z)==true)
{
head++;
q[head].x=x;
q[head].y=y;
q[head].z=z;
q[head].step=step;
vis[z][x][y]=true;
if(x==mx&&y==my&&z==mz) return true;
}
return false;
}
int bfs()
{
head++;
q[head].x=qx;
q[head].y=qy;
q[head].z=qz;
q[head].step=0;
vis[qz][qx][qy]=true;
while(head-tail+1>0)
{
int x=q[tail].x;
int y=q[tail].y;
int z=q[tail].z;
int step=q[tail].step;
tail++;
if(work(x-1,y,z,step+1)==true) return step+1;
if(work(x+1,y,z,step+1)==true) return step+1;
if(work(x,y-1,z,step+1)==true) return step+1;
if(work(x,y+1,z,step+1)==true) return step+1;
if(work(x,y,z-1,step+1)==true) return step+1;
if(work(x,y,z+1,step+1)==true) return step+1;
}
return -1;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>h>>n>>m;
for(int k=1;k<=h;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>a[k][i][j];
if(a[k][i][j]=='#') vis[k][i][j]=true;
if(a[k][i][j]=='S') qx=i,qy=j,qz=k;
if(a[k][i][j]=='E') mx=i,my=j,mz=k;
}
}
}
int ans=bfs();
if(ans==-1) cout<<"Trapped!";
else cout<<"Escaped in "<<ans<<" minute(s).";
return 0;
}爬山
这道题我们可以先,找到最近道路有几次向上几次向下,接着把所有人都试一遍,看那个人时间最少,
:
#include<bits/stdc++.h>
#define int long long
using namespace std;
char s[2005][2005];
bool vis[2005][2005];
int n,m,k,cnt=0,minx=2e9;
int u=1e9,d=1e9;
int a[1000005],b[1000005];
int head=0,tail=1;
struct node
{
int x,y,l,r;
}q[4000005];
bool check(int x,int y)
{
if(x<1||x>n||y<1||y>m||vis[x][y]==true) return false;
else return true;
}
void work(int x,int y,int l,int r)
{
if(check(x,y)==true)
{
head++;
q[head].x=x;
q[head].y=y;
q[head].l=l;
q[head].r=r;
vis[x][y]=true;
if(x==n&&y==m)
{
u=l;
d=r;
int minn=1e18,cnt=0;
for(int i=1;i<=k;i++)
{
if(u*a[i]+d*b[i]<minn)
{
minn=u*a[i]+d*b[i];
cnt=1;
}
else if(u*a[i]+d*b[i]==minn) cnt++;
}
cout<<minn<<" "<<cnt;
exit(0);
}
}
}
void bfs()
{
minx=1e18;
head++;
q[head].x=1;
q[head].y=1;
q[head].l=0;
q[head].r=0;
vis[1][1]=true;
while(head-tail+1>0)
{
int x=q[tail].x;
int y=q[tail].y;
int l=q[tail].l;
int r=q[tail].r;
tail++;
work(x-1,y,l,r+1);
work(x+1,y,l+1,r);
work(x,y-1,l,r+1);
work(x,y+1,l+1,r);
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>k;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>s[i][j];
if(s[i][j]=='X') vis[i][j]=true;
}
}
for(int l=1;l<=k;l++)
{
cin>>a[l]>>b[l];
}
bfs();
return 0;
}字母迷宫
这道题就是分别处理传送门。
可有一个特别大的坑!
如果是传送门,小竹子们肯定想的是,可广搜有一个原则,就是必须一样!
所以我们可以先把这个点延迟一下,让他一次行动不动
我们加一个判断这个点是否在延时状态,如果是,解除,否则,延时启动!
传送门就正常处理就行了
代码:
话说输出no answer 有70分,建议增加数据!
不过海洋之门要注意,他是可以穿墙的。
穿越栅栏
这道题思路不难,可格式不是一般的恶心!
首先,他第一行后双换行\n\r,难受!
其次,他的迷宫的外形是真的“优美”,很难处理,特别是出口QWQ
他的思路有两种:
① 从两个终点开始,看看跑到哪个点距离最远
② 暴力,枚举哪个点到终点最近
都可以过的QWQ
代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
string s;
bool vis[1005][1005];
string a[1005];
int n,m,maxn=-1e18;
int head=0,tail=1;
int qx,qy;
int mx1,my1,mx2,my2;
struct node
{
int x,y,step;
}q[1000005];
bool check(int x,int y)
{
if(x<1||x>n||y<1||y>m||vis[x][y]==true) return false;
return true;
}
bool work(int x,int y,int step)
{
if((x==mx1&&y==my1)||(x==mx2&&y==my2)) return true;
if(check(x,y)==true)
{
head++;
q[head].x=x;
q[head].y=y;
q[head].step=step;
vis[x][y]=true;
if((x==mx1&&y==my1)||(x==mx2&&y==my2)) return true;
}
return false;
}
int bfs()
{
head++;
q[head].x=qx;
q[head].y=qy;
q[head].step=0;
vis[qx][qy]=true;
while(head-tail+1>0)
{
int x=q[tail].x;
int y=q[tail].y;
int step=q[tail].step;
tail++;
if(work(x-1,y,step+1)==true) return step+1;
if(work(x+1,y,step+1)==true) return step+1;
if(work(x,y-1,step+1)==true) return step+1;
if(work(x,y+1,step+1)==true) return step+1;
// cout<<x<<" "<<y<<endl;
}
return -1;
}
signed main()
{
cin>>m>>n;
getchar();
getchar();
n*=2;
n++;
m*=2;
m++;
for(int i=1;i<=n;i++)
{
getline(cin,a[i]);
a[i]="#"+a[i];
for(int j=1;j<=m;j++)
{
if(a[i][j]=='+'||a[i][j]=='-'||a[i][j]=='|')
{
vis[i][j]=true;
}
else
{
if(i==1||i==n||j==1||j==m)
{
if(mx1==0&&my1==0) mx1=i,my1=j;
else mx2=i,my2=j;
}
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(a[i][j]=='+'||a[i][j]=='-'||a[i][j]=='|') continue;
qx=i,qy=j;
for(int k=1;k<=n;k++)
{
for(int l=1;l<=m;l++)
{
vis[k][l]=false;
if(a[k][l]=='+'||a[k][l]=='-'||a[k][l]=='|')
{
vis[k][l]=true;
}
}
}
head=0,tail=1;
maxn=max(maxn,bfs());
}
}
cout<<(maxn+1)/2;
return 0;
}
/*
5 3\n\r
+-+-+-+-+-+
| |
+-+ +-+ + +
| | | |
+ +-+-+ + +
| | |
+-+ +-+-+-+
*/写完了,
ヾ( ̄▽ ̄)ByeBye