杭电pj备战专题-2

搜索专题,略微有一点难度

T1 胜利大逃亡

一个普通的三维BFS,基本上模板即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<bits/stdc++.h>
using namespace std;
int K,p;
int a,b,c,T;
int ans;
int nx,ny,nz;
int val[100][100][100];
struct dir
{
int fx,fy,fz;
}d[10]={{0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}};

struct node
{
int x,y,z,step;
};

int bfs()
{
queue<node> q;
node a1;
a1.x=a1.y=a1.z=1;
a1.step=0;
q.push(a1);
val[1][1][1]=1;
while(q.size())
{
node b1=q.front();
q.pop();
for(int i=0;i<6;i++)
{
nx=b1.x+d[i].fx;
ny=b1.y+d[i].fy;
nz=b1.z+d[i].fz;
if(!val[nx][ny][nz])
{
if(nx==a&&ny==b&&nz==c)
{
return ++b1.step;
}
if(b1.step<T)
{
val[nx][ny][nz]=1;
node c1;
c1.x=nx;
c1.y=ny;
c1.z=nz;
c1.step=b1.step+1;
q.push(c1);
}
}
}
}
return -1;
}
int main()
{
scanf("%d",&K);
while(K--)
{
memset(val,1,sizeof(val));
scanf("%d%d%d%d",&a,&b,&c,&T);
for(int i=1;i<=a;i++)
{
for(int j=1;j<=b;j++)
{
for(int k=1;k<=c;k++)
{
scanf("%d",&val[i][j][k]);
}
}
}
ans=bfs();
printf("%d\n",ans);
}
}

T2 拯救公主

类似于模板,把关于传输机的想清楚就行

坑点:如果其实走到了一个时空传输机,而它的对应位置还是传输机,就会卡在里面!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<bits/stdc++.h>
using namespace std;
int K,p;
int a,b,c,T;
int ans;
int nx,ny,nz;
int sx,sy,sz;
char val[15][15][10];
struct dir
{
int fx,fy;
}d[10]={{0,1},{0,-1},{1,0},{-1,0}};

struct node
{
int x,y,z,step;
};

int bfs()
{
queue<node> q;
node a1;
a1.x=sx;
a1.y=sy;
a1.z=sz;
a1.step=0;
q.push(a1);
val[sx][sy][sz]='*';
while(q.size())
{
node b1=q.front();
q.pop();
for(int i=0;i<4;i++)
{
nx=b1.x+d[i].fx;
ny=b1.y+d[i].fy;
nz=b1.z;
if(val[nx][ny][nz]=='#')
{
nz=3-nz;
}
if(val[nx][ny][nz]!='*')
{
if(val[nx][ny][nz]=='P')
{
return ++b1.step;
}
if(b1.step<T)
{
val[nx][ny][nz]='*';
node c1;
c1.x=nx;
c1.y=ny;
c1.z=nz;
c1.step=b1.step+1;
q.push(c1);
}
}
}
}
return 10000000;
}
int main()
{
scanf("%d",&K);
while(K--)
{
memset(val,'*',sizeof(val));
scanf("%d%d%d",&a,&b,&T);
getchar();
for(int i=1;i<=a;i++)
{
for(int j=1;j<=b;j++)
{
scanf("%c",&val[i][j][1]);
if(val[i][j][1]=='S')
{
sx=i;
sy=j;
sz=1;
}
}
getchar();
}
getchar();
for(int i=1;i<=a;i++)
{
for(int j=1;j<=b;j++)
{
scanf("%c",&val[i][j][2]);
}
getchar();
}
for(int i=1;i<=2;i++)
{
for(int j=1;j<=a;j++)
{
for(int k=1;k<=b;k++)
{
if(val[j][k][i]=='#'&&val[j][k][3-i]=='#')
{
val[j][k][i]=val[j][k][3-i]='*';
}
}
}
}
ans=bfs();
//cout<<ans<<endl;
if(ans<=T)printf("YES\n");
else printf("NO\n");
}
}

T3 纸牌游戏

比较难,考点是BFS+位运算,细节有点麻烦

