当前位置:网站首页>All in one 1251 - Fairy Island for medicine (breadth first search)
All in one 1251 - Fairy Island for medicine (breadth first search)
2022-07-27 08:14:00 【Bamboo monk-】
subject
Original link
http://ybt.ssoier.cn:8088/problem_show.php?pid=1251
【 Title Description 】
Young Li Xiaoyao's aunt is ill , Wang Xiaohu introduced him to Xianling island , Ask fairy sister for fairy pill to save my aunt . Rebellious but filial Li Xiaoyao broke into Xianling island , Overcome thousands of risks and difficulties and come to the center of the island , I found that the magic medicine was placed in the depths of the maze . Maze by M×N Square composition , Some squares contain monsters that can instantly kill Li Xiaoyao , And some squares are safe . Now Li Xiaoyao wants to find the magic medicine as soon as possible , Obviously he should avoid squares with monsters , And through the least squares , And there will be mysterious people waiting for him . Now I ask you to help him achieve this goal .
The figure below It shows an example of a maze and Li Xiaoyao's route to find the magic medicine .

【 Input 】
There are multiple sets of test data entered . Each set of test data is represented by two non-zero integers M and N Start , Neither is greater than 20.M Indicates the number of rows in the maze , N Indicates the number of maze Columns . Next there is M That's ok , Each row contains N Characters , Different characters represent different meanings :
1)‘@’: The position of young Li Xiaoyao ;
2)‘.’: A square that allows safe passage ;
3)‘#’: A square with monsters ;
4)‘*’: The location of the elixir .
When two zeros are read in a line , End of input .
【 Output 】
For each group of test data , Output one line each , This line contains the minimum number of squares that Li Xiaoyao needs to go through to find the magic medicine ( The count includes the initial position of the box ). If he can't find the magic medicine , The output -1.
【 sample input 】
8 8
[email protected]##...#
#....#.#
#.#.##..
..#.###.
#.#...#.
..###.#.
...#.*..
.#...###
6 5
.*.#.
.#...
..##.
.....
.#...
[email protected]
9 6
.#..#.
.#.*.#
.####.
..#...
..#...
..#...
..#...
#[email protected]##
.#..#.
0 0【 sample output 】
10
8
-1 Examine the subject
This problem seems to be solved by deep search , But deep search can only judge whether it can reach , Cannot judge the minimum number of steps
This problem can be said to be in addition to cells ( See blog all in one 1329 cells ) Another broad search template question besides
step
1. Defining variables 、 Structure
struct node{
int x,y,s;
}; // There must be a semicolon here
int mk[25][25];// Tag array
char s[25][25];// Map
int n,m;
bool flag;// The variable to judge whether it can be reached
int xy[4][2]={
{1,0},{-1,0},{0,1},{0,-1}};// Offset
int res;// Shortest path
int sx,sy;2. Define search (BFS) function
void bfs(int x,int y)
{
mk[x][y]=1;// The passing mark is 1
queue<node>q;// Define the queue
q.push((node){x,y,0});
while(q.size()) // As long as the queue is not empty, continue to judge
{
node t=q.front();// Take out the team leader
q.pop();// Delete team head
for(int i=0;i<4;i++)
{
int xx=t.x+xy[i][0],yy=t.y+xy[i][1]; // Where to go
if(xx>=1 && xx<=n &&yy>=1&&yy<=m&&!mk[xx][yy]&&s[xx][yy]!='#') // If you can walk to
{
if(s[xx][yy]=='*'){// Judge whether it is a fairy medicine
flag=1;// Indicates that
res=t.s+1;
return;// return
}
mk[xx][yy]=1;// Otherwise, mark the place you have passed as 1
q.push((node){xx,yy,t.s+1});// Join the team where you are going
}
}
}
}3. The main function
while(1)// Multiple sets of data input
{
cin>>n>>m;
memset(mk,0,sizeof(mk));// Because there are many groups , Initialize every time
flag=0;
if(n==0 && m==0) break;// If both inputs are 0, End of input
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++) // Repeat input
{
cin>>s[i][j];
if(s[i][j]=='@') sx=i,sy=j; // Mark the starting point
}
}
bfs(sx,sy);// Start searching from the beginning
if(flag) cout<<res<<endl; // If flag It's true , Output steps
else cout<<"-1"<<endl;// Otherwise output -1
}
return 0;Take a look at the complete code :
#include<bits/stdc++.h>
using namespace std;
struct node{
int x,y,s;
}; // There must be a semicolon here
int mk[25][25];// Tag array
char s[25][25];// Map
int n,m;
bool flag;// The variable to judge whether it can be reached
int xy[4][2]={
{1,0},{-1,0},{0,1},{0,-1}};// Offset
int res;// Shortest path
int sx,sy;
void bfs(int x,int y)
{
mk[x][y]=1;// The passing mark is 1
queue<node>q;// Define the queue
q.push((node){x,y,0});
while(q.size()) // As long as the queue is not empty, continue to judge
{
node t=q.front();// Take out the team leader
q.pop();// Delete team head
for(int i=0;i<4;i++)
{
int xx=t.x+xy[i][0],yy=t.y+xy[i][1]; // Where to go
if(xx>=1 && xx<=n &&yy>=1&&yy<=m&&!mk[xx][yy]&&s[xx][yy]!='#') // If you can walk to
{
if(s[xx][yy]=='*'){// Judge whether it is a fairy medicine
flag=1;// Indicates that
res=t.s+1;
return;// return
}
mk[xx][yy]=1;// Otherwise, mark the place you have passed as 1
q.push((node){xx,yy,t.s+1});// Join the team where you are going
}
}
}
}
int main()
{
while(1)// Multiple sets of data input
{
cin>>n>>m;
memset(mk,0,sizeof(mk));// Because there are many groups , Initialize every time
flag=0;
if(n==0 && m==0) break;// If both inputs are 0, End of input
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++) // Repeat input
{
cin>>s[i][j];
if(s[i][j]=='@') sx=i,sy=j; // Mark the starting point
}
}
bfs(sx,sy);// Start searching from the beginning
if(flag) cout<<res<<endl; // If flag It's true , Output steps
else cout<<"-1"<<endl;// Otherwise output -1
}
return 0;
}边栏推荐
- Grandson's questions are difficult, and his son's invigilation is strict. I can't do it. Pay back my school money
- "Intermediate and advanced test questions": what is the implementation principle of mvcc?
- Digital transformation driven by enterprise architecture!
- 如何获取广告服务流量变现数据,助力广告效果分析?
- Is redis really slowing down?
- 3D laser slam: Interpretation of logo-loam paper --- Abstract
- Use of string type "PHP Basics"
- Lua stateful iterator
- Prevent cookies from modifying ID to cheat login
- Netdata 性能监测工具介绍、安装、使用
猜你喜欢
![[ciscn2019 southeast China division]web11 1](/img/94/61ad4f6cbbd46ff66f361462983d7a.png)
[ciscn2019 southeast China division]web11 1

SETTA 2020 国际学术会议即将召开,欢迎大家参加!

redis配置文件下载

Demo:st05 find text ID information

QT creator code style plug-in beautifier

Comprehensive cases

Use of string type "PHP Basics"

DEMO:ST05 找文本ID 信息
![[target detection] yolov6 theoretical interpretation + practical test visdrone data set](/img/ad/78835eea4decc15e0981e6561b875f.png)
[target detection] yolov6 theoretical interpretation + practical test visdrone data set

Gossip: is rotting meat in the pot to protect students' rights and interests?
随机推荐
Shenzhi Kalan Temple
Data extraction 2
C event usage case subscription event+=
ERP生产作业控制 华夏
情人节,我用字符画出了一个对象!
Attack and defense world MFW
"PHP Basics" PHP statements and statement blocks
Lua stateful iterator
信息化项目风险控制与应用
[applet] how to get wechat applet code upload key?
【目标检测】YOLOv6理论解读+实践测试VisDrone数据集
Internet of things industrial UART serial port to WiFi to wired network port to Ethernet Gateway WiFi module selection
"Intermediate and advanced test questions": what is the implementation principle of mvcc?
Digital transformation driven by enterprise architecture!
Use of string type "PHP Basics"
Usage scenarios for automated testing
QT creator code style plug-in beautifier
QingChuang technology joined dragon lizard community to build a new ecosystem of intelligent operation and maintenance platform
What are the software tuning methods? Let's see what Feiteng technology experts say about dragon lizard technology
想让照片中的云飘起来?视频编辑服务一键动效3步就能实现