logo头像

学如逆水行舟,不进则退!!!

文章目录

减谈迷宫C++

本文于1032天之前发表,文中内容可能已经过时。

今天老师让做了个迷宫问题,我一看到就发现和我之前写过的一个程序是一样 的,但是在后来编写的时候有一个地方搞错了,最后下课了我还是没有正确的编写好,然后今天回来之后自己有看了一下,现在已经解决了。

#ifndef DIRECTION_H
#define DIRECTION_H
#include<iostream>
using namespace std;
struct node
{
int a;
int b;
};
class Direction
{
public:
Direction();
~Direction();
void DFS(int n, int m);
void udlr(int &n, int &m, int num);
void durl(int &n, int &m, int num);
void output();
private:
int **a;
int **visit;
node zxh[64];
int count1;
};
Direction::Direction()
{
count1 = 0;
a = new int*[8];
visit = new int*[8];
for (int i = 0; i<8; i++)
{
a[i] = new int[8];
visit[i] = new int[8];
}
for (int i = 0; i<8; i++)
{
zxh[i].a = -1;
zxh[i].b = -1;
}
int b[] = { 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 0, 1, 0,
0, 0, 0, 0, 1, 0, 1, 0,
0, 1, 0, 0, 0, 0, 1, 0,
0, 1, 0, 1, 1, 0, 1, 0,
0, 1, 0, 0, 1, 0, 0, 0,
0, 1, 0, 0, 1, 0, 0, 0,
0, 1, 1, 1, 1, 1, 1, 0 };
for (int i = 0; i<8; i++)
{
for (int j = 0; j<8; j++)
{
a[i][j] = b[i * 8 + j];
visit[i][j] = 0;
}
}
}
Direction::~Direction()
{}
void Direction::udlr(int &n, int &m, int num)
{
if (num == 1)
n--;
else if (num == 2)
n++;
else if (num == 3)
m--;
else
m++;
}//著名方向
void Direction::durl(int &n, int &m, int num)
{
if (num == 1)
n++;
else if (num == 2)
n--;
else if (num == 3)
m++;
else
m--;
}//著名方向
void Direction::output()
{
int num = 0;
while (zxh[num].a != -1)
{
if (num % 7 == 0)
cout << endl;
cout << "(" << zxh[num].a << "," << zxh[num].b << ")"<<"  ";
num++;
}
}
void Direction::DFS(int n, int m)
{
if (n == 7 && m == 7)
{
output(); cout << endl; return;
}
else
{
for (int i = 1; i < 5; i++)
{
udlr(n, m, i);
if (n >= 0 && n < 8 && m >= 0 && m < 8 && a[n][m] == 0)
{
zxh[count1].a = n;
zxh[count1].b = m;
count1++;
a[n][m] = 1;
DFS(n, m);
a[n][m] = 0;
count1--;
zxh[count1].a = -1;
zxh[count1].b = -1;
}
durl(n, m, i);
}
}
}
#endif
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include"Direction.h"
#include<iostream>
using namespace std;
int main()
{
Direction z;
z.DFS(0,0);
return 0;
}
上一篇
坚持原创技术分享,您的支持将鼓励我继续创作!