题目描述
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
下图给出了一个迷宫的平面图,其中标记为 11 的为障碍,标记为 00 的为可以通行的地方。
010000
000100
001001
110000
迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这 个它的上、下、左、右四个方向之一。
对于上面的迷宫,从入口开始,可以按 DRRURRDDDR 的顺序通过迷宫, 一共 1010 步。其中 D、U、L、RD、U、L、R 分别表示向下、向上、向左、向右走。 对于下面这个更复杂的迷宫(3030 行 5050 列),请找出一种通过迷宫的方式,其使用的步数最少,在步数最少的前提下,请找出字典序最小的一个作为答案。
请注意在字典序中 D<L<R<UD<L<R<U。
01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000
运行限制
- 最大运行时间:1s
- 最大运行内存: 256M
第一次接触的搜索题是lake counting。用的是深搜。,这题是深搜和广搜都可以。实际测试了一下,本题还是bfs比较好用,毕竟有位置的优势。 以下是AC代码 BFS
#include<iostream>
#include<queue>
#include<algorithm>
#include<string>
using namespace std;
string n[31];
int vis[31][51];
int dx[4]={1,0,0,-1};
int dy[4]={0,-1,1,0};
string dir="DLRU";
struct node{
int x,y;//当前位置
string direction;//记录路径
};
int judge(int x,int y)
{
if(x>0&&x<=30&&y>=0&&y<50&&n[x][y]=='0')
return 1;
return 0;
}
void bfs()
{
queue<node>q;
q.push({1,0,""});//数组从(1,0)开始搜索
vis[1][0]=1;
while(!q.empty())
{
node now=q.front();
q.pop();
if(now.x==30&&now.y==49)
cout<<now.direction<<endl;
for(int i=0;i<4;i++)
{
int nowx=now.x+dx[i];
int nowy=now.y+dy[i];
if(vis[nowx][nowy]==1||judge(nowx,nowy)==0) {
continue;
}
// printf("n[%d][%d]=%d",nowx,nowy,(int )n[nowx][nowy]);
q.push({nowx,nowy,now.direction+dir[i]});
vis[nowx][nowy]=1;
}
}
}
int main()
{
// string n[31];
for(int i=1;i<=30;i++)
{
cin>>n[i];
}
bfs();
return 0;
}
DFS
#include<iostream>
#include<cstring>
using namespace std;
const int ax[4]={0,0,1,-1};
const int ay[4]={1,-1,0,0};
const char dir[5]={'R','L','D','U'};
int maze[60][60],mins[60][60];
char a[2000];
int best;
string ans;
bool judge(int x,int y) {//判断这个点是否越界,是否走过。
if(x>0&&x<=30&&y>0&&y<=50&&!maze[x][y])
return true;
return false;
}
void dfs(int x,int y,int pos) {
if(pos>best)
return ;
if(x==30&&y==50) {
string temp;
for(int i=1;i<pos;i++)
temp+=a[i];
if(pos<best) {//是最短的路
ans=temp;
best=pos;
}
else if(pos==best&&temp<ans) ans=temp;//在实现路时最短的同时,保证字典序最小。
return ;
}
for(int i=0;i<4;i++) {
int tempx=x+ax[i];
int tempy=y+ay[i];
if(judge(tempx,tempy)&&pos+1<=mins[tempx][tempy]) {
maze[tempx][tempy]=1;
mins[tempx][tempy]=pos+1;
a[pos]=dir[i];
dfs(tempx,tempy,pos+1);
maze[tempx][tempy]=0;//回溯找短的路,或者时字典序最小的。
}
}
}
int main()
{
memset(mins,1,sizeof(mins));
best=1<<28;
for(int i=1;i<=30;i++)
for(int j=1;j<=50;j++) {
char t;
cin>>t;//因为输入的是无空格,故将字符转换成数字
maze[i][j]=t-'0';
}
maze[1][1]=1;
dfs(1,1,1);
cout<<ans<<endl;
return 0;
}
DFS的缺点就是要剪枝,一旦不剪枝,效率就会很低,优点是思维难度比较低。
BFS的优点不用重新剪枝,搜出来的结果就是优解,适合求解至少等最优问题。
-----------------------------------------------------------------------分割线
LAKE COUNTING 描述
Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.
Given a diagram of Farmer John's field, determine how many ponds he has.
输入
-
Line 1: Two space-separated integers: N and M
-
Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.
输出
- Line 1: The number of ponds in Farmer John's field.
样例输入
10 12 W........WW. .WWW.....WWW ....WW...WW. .........WW. .........W.. ..W......W.. .W.W.....WW. W.W.W.....W. .W.W......W. ..W.......W. 样例输出
3
#include<stdio.h>
char a[101][101];
int n,m;
void ponds(int x,int y){
a[x][y]='.';
int dx,dy;
for(int i=-1;i<=1;i++){
for(int j=-1;j<=1;j++){
dx=i+x;
dy=j+y;
if(dx>=0&&dx<n&&dy>=0&&dy<m&&a[dx][dy]=='W'){
ponds(dx,dy);
}
}
}
return ;
}
int main()
{
scanf("%d %d",&n,&m);
int i,j,cnt=0;
for(i=0;i<n;i++){
scanf("%s",a[i]);
}
//printf("%d",book[1][1]);
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(a[i][j]=='W'){
ponds(i,j);
cnt++;
}
}
}
printf("%d",cnt);
return 0;
}
没学c++的时候做的这个题目,后来发现《挑战程序设计竞赛》 这本书刚好又讲到这个题,纯c手打。有点小麻烦