当前位置:网站首页>Backtracking maze problem
Backtracking maze problem
2022-06-28 03:56:00 【I am already furious!】
to flash back
Examples of data structures
Ask to get out of the maze , The maze is as follows
0,0,0,0,0,0,0,0,0,0
0,1,1,0,1,1,1,0,1,0
0,1,1,0,1,1,1,0,1,0
0,1,1,1,1,0,0,1,1,0
0,1,0,0,0,1,1,1,1,0
0,1,1,1,0,1,1,1,1,0
0,1,0,1,1,1,0,1,1,0
0,1,0,0,0,1,0,0,1,0
0,0,1,1,1,1,1,1,1,0
0,0,0,0,0,0,0,0,0,0
here 0 Representative wall ,1 Represents the road that can be taken .
It can be used DFS and BFS Write , But I won't hahaha .
Here is the code
#include<iostream>
using namespace std;
#define M 100
#define MAX 0x3f3f3f3f
typedef long long ll;
#define bug(a) cout<<endl<<"*"<<a<<endl;
int arr[10][10] = {
0,0,0,0,0,0,0,0,0,0,//0
0,1,1,0,1,1,1,0,1,0,//1
0,1,1,0,1,1,1,0,1,0,//2
0,1,1,1,1,0,0,1,1,0,//3
0,1,0,0,0,1,1,1,1,0,//4
0,1,1,1,0,1,1,1,1,0,//5
0,1,0,1,1,1,0,1,1,0,//6
0,1,0,0,0,1,0,0,1,0,//7
0,0,1,1,1,1,1,1,1,0,//8
0,0,0,0,0,0,0,0,0,0 };//9
int dic[4][2]={
1,0,0,1,-1,0,0,-1};// Coordinates of movement in four directions
int brr[10][10];
typedef struct node1
{
int h, l;
}qo;
typedef struct node
{
int ord;
qo* seat;// coordinate
qo* base;// Bottom
int di;// Direction
}stack;
void newstack(stack& s) {
s.base = new qo[M];// Both need to create space
s.seat = new qo[M];// perhaps s.seat Do not create space s.seat=s.base It's OK
if (!s.base) exit(0);
if (!s.seat) exit(0);
}
void push(stack& s, qo& b) {
// Put the value on the stack
*(s.seat++) = b;
s.ord++;
}
void pop(stack& s) {
// Pop up elements , But it doesn't return a value
if (s.seat == s.base)exit(0);
s.ord--;
*(s.seat--);
}
void popp(stack& s, qo& a2) {
// Pop up the element and bring it out
if (s.ord==0)exit(0);
s.ord--;
*(s.seat--);
a2 = *s.seat;
}
int main()
{
int ans1 = 8,flag=0;//8 Because the exit is in the coordinates 8,8 The location of
stack s;
s.ord = 0;// Remember to assign values to this tag , Otherwise, no output
qo a1;
newstack(s);// Create space , Don't forget
a1.h = 1;a1.l = 1;
brr[1][1] = 1;// The road signs we have passed 1, The starting position is also marked
push(s, a1);
while (1) {
if (a1.h == ans1 && a1.l == ans1) {
flag = 1; break; }// Export order flag=0
if (s.ord==0)break;// An empty stack means there is no way to the exit , Just jump out
if (arr[a1.h + dic[0][0]][a1.l + dic[0][1]] != 0&&brr[a1.h + dic[0][0]][a1.l + dic[0][1]]==0) {
a1.h += dic[0][0];
a1.l += dic[0][1];
brr[a1.h][a1.l] = 1;
push(s, a1);
}
else {
int i;
for (i = 1; i <= 3; i++) {
if (arr[a1.h + dic[i][0]][a1.l + dic[i][1]] != 0 && brr[a1.h + dic[i][0]][a1.l + dic[i][1]] == 0) {
a1.h += dic[i][0];
a1.l += dic[i][1];
brr[a1.h][a1.l] = 1;
push(s, a1);
break;
}
}
if (i > 3) {
// It means 4 Neither direction satisfies the condition , To pop up a , Equivalent to backtracking
pop(s);
a1 = *--s.seat;
*(s.seat++) = a1;// This means that a value will pop up for a2 however a2 Again
brr[a1.h][a1.l] = 1;// Giving it back is equivalent to not playing it out but giving it a value
}
}
}
if (!flag)cout << "eror" << endl;// have no way out
else if (flag) {
// There is a way
qo a2;
memset(brr, 0, sizeof(brr));// Clear the road you have walked before 0
int k = s.ord;
for (int j = 1; j <= k; j++) {
popp(s, a2);
brr[a2.h][a2.l] = 1; A road sign that will lead to the end 1
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (brr[i][j] == 1)cout << "@" << ' ';// The way out is used @ Mark ;
else cout << "#" << ' ';
}
cout << endl;
}
}
return 0;
}
边栏推荐
- 数据库系列之InnoDB中在线DDL实现机制
- Extensible database (Part 2)
- MySQL configuration of database Series F5 load balancing
- 从遇见大咖到成为大咖,昇腾AI开发者创享日给开发者带来无限可能
- TypeScript 联合类型
- 英语小记 - 表因果
- Typescript union type
- 力扣每日一题-第29天-219.存在重复元素Ⅱ
- Resource management, high availability and automation (medium)
- Does the applet input box flash?
猜你喜欢

Does the applet image component not display pictures?

数字电路学习笔记(二)

Go speed

Scalable storage system (I)

A solution to the inefficiency of setting debug mode in developing flask framework with pychar

自用工具 猴子都会用的unity视频播放器
![[graduation season] graduate summary](/img/f6/59134c1dbf70fc809652d925fd528f.jpg)
[graduation season] graduate summary

多线程与高并发四:VarHandle与强软弱虚引用和ThreadLocal

Pycharm setting pseudo sublime color scheme

"9 No" principle and "5 measurement dimensions" of extensible system
随机推荐
多线程与高并发三:AQS底层源码分析及其实现类
Understanding and learning of parental delegation mechanism
「运维有小邓」监控文件及文件夹变更
MySQL error
云应用、服务的“5层”架构
Extensible database (I)
资源管理、高可用与自动化(下)
月赛补题
力扣每日一题-第29天-1491.去掉最低工资和最高工资后的平均工资
数据库系列之MySQL中的执行计划
Sublime text 3 basic configuration tutorial
ambari SSLError: Failed to connect. Please check openssl library versions.
What is the difference between slice and array in go question bank 12?
Li Kou daily question - day 29 -523 Count odd numbers in interval range
Execution plan in MySQL of database Series
leetcode:494.数组中添加加减运算符得到指定值的所有方法
【毕业季】研究生の毕业总结
Lost connection repair: make "hide and seek" nowhere to hide
密码加密md5和加盐处理
谈云原生,不得不谈的容器