当前位置:网站首页>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;
}
边栏推荐
- Circular sliding auto adsorption UI tool that monkeys can use
- English notes - cause and effect
- leetcode:单调栈结构(进阶)
- 可扩展数据库(下)
- Extensible database (Part 2)
- How does the open-ended Hall current sensor help the transformation of DC power distribution?
- 物体上下漂浮工具
- Lamaba表达式学习及常用函数式接口
- Does the applet image component not display pictures?
- Huawei equipment WLAN basic service configuration command
猜你喜欢

如何系统学习一门编程语言? | 黑马程序员

数字电路学习笔记(二)

MySQL configuration of database Series F5 load balancing

English语法_形容词/副词3级-比较级_常用短语

Web APIs DOM-事件基础丨黑马程序员

Paging query optimization in MySQL of database Series

iptables防火墙规则和firewalld防火墙规则详解

荣耀v8 真机调试时不显示 Logcat 日志的解决办法

A preliminary study of blackbody radiation

WPF 下的自定义控件以及 Grid 中控件的自适应
随机推荐
Summary of SQL basic syntax for C #
友链须知
使用信号分析器
Introduction to kubernetes resource object and common commands
Learning - useful resources
Chapter 14 AC-DC power supply front stage circuit note I
A pit filling trip based on LNMP to build a personal website
可扩展数据库(下)
几个重要的物理概念
No&nbsp; result&nbsp; defined&amp; nbsp…
Simple implementation of cool GUI window based on WPF
月赛补题
[graduation season] graduate summary
如何系统学习一门编程语言? | 黑马程序员
MySQL error
继承
数据库系列之MySQL配置F5负载均衡
KVM常用命令详解
Li Kou daily question - day 29 -575 Divide candy
多线程与高并发四:VarHandle与强软弱虚引用和ThreadLocal