我的思路是先把字符串(二进制)转成一个整数,然后用异或来实现翻牌,注意翻最边上的时候是只翻两张,所以异或3或是与1…1,其余异或7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<bits/stdc++.h>
using namespace std;
bool a[10000000];
string b;
int len,sum,ans;
struct node
{
int x,y;
};
int bfs()
{
memset(a,0,sizeof(a));
queue<node> q;
node a1;
a1.y=0;
a1.x=sum;
q.push(a1);
while(q.size())
{
node a2;
a2=q.front();
q.pop();
if(a2.x==0)return a2.y;
for(int i=0;i<len;i++)
{
a1=a2;
if(i==0)a1.x^=3;
else a1.x^=(7<<(i-1));
a1.x&=(1<<len)-1;
if(a[a1.x])continue;
a[a1.x]=1;
a1.y++;
q.push(a1);
}
}
return -1;
}
int main()
{
while(cin>>b)
{
len=b.length();
sum=0;
for(int i=0;i<len;i++)
{
sum=sum*2+b[i]-'0';
}
ans=bfs();
if(ans==-1)cout<<"NO\n";
else cout<<ans<<endl;
}
}

T4 转弯限制

稍微改一下,注意while循环里的内容以及访问过了可以再走

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<bits/stdc++.h>
using namespace std;
int K,p;
int a,b,T;
int ans;
int nx,ny;
int sx,sy;
int wx,wy;
char val[105][105];
bool vis[105][105];
struct dir
{
int fx,fy;
}d[10]={{0,1},{0,-1},{1,0},{-1,0}};

struct node
{
int x,y,step;
};

int bfs()
{
queue<node> q;
memset(vis,0,sizeof(vis));
node a1;
a1.x=sx;
a1.y=sy;
a1.step=-1;
q.push(a1);
vis[sx][sy]=1;
node c1;
while(q.size())
{
node b1=q.front();
q.pop();
if(b1.x==wx&&b1.y==wy&&b1.step<=K)
{
return b1.step;
}
for(int i=0;i<4;i++)
{
c1.x=b1.x+d[i].fx;
c1.y=b1.y+d[i].fy;
while(val[c1.x][c1.y]!='*')
{
if(vis[c1.x][c1.y]==0)
{
vis[c1.x][c1.y]=1;
c1.step=b1.step+1;
q.push(c1);
}
c1.x+=d[i].fx;
c1.y+=d[i].fy;
}

}
}
return 10000000;
}
int main()
{
cin>>T;
while(T--)
{
memset(val,'*',sizeof(val));
memset(vis,0,sizeof(vis));
cin>>a>>b;
for(int i=1;i<=a;i++)
{
for(int j=1;j<=b;j++)
{
cin>>val[j][i];
}
}
cin>>K>>sx>>sy>>wx>>wy;
ans=bfs();
if(ans<=K)cout<<"yes\n";
else cout<<"no\n";
}
}

T5 丁爸又双叒叕被抓走了

典型的用基于优先队列(记得重载运算符)的BFS,就是用来反过来搜的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include<bits/stdc++.h>
using namespace std;
int K,p;
int a,b,c,T;
int ans;
int sx,sy;
int nx,ny,nz;
char val[205][205];
struct dir
{
int fx,fy;
}d[10]={{0,1},{0,-1},{1,0},{-1,0}};

struct node
{
int x,y,step;
friend bool operator < (const node &a,const node &b)
{
return a.step>b.step;
}
};

int bfs()
{
priority_queue<node> q;
node a1;
a1.x=sx;
a1.y=sy;
a1.step=0;
q.push(a1);
val[sx][sy]='#';
while(q.size())
{
node b1=q.top();
q.pop();
//cout<<b1.x<<" "<<b1.y<<" "<<b1.step<<endl;
for(int i=0;i<4;i++)
{
nx=b1.x+d[i].fx;
ny=b1.y+d[i].fy;
if(val[nx][ny]!='#')
{
if(val[nx][ny]=='r')
{
return ++b1.step;
}
if(val[nx][ny]=='.')
{
val[nx][ny]='#';
node c1;
c1.x=nx;
c1.y=ny;
c1.step=b1.step+1;
q.push(c1);
}
if(val[nx][ny]=='x')
{
val[nx][ny]='#';
node c1;
c1.x=nx;
c1.y=ny;
c1.step=b1.step+2;
q.push(c1);
}
}
}
}
return -1;
}
int main()
{
while(cin>>a>>b)
{
memset(val,'#',sizeof(val));
for(int i=1;i<=a;i++)
{
for(int j=1;j<=b;j++)
{
cin>>val[i][j];
if(val[i][j]=='a')
{
sx=i;
sy=j;
}
}
}
ans=bfs();
if(ans==-1)cout<<"Poor Dingba has to stay in the prison all his life.\n";
else cout<<ans<<endl;
}
}

T6 张晨乐的迷宫

DFS即可,为了不TLE要加奇偶性剪枝和一个常规判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<bits/stdc++.h>
using namespace std;
int n,m,T,sx,sy,fx,fy,ans,cnt;
int qx[5]={0,0,1,-1},qy[5]={1,-1,0,0};
char mapp[10][10];
bool vis[10][10];
void dfs(int x,int y,int t)
{
if(x==fx&&y==fy&&t==T)ans=1;
if(ans)return;
int tmp=(T-t)-abs(x-fx)-abs(y-fy);
if(tmp<0||tmp%2==1)return;
for(int i=0;i<4;i++)
{
int nx=x+qx[i],ny=y+qy[i];
if(nx<=n&&nx>=1&&ny<=m&&ny>=1&&mapp[nx][ny]!='X'&&!vis[nx][ny])
{
vis[nx][ny]=1;
dfs(nx,ny,t+1);
vis[nx][ny]=0;
}
}
}
int main()
{
while(cin>>n>>m>>T&&n!=0&&m!=0&&T!=0)
{
cnt=ans=0;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>mapp[i][j];
if(mapp[i][j]=='S')sx=i,sy=j;
else if(mapp[i][j]=='D')fx=i,fy=j;
else if(mapp[i][j]=='X')cnt++;
}
}
if(n*m-cnt<=T)
{
cout<<"NO\n";
continue;
}
vis[sx][sy]=1;
dfs(sx,sy,0);
if(!ans)cout<<"NO\n";
else cout<<"YES\n";
}
}

T7 胖鼠

记忆化DFS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<bits/stdc++.h>
using namespace std;
int n,k;
int mapp[105][105];
int maxx[105][105];
int qx[5]={0,0,1,-1};
int qy[5]={1,-1,0,0};
int dfs(int x,int y)
{
if(maxx[x][y])return maxx[x][y];
int ans=0;
for(int i=0;i<4;i++)
{
for(int j=1;j<=k;j++)
{
int nx=x+qx[i]*j;
int ny=y+qy[i]*j;
if(mapp[nx][ny]>mapp[x][y]&&nx<=n&&ny<=n&&nx>=1&&ny>=1)
ans=max(ans,dfs(nx,ny));
}
}
return maxx[x][y]=mapp[x][y]+ans;
}
int main()
{
while(cin>>n>>k&&n!=-1&&k!=-1)
{
memset(mapp,-1,sizeof(mapp));
memset(maxx,0,sizeof(maxx));
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin>>mapp[i][j];
dfs(1,1);
cout<<maxx[1][1]<<endl;
}
}

T8 How many ways

还是记忆化DFS,突发奇想用DP做了一下其实是记忆化DFS写炸了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<bits/stdc++.h>
using namespace std;
int t,n,m;
int ma[105][105];
int dp[105][105];
int main()
{
cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>ma[i][j];
dp[i][j]=-1000000;
}
}
dp[1][1]=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(dp[i][j]>=0)
{
for(int i1=i;i1<=n&&i1-i<=ma[i][j];i1++)
{
for(int j1=j;j1<=m&&i1-i+j1-j<=ma[i][j];j1++)
{
if(i1==i&&j1==j)continue;
if(dp[i1][j1]<0)dp[i1][j1]=0;
dp[i1][j1]+=dp[i][j];
dp[i1][j1]%=10000;
}
}
}
}
}
if(dp[n][m]<0)
{
cout<<0<<endl;
}
else
{
cout<<dp[n][m]<<endl;
}
}
}