当前位置:网站首页>[width first search] Ji Suan Ke: Suan tou Jun goes home (BFS with conditions)
[width first search] Ji Suan Ke: Suan tou Jun goes home (BFS with conditions)
2022-07-06 01:57:00 【muse_ age】

8 10
P.####.#P#
..#..#...#
..#T##.#.#
..........
..##.#####
..........
#####...##
###....S##
21
It is generally used to find the shortest number of steps BFS:
Conditional BFS:
Starting from S, The finish for T, When you reach the end, judge whether you have passed P( If two conditions are met at the same time, return )
Tag arrays use three-dimensional :
vis[x][y][flag]
vis One more dimension indicates whether the key has been obtained .
If the terminal is reached and the state of obtaining the key is marked ,bfs end .
You can actually walk twice at the same point , The first time was when I didn't get the key , The second time was when I got the key ?
Code :
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<cstring>
using namespace std;
int n,m;
vector<string>G;
bool vis[2001][2001][2];
struct state{
int x,y;
int step;
bool flag;
state(int _x,int _y,int _step,bool _flag){
x=_x;
y=_y;
step=_step;
flag=_flag;
}
};
void input(){
cin>>n>>m;
for(int i=0;i<n;i++){
string s;
cin>>s;
G.push_back(s);
}
memset(vis,false,sizeof(vis));
}
void bfs(int x,int y){
queue<state>q;
q.push(state(x,y,0,false));
while(!q.empty()){
state cur=q.front();
q.pop();
int x=cur.x;
int y=cur.y;
int step=cur.step;
bool flag=cur.flag;
if(G[x][y]=='P'){
flag=true;
}
if(G[x][y]=='T'&&flag==true){
cout<<" They count :"<<step<<endl;
return;
}
vis[x][y][flag]=true;
if(x+1<n&&!vis[x+1][y][flag]&&G[x+1][y]!='#'){
vis[x+1][y][flag]=true;
q.push(state(x+1,y,step+1,flag));
}
if(x-1>=0&&!vis[x-1][y][flag]&&G[x-1][y]!='#'){
vis[x-1][y][flag]=true;
q.push(state(x-1,y,step+1,flag));
}
if(y-1>=0&&!vis[x][y-1][flag]&&G[x][y-1]!='#'){
vis[x][y-1][flag]=true;
q.push(state(x,y-1,step+1,flag));
}
if(y+1<m&&!vis[x][y+1][flag]&&G[x][y+1]!='#'){
vis[x][y+1][flag]=true;
q.push(state(x,y+1,step+1,flag));
}
}
}
void output(){
for(int i=0;i<n;i++){
cout<<G[i]<<endl;
}
}
void solve(){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(G[i][j]=='S'){
bfs(i,j);
return;
}
}
}
}
int main(){
input();
solve();
}边栏推荐
- Campus second-hand transaction based on wechat applet
- leetcode3、实现 strStr()
- 竞赛题 2022-6-26
- [understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.
- Tensorflow customize the whole training process
- Cookie concept, basic use, principle, details and Chinese transmission
- leetcode-2.回文判断
- Visualstudio2019 compilation configuration lastools-v2.0.0 under win10 system
- Folio. Ink is a free, fast and easy-to-use image sharing tool
- Sword finger offer 38 Arrangement of strings
猜你喜欢

Docker compose configures MySQL and realizes remote connection

02.Go语言开发环境配置

Computer graduation design PHP enterprise staff training management system
![[flask] official tutorial -part2: Blueprint - view, template, static file](/img/bd/a736d45d7154119e75428f227af202.png)
[flask] official tutorial -part2: Blueprint - view, template, static file

Redis string type

Basic operations of databases and tables ----- non empty constraints

Blue Bridge Cup embedded_ STM32 learning_ Key_ Explain in detail

NiO related knowledge (II)

Kubernetes stateless application expansion and contraction capacity

Computer graduation design PHP college student human resources job recruitment network
随机推荐
leetcode-2. Palindrome judgment
Mongodb problem set
Computer graduation design PHP enterprise staff training management system
selenium 元素定位(2)
Executing two identical SQL statements in the same sqlsession will result in different total numbers
module ‘tensorflow. contrib. data‘ has no attribute ‘dataset
Pangolin Library: subgraph
Basic operations of databases and tables ----- primary key constraints
leetcode-两数之和
NLP fourth paradigm: overview of prompt [pre train, prompt, predict] [Liu Pengfei]
Apicloud openframe realizes the transfer and return of parameters to the previous page - basic improvement
Blue Bridge Cup embedded_ STM32_ New project file_ Explain in detail
Redis daemon cannot stop the solution
Cadre du Paddle: aperçu du paddlelnp [bibliothèque de développement pour le traitement du langage naturel des rames volantes]
Paddle框架:PaddleNLP概述【飛槳自然語言處理開發庫】
[Jiudu OJ 09] two points to find student information
genius-storage使用文档,一个浏览器缓存工具
02.Go语言开发环境配置
[network attack and defense training exercises]
500 lines of code to understand the principle of mecached cache client driver