概述
搜索
例题
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!
大致题意
三维迷宫,.可行走#不可行走S起点E终点,可以上下左右前后六个方向行走,求最短到达时间(步数)。
解题思路
类似二维bfs,在搜索时搜索六个方向即可。
#include <cstdio>
#include <iostream>
#include <queue>
#define N 40000
using namespace std;
char a[40][40][40];
queue<int>b;
char c[40][40][40]={0};
int d[40][40][40]={0};
void enqueue(int x,int y,int z)
{
b.push(x);
b.push(y);
b.push(z);
}
void dequeue(int &x,int &y,int &z)
{
x=b.front();
b.pop();
y=b.front();
b.pop();
z=b.front();
b.pop();
}
int change(int i,int j,int k,int `在这里插入代码片`dd)
{
if(!c[i][j][k])
{
if(a[i][j][k]=='E')
{
printf("Escaped in %d minute(s).n",dd+1);
return 1;
}
c[i][j][k]++;
d[i][j][k]=dd+1;
enqueue(i,j,k);
}
return 0;
}
int x,y,z;
int getdis(int i,int j,int k)
{
int b=0;
if(i<x-1&&a[i+1][j][k]!='#') b+=change(i+1,j,k,d[i][j][k]);
if(i>0&&a[i-1][j][k]!='#') b+=change(i-1,j,k,d[i][j][k]);
if(j<y-1&&a[i][j+1][k]!='#') b+=change(i,j+1,k,d[i][j][k]);
if(j>0&&a[i][j-1][k]!='#') b+=change(i,j-1,k,d[i][j][k]);
if(k<z-1&&a[i][j][k+1]!='#') b+=change(i,j,k+1,d[i][j][k]);
if(k>0&&a[i][j][k-1]!='#') b+=change(i,j,k-1,d[i][j][k]);
return b;
}
int main()
{
while(1)
{
scanf("%d%d%d",&x,&y,&z);
getchar();
if(x==0&&y==0&&z==0) return 0;
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
gets(a[i][j]);
}
if(i<x-1) getchar();
}
for(int i=0;i<x;i++)
for(int j=0;j<y;j++)
for(int k=0;k<z;k++)
{
c[i][j][k]=d[i][j][k]=0;
if(a[i][j][k]=='S')
{
enqueue(i,j,k);
c[i][j][k]=1;
}
}
int flag=0;
while(b.size())
{
int i,j,k;
dequeue(i,j,k);
flag=getdis(i,j,k);
if(flag) break;
c[i][j][k]++;
}
if(!flag) printf("Trapped!n");
while(b.size())
b.pop();
}
return 0;
}
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.
大致题意
从n到k,可以选择左移、右移、翻倍三种移动方案,求最小移动次数。
解题思路
一维,考虑左移、右移、翻倍三种移动方案,注意防止因队列内元素过多RE或MLE
#include <cstdio>
#include <iostream>
#include <queue>
#include <cstdlib>
#define N 400005
using namespace std;
queue<int>b;
char c[N]={0};
int d[N]={0};
int n,k;
void dequeue(int &x)
{
x=b.front();
b.pop();
}
void change(int i,int dd)
{
if(!c[i])
{
if(i==k)
{
printf("%d",dd+1+i-k);
exit(0);
}
c[i]++;
d[i]=dd+1;
b.push(i);
}
}
void getdis(int i)
{
if(i*2>=N) return;
if(i>0) change(i-1,d[i]);
change(i+1,d[i]);
change(i*2,d[i]);
}
int main()
{
scanf("%d%d",&n,&k);
if(n>=k) {printf("%d",n-k);return 0;}
b.push(n);
c[n]=1;
while(b.size())
{
int i;
dequeue(i);
getdis(i);
c[i]++;
}
return 0;
}
最后
以上就是踏实大雁为你收集整理的搜索例题搜索例题的全部内容,希望文章能够帮你解决搜索例题搜索例题所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复