当前位置:网站首页>(BFS) template + example (maze, eight digits)
(BFS) template + example (maze, eight digits)
2022-07-23 15:41:00 【Salted egg_ dd】
Labyrinth
Given a n×mn×m A two-dimensional array of integers , Used to represent a maze , The array contains only 00 or 11, among 00 Means the way to go ,11 Indicates an impassable wall .
first , There is a man in the upper left corner (1,1)(1,1) It's about , It is known that the person can go up every time 、 Next 、 Left 、 Move a position in any right direction .
Excuse me, , The person moves from the upper left corner to the lower right corner (n,m)(n,m) It's about , At least how many times you need to move .
Data assurance (1,1)(1,1) Place and (n,m)(n,m) The number at is 00, And there must be at least one path .
Input format
The first line contains two integers nn and mm.
Next nn That's ok , Each row contains mm It's an integer (00 or 11), Represents a complete two-dimensional array .
Output format
Output an integer , Represents the minimum number of moves from the upper left corner to the lower right corner .
Data range
1≤n,m≤1001≤n,m≤100
sample input :
5 5 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0sample output :
8#include <bits/stdc++.h>
using namespace std;
queue <pair<int,int>> q;
const int N=110;
int mp[N][N];
int d[N][N];
int n,m;
int bfs()
{
memset(d,-1,sizeof(d));
d[1][1]=0;
q.push({1,1});
int dx[]={-1,0,0,1};
int dy[]={0,1,-1,0};
while(!q.empty())
{
for(int i=0;i<=3;i++)
{
auto t=q.front();
int x=t.first+dx[i],y=t.second+dy[i];
if(x>=1&&x<=n&&y>=1&&y<=m&&d[x][y]==-1&&mp[x][y]==0)
{
mp[x][y]=0;
d[x][y]=d[t.first][t.second]+1;
q.push({x,y});
}
}
q.pop();
}
return d[n][m];
}
int main()
{
int i,j;
scanf("%d %d",&n,&m);
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%d",&mp[i][j]);
}
}
printf("%d",bfs());
return 0;
}
2. Eight digital
In a 3×33×3 In the grid ,1∼81∼8 this 88 A number and a
xIt happens to be distributed here without weight or leakage 3×33×3 In the grid .for example :
1 2 3 x 4 6 7 5 8During the game , You can put
xAbove it 、 Next 、 Left 、 Digital exchange in one of the four right directions ( If there is ).Our goal is to exchange , Make the grid arranged as follows ( It is called correct arrangement ):
1 2 3 4 5 6 7 8 xfor example , In the example, the graph can be created by making
xAnd right 、 Next 、 The numbers in the right three directions are exchanged successfully and arranged correctly .The exchange process is as follows :
1 2 3 1 2 3 1 2 3 1 2 3 x 4 6 4 x 6 4 5 6 4 5 6 7 5 8 7 5 8 7 x 8 7 8 xNow? , Give you an initial grid , Please find out at least how many exchanges are needed to get the correct arrangement .
Input format
Input takes up a line , take 3×33×3 The initial grid of .
for example , If the initial mesh is as follows :
1 2 3 x 4 6 7 5 8Then enter :
1 2 3 x 4 6 7 5 8Output format
Output takes up one line , Contains an integer , Indicates the minimum number of exchanges .
If no solution exists , The output −1−1.
sample input :
2 3 4 1 5 x 7 6 8sample output
19
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <queue>
using namespace std;
int bfs(string state)
{
queue<string> q;
unordered_map<string, int> d;
q.push(state);
d[state] = 0;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
string end = "12345678x";
while (q.size())
{
auto t = q.front();
q.pop();
if (t == end) return d[t];
int distance = d[t];
int k = t.find('x');
int x = k / 3, y = k % 3;
for (int i = 0; i < 4; i ++ )
{
int a = x + dx[i], b = y + dy[i];
if (a >= 0 && a < 3 && b >= 0 && b < 3)
{
swap(t[a * 3 + b], t[k]);
if (!d.count(t))
{
d[t] = distance + 1;
q.push(t);
}
swap(t[a * 3 + b], t[k]);
}
}
}
return -1;
}
int main()
{
char s[2];
string state;
for (int i = 0; i < 9; i ++ )
{
cin >> s;
state += *s;
}
cout << bfs(state) << endl;
return 0;
}边栏推荐
- [heuristic divide and conquer] the inverse idea of heuristic merging
- bgp基本配置
- Time series data in industrial Internet of things
- Exploration and practice of Ali multimodal knowledge atlas
- 什么是真正的 HTAP ?(二)挑战篇
- 【OpenCV 例程200篇】225. 特征提取之傅里叶描述子
- Idea starts multiple projects at once
- Linux: analysis of the basic use of vim editor
- select......for update 语句的功能是什么? 会锁表还是锁行?
- C语言经典例题-将输入的两位数转成英文
猜你喜欢

Safe operation 7.22

安全7.18作业
![[200 opencv routines] 225. Fourier descriptor for feature extraction](/img/4b/1f373505ffd5c0dbaa5c20431c4b42.png)
[200 opencv routines] 225. Fourier descriptor for feature extraction

Idea five free plug-ins to improve efficiency

CBOC signal modulation and demodulation simulation based on MATLAB, output its correlation, power spectrum and frequency offset tracking

第二篇 如何设计一个RBAC权限系统

地图附近名片流量主小程序开发

Exploration and practice of Ali multimodal knowledge atlas

用rpm -e --nodeps进行批量删除

Can multithreading optimize program performance?
随机推荐
Open source quadruped robot with design drawings and code "suggestions collection"
Part III detailed explanation of RBAC authority management database design
Deep understanding of CAS (spin lock)
select......for update 语句的功能是什么? 会锁表还是锁行?
Shell script case ---3
[ctfhub] the data of JWT header and payload are transmitted in clear text. If sensitive information is contained in it, sensitive information will be leaked. Try to find the flag. Format is flag{}
Find a specific number in an ordered array (binary search or half search)
Completely uninstall MySQL in centos7
7.13web safety operation
Clickhouse, let the query fly!!!
第一篇 项目基本情况介绍
3D数学 - 矢量
奔驰新能源产品线:豪华新能源市场或将改变格局
centos7 中彻底卸载mysql
Six ways of uniapp route jump
Idea starts multiple projects at once
开源四足机器人 附设计图及代码「建议收藏」
【云原生】docker环境中安装mysql、redis服务
C语言经典例题-switch case语句转换日期格式
airserver在哪里下载?使用方法教程