当前位置:网站首页>[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();
}
边栏推荐
- 【详细】快速实现对象映射的几种方式
- Open source | Ctrip ticket BDD UI testing framework flybirds
- Redis daemon cannot stop the solution
- [the most complete in the whole network] |mysql explain full interpretation
- Code review concerns
- Paddle framework: paddlenlp overview [propeller natural language processing development library]
- 【网络攻防实训习题】
- 通过PHP 获取身份证相关信息 获取生肖,获取星座,获取年龄,获取性别
- Selenium waiting mode
- 阿裏測開面試題
猜你喜欢
Leetcode sum of two numbers
Force buckle 9 palindromes
[detailed] several ways to quickly realize object mapping
3D视觉——4.手势识别(Gesture Recognition)入门——使用MediaPipe含单帧(Singel Frame)和实时视频(Real-Time Video)
[技术发展-28]:信息通信网大全、新的技术形态、信息通信行业高质量发展概览
Tensorflow customize the whole training process
Unity learning notes -- 2D one-way platform production method
Basic operations of databases and tables ----- default constraints
2022 PMP project management examination agile knowledge points (8)
Poj2315 football games
随机推荐
PHP campus movie website system for computer graduation design
Reasonable and sensible
国家级非遗传承人高清旺《四大美人》皮影数字藏品惊艳亮相!
【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
2022年PMP项目管理考试敏捷知识点(8)
Leetcode skimming questions_ Verify palindrome string II
NLP fourth paradigm: overview of prompt [pre train, prompt, predict] [Liu Pengfei]
[le plus complet du réseau] | interprétation complète de MySQL explicite
[flask] official tutorial -part3: blog blueprint, project installability
How does redis implement multiple zones?
02. Go language development environment configuration
剑指 Offer 12. 矩阵中的路径
Basic operations of database and table ----- set the fields of the table to be automatically added
插卡4G工业路由器充电桩智能柜专网视频监控4G转以太网转WiFi有线网速测试 软硬件定制
Computer graduation design PHP college classroom application management system
Docker compose configures MySQL and realizes remote connection
Sword finger offer 38 Arrangement of strings
How to set an alias inside a bash shell script so that is it visible from the outside?
Get the relevant information of ID card through PHP, get the zodiac, get the constellation, get the age, and get the gender
[机缘参悟-39]:鬼谷子-第五飞箝篇 - 警示之二:赞美的六种类型,谨防享受赞美快感如同鱼儿享受诱饵。