【kuangbin题集】专题一 简单搜索
题目链接
A - 棋盘问题
题目描述
在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。
Input
输入含有多组测试数据。
每组数据的第一行是两个正整数,n、 k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。
Output
对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证C<2^31)。
Sample Input
2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1
Sample Output
2
1
解题思路
dfs+状态压缩
把每行的可选列数压缩为一个数,一行一行dfs
,用一个数统计选过的列数,&
运算确定该行可选择的列数~
- 时间复杂度:\((n!)\)
代码
//dfs,状态压缩
#include<cstdio>
#include<cstring>
using namespace std;
int n,k,res,col;
char a[10];
int b[10];
void dfs(int pos,int chosen)
{
if(pos==n&&chosen==k)res++;
if(pos==n)return ;
if(chosen>k)return ;
dfs(pos+1,chosen);
int x=col&b[pos];
while(x)
{
col-=x&-x;
dfs(pos+1,chosen+1);
col+=x&-x;
x-=x&-x;
}
}
int main()
{
while(scanf("%d%d",&n,&k),n!=-1&&k!=-1)
{
res=0,col=(1<<n)-1;
memset(b,0,sizeof b);
for(int i=0;i<n;i++)
{
scanf("%s",a);
for(int j=0;j<n;j++)
if(a[j]=='#')b[i]+=1<<j;
}
dfs(0,0);
printf("%d\n",res);
}
return 0;
}
B - Dungeon Master
题目描述
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
解题思路
三维bfs
三维空间求解最短路,同二维空间解法~
- 时间复杂度:\(O(l\times r\times c)\)
代码
//bfs求最短路——3维空间
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
int d[35][35][35];
int l,r,c;
int sx,sy,sz,ex,ey,ez;
char maze[35][35][35];
bool v[35][35][35];
int dx[6]={-1,0,1,0,0,0},dy[6]={0,1,0,-1,0,0},dz[6]={0,0,0,0,1,-1};
struct point
{
int x,y,z;
point(int a,int b,int c):x(a),y(b),z(c){}
};
void bfs()
{
memset(v,0,sizeof v);
memset(d,0x3f,sizeof d);
queue<point> q;
q.push(point(sx,sy,sz));
v[sx][sy][sz]=true;
d[sx][sy][sz]=0;
while(q.size())
{
point t=q.front();
q.pop();
int x=t.x,y=t.y,z=t.z;
if(x==ex&&y==ey&&z==ez)return ;
for(int i=0;i<6;i++)
{
int nx=x+dx[i],ny=y+dy[i],nz=z+dz[i];
if(nz>=0&&nz<l&&nx>=0&&nx<r&&ny>=0&&ny<c&&maze[nz][nx][ny]!='#')
{
if(d[nx][ny][nz]>d[x][y][z]+1)
{
d[nx][ny][nz]=d[x][y][z]+1;
v[nx][ny][nz]=true;
q.push(point(nx,ny,nz));
}
}
}
}
}
int main()
{
while(scanf("%d%d%d",&l,&r,&c),l||r||c)
{
for(int i=0;i<l;i++)
for(int j=0;j<r;j++)
{
scanf("%s",maze[i][j]);
for(int k=0;k<c;k++)
{
if(maze[i][j][k]=='E')ex=j,ey=k,ez=i;
if(maze[i][j][k]=='S')sx=j,sy=k,sz=i;
}
}
bfs();
int res=d[ex][ey][ez];
if(res==0x3f3f3f3f)puts("Trapped!");
else
printf("Escaped in %d minute(s).\n",res);
}
return 0;
}
C - Catch That Cow
题目描述
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
- Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
- Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
Line 1: Two space-separated integers: N and K
Output
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
Sample Input
5 17
Sample Output
4
Hint
The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
解题思路
bfs
bfs变形题,bfs并不局限于上下左右前后等方向问题,是解决一种树形结构的问题的方法,本题等价于有三个子树:\(-1,+1,\times2\),bfs是三个子树同时进行,找到答案时一定时最短的~
时间复杂度不好分析,按照最坏时间复杂度的话,有:
- 时间复杂度:\(O(|n-k|)\)
代码
//bfs变形
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
int n,k;
bool v[100010];
int d[100010];
void bfs()
{
memset(d,0x3f,sizeof d);
queue<int> q;
q.push(n);
v[n]=true;
d[n]=0;
while(q.size())
{
int x=q.front();
q.pop();
if(x==k)return ;
for(int i=0;i<=2;i++)
{
int nx;
if(i==0)//-1
nx=x-1;
else if(i==1)//+1
nx=x+1;
else //*2;
nx=x*2;
if(nx>=0&&nx<=100000&&!v[nx])
{
if(d[nx]>d[x]+1)
{
d[nx]=d[x]+1;
v[nx]=true;
q.push(nx);
}
}
}
}
}
int main()
{
scanf("%d%d",&n,&k);
bfs();
printf("%d",d[k]);
return 0;
}
E - Find The Multiple
题目描述
Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.
Input
The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.
Output
For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.
Sample Input
2
6
19
0
Sample Output
10
100100100100100100
111111111111111111
解题思路
要求能整除 \(n\) 的01表示十进制,同时,位数不超过100位,暴力dfs
的话,复杂度为 \(O(2^{100})\),肯定不行,使用bfs
可以不用考虑深度问题,但到多少位时才能有答案?\(n\leq 200\),数据比较小,可以通过bfs
打表判断是否结果会爆long long
,很幸运,没有。所以,为方便,我们可以使用dfs
,确定深度为long long
最高位~
- 时间复杂度:\((2^{18})\)
代码
//dfs变形
#include<cstdio>
using namespace std;
typedef long long LL;
int n;
bool f;
bool dfs(int cnt,LL num)
{
if(f)return true;
if(cnt>19)return false;
if(num%n==0)
{
printf("%lld\n",num);
f=true;
return true;
}
if(dfs(cnt+1,num*10))return true;
if(dfs(cnt+1,num*10+1))return true;
return false;
}
int main()
{
while(scanf("%d",&n),n)
{
f=false;
dfs(1,1);
}
return 0;
}
J - 迷宫问题
题目描述
定义一个二维数组:
int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
Input
一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。
Output
左上角到右下角的最短路径,格式如样例所示。
Sample Input
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
Sample Output
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
解题思路
bfs
bfs求最短路,可以存下当前最短路的前一个位置,最后从终点逆推到起点~
- 时间复杂度:\(O(n^2)\)
代码
//bfs求最短路径
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
using namespace std;
int a[5][5];
bool v[5][5];
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
int d[5][5];
pair<int,int> last[5][5];
vector<pair<int,int>>res;
void bfs(int sx,int sy)
{
memset(d,0x3f,sizeof d);
queue<pair<int,int>> q;
q.push(make_pair(sx,sy));
v[sx][sy]=true;
d[sx][sy]=0;
while(q.size())
{
pair<int,int> t=q.front();
q.pop();
int x=t.first,y=t.second;
if(x==4&&y==4)return ;
for(int i=0;i<4;i++)
{
int nx=x+dx[i],ny=y+dy[i];
if(nx>=0&&nx<=4&&ny>=0&&ny<=4&&!v[nx][ny]&&a[nx][ny]==0)
{
if(d[nx][ny]>d[x][y]+1)
{
d[nx][ny]=d[x][y]+1;
last[nx][ny]=make_pair(x,y);
v[nx][ny]=true;
q.push(make_pair(nx,ny));
}
}
}
}
}
int main()
{
for(int i=0;i<=4;i++)
for(int j=0;j<=4;j++)scanf("%d",&a[i][j]);
bfs(0,0);
res.push_back(make_pair(4,4));
int x=last[4][4].first,y=last[4][4].second;
while(!(x==0&&y==0))
{
res.push_back(make_pair(x,y));
pair<int,int> t=last[x][y];
x=t.first,y=t.second;
}
res.push_back(make_pair(0,0));
for(int i=res.size()-1;~i;i--)
printf("(%d, %d)\n",res[i].first,res[i].second);
return 0;
}