码迷,mamicode.com
首页 > 编程语言 > 详细

减谈迷宫C++

时间:2015-04-07 13:51:37      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:数据结构cc++   c++感悟   迷宫   管理   遍历   

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

#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;
}

减谈迷宫C++

标签:数据结构cc++   c++感悟   迷宫   管理   遍历   

原文地址:http://blog.csdn.net/u013132051/article/details/44919045

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!