当前位置:网站首页>[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();
}边栏推荐
- Leetcode3, implémenter strstr ()
- Leetcode skimming questions_ Invert vowels in a string
- [机缘参悟-39]:鬼谷子-第五飞箝篇 - 警示之二:赞美的六种类型,谨防享受赞美快感如同鱼儿享受诱饵。
- PHP error what is an error?
- Paddle框架:PaddleNLP概述【飛槳自然語言處理開發庫】
- 干货!通过软硬件协同设计加速稀疏神经网络
- Regular expressions: examples (1)
- 正则表达式:示例(1)
- 阿里测开面试题
- [flask] response, session and message flashing
猜你喜欢

Leetcode3, implémenter strstr ()

同一个 SqlSession 中执行两条一模一样的SQL语句查询得到的 total 数量不一样

Computer graduation design PHP enterprise staff training management system

Force buckle 1020 Number of enclaves

Basic operations of databases and tables ----- unique constraints

Redis如何实现多可用区?

500 lines of code to understand the principle of mecached cache client driver

Redis list

Blue Bridge Cup embedded_ STM32 learning_ Key_ Explain in detail

Initialize MySQL database when docker container starts
随机推荐
Folio. Ink is a free, fast and easy-to-use image sharing tool
正则表达式:示例(1)
Redis-Key的操作
Thinking about the best practice of dynamics 365 development collaboration
[flask] official tutorial -part1: project layout, application settings, definition and database access
干货!通过软硬件协同设计加速稀疏神经网络
Basic operations of database and table ----- set the fields of the table to be automatically added
Poj2315 football games
Leetcode sum of two numbers
竞价推广流程
Computer graduation design PHP enterprise staff training management system
How does redis implement multiple zones?
genius-storage使用文档,一个浏览器缓存工具
【Flask】静态文件与模板渲染
Ali test open-ended questions
Dynamics 365 开发协作最佳实践思考
Visualstudio2019 compilation configuration lastools-v2.0.0 under win10 system
Basic operations of database and table ----- delete data table
Leetcode skimming questions_ Invert vowels in a string
Computer graduation design PHP college student human resources job recruitment network