java用二维数组做的迷宫_迷宫问题,二维数组模拟迷宫;

// Datastructure2.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include "iostream"

using namespace std;

static int w;

static int h;

static int column = 0;

static int row = 0;

static int **A;//声明二级指针A,指向一个指针数组

//***************************************************************************

//顺序栈实现

//***************************************************************************

template

class ArrayStack

{

private:

int size;//栈的大小

int tos;//栈顶索引

T*contain;

public:

ArrayStack():size(0),tos(-1),contain(NULL){}

ArrayStack(int maxsize);

~ArrayStack();

void Push(const T&element);

T& GetTop();

T&Pop();

bool IsEmpty();

void MakeEmpty();

};

template

ArrayStack::ArrayStack(int maxsize)

{

size=maxsize;

tos=-1;

contain=new T[maxsize];

}

template

ArrayStack::~ArrayStack()

{

delete contain;

tos=-1;

size=0;

}

template

void ArrayStack::Push(const T&element)

{

if(tos==size-1)

{

//cout<

return;

}

else

{

tos++;

contain[tos]=element;

}

}

template

T& ArrayStack::GetTop()

{

return contain[tos];

}

template

T& ArrayStack::Pop()

{

if(tos==-1)

{

int i=0;

//cout<

return contain[tos--];

}

else

{

return contain[tos--];

}

}

template

bool ArrayStack::IsEmpty()

{

if(tos==-1)

return true;

else

return false;

}

template

void ArrayStack::MakeEmpty()

{

delete contain;

tos=-1;

size=0;

}

bool Right(int column,int row){

if (column + 1 < w) {

if (A[column + 1][row] == 0) {

return true;

} else {

return false;

}

} else {

return false;

}

}

bool Left(int column,int row){

if (column - 1 >= 0) {

if (A[column - 1][row] == 0) {

return true;

} else {

return false;

}

} else {

return false;

}

}

bool Down(int column,int row){

if (row + 1 < h) {

if (A[column][row + 1] == 0) {

return true;

} else {

return false;

}

} else {

return false;

}

}

bool Up(int column,int row){

if (row - 1 >= 0) {

if (A[column][row - 1] == 0) {

return true;

} else {

return false;

}

} else {

return false;

}

}

bool isPassway(){

ArrayStack*point=new ArrayStack(w*h);

point->Push(column * 100 + row * 10 + 0);

if (Right(column, row)) {

column = column + 1;

int data = column * 100 + row * 10 + 6;

point->Push(data);

} else {

if (Down(column, row)) {

row = row + 1;

point->Push(column * 100 + row * 10 + 2);

} else {

cout<

return false;

}

}

int count = 0;

while (!(column == (w - 1) && row == (h - 1))) {

int temp;

if(point->IsEmpty()){

cout<

return false;

}else{

temp = point->GetTop();

}

int turn = temp % 10;

if (turn == 6) {

if (Right(column, row)) {

column = column + 1;

point->Push(column * 100 + row * 10 + 6);

count++;

} else {

if (Down(column, row)) {

row = row + 1;

point->Push(column * 100 + row * 10 + 2);

count++;

} else {

if (Up(column, row)) {

row = row - 1;

point->Push(column * 100 + row * 10 + 8);

count++;

} else {

column = column - 1;

if(point->IsEmpty()){

cout<

return false;

}else{

point->Pop();

point->Pop();

point->Push(column * 100 + row * 10 + 4);

}

}

}

}

} else {

if (turn == 2) {

if (Right(column, row)) {

column = column + 1;

point->Push(column * 100 + row * 10 + 6);

count++;

} else {

if (Down(column, row)) {

row = row + 1;

point->Push(column * 100 + row * 10 + 2);

count++;

} else {

if (Left(column, row)) {

column = column - 1;

point->Push(column * 100 + row * 10 + 4);

count++;

} else {

row = row - 1;

if(point->IsEmpty()){

cout<

return false;

}else{

point->Pop();

point->Pop();

point->Push(column * 100 + row * 10 + 8);

}

}

}

}

} else {

if (turn == 4) {

if (Down(column, row)) {

row = row + 1;

point->Push(column * 100 + row * 10 + 2);

count++;

} else {

if (Left(column, row)) {

column = column - 1;

point->Push(column * 100 + row * 10 + 4);

count++;

} else {

if (Up(column, row)) {

row = row - 1;

point->Push(column * 100 + row * 10 + 8);

count++;

} else {

column = column + 1;

if(point->IsEmpty()){

cout<

return false;

}else{

point->Pop();

point->Pop();

point->Push(column * 100 + row * 10 + 6);

}

}

}

}

} else {

if (Right(column, row)) {

column = column + 1;

point->Push(column * 100 + row * 10 + 6);

count++;

} else {

if (Left(column, row)) {

column = column - 1;

point->Push(column * 100 + row * 10 + 4);

count++;

} else {

if (Up(column, row)) {

row = row - 1;

point->Push(column * 100 + row * 10 + 8);

count++;

} else {

row = row + 1;

if(point->IsEmpty()){

cout<

return false;

}else{

point->Pop();

point->Pop();

point->Push(column * 100 + row * 10 + 2);

}

}

}

}

}

}

}

if (count >= (w * h)) {

cout<

column = 0;

row = 0;

point->MakeEmpty();

return false;

}

}

column = 0;

row = 0;

point->MakeEmpty();

return true;

}

void Step(){

ArrayStack*point=new ArrayStack(w*h);

cout<

point->Push(column * 100 + row * 10 + 0);

if (Right(column, row)) {

column = column + 1;

point->Push(column * 100 + row * 10 + 6);

cout<

} else {

if (Down(column, row)) {

row = row + 1;

point->Push(column * 100 + row * 10 + 2);

cout<

} else {

cout<

}

}

while (!(column == (w - 1) && row == (h - 1))) {

int temp = point->Pop();

point->Push(temp);

int turn = temp % 10;

if (turn == 6) {

if (Right(column, row)) {

column = column + 1;

point->Push(column * 100 + row * 10 + 6);

cout<

} else {

if (Down(column, row)) {

row = row + 1;

point->Push(column * 100 + row * 10 + 2);

cout<

} else {

if (Up(column, row)) {

row = row - 1;

point->Push(column * 100 + row * 10 + 8);

cout<

} else {

column = column - 1;

point->Pop();

point->Pop();

point->Push(column * 100 + row * 10 + 4);

cout<

}

}

}

} else {

if (turn == 2) {

if (Right(column, row)) {

column = column + 1;

point->Push(column * 100 + row * 10 + 6);

cout<

} else {

if (Down(column, row)) {

row = row + 1;

point->Push(column * 100 + row * 10 + 2);

cout<

} else {

if (Left(column, row)) {

column = column - 1;

point->Push(column * 100 + row * 10 + 4);

cout<

} else {

row = row - 1;

point->Pop();

point->Pop();

point->Push(column * 100 + row * 10 + 8);

cout<

<

}

}

}

} else {

if (turn == 4) {

if (Down(column, row)) {

row = row + 1;

point->Push(column * 100 + row * 10 + 2);

cout<

} else {

if (Left(column, row)) {

column = column - 1;

point->Push(column * 100 + row * 10 + 4);

cout<

} else {

if (Up(column, row)) {

row = row - 1;

point->Push(column * 100 + row * 10 + 8);

cout<

} else {

column = column + 1;

point->Pop();

point->Pop();

point->Push(column * 100 + row * 10 + 6);

cout<

<

}

}

}

} else {

if (Right(column, row)) {

column = column + 1;

point->Push(column * 100 + row * 10 + 6);

cout<

} else {

if (Left(column, row)) {

column = column - 1;

point->Push(column * 100 + row * 10 + 4);

cout<

} else {

if (Up(column, row)) {

row = row - 1;

point->Push(column * 100 + row * 10 + 8);

cout<

} else {

row = row + 1;

point->Pop();

point->Pop();

point->Push(column * 100 + row * 10 + 2);

cout<

<

}

}

}

}

}

}

}

}

int _tmain(int argc, _TCHAR* argv[])

{

cout<

cin>>w;

cout<

cin>>h;

A = new int *[w]; //开辟指针数组

for(int i=0; i

cout<

for (int i = 0; i < h; i++) {

cout<

char input[10];

cin>>input;

for (int j = 0; j < w; j++) {

if(input[j]==48){

A[j][i]=0;

}else{

A[j][i]=1;

}

}

}

cout<

cout<

for (int i = 0; i < h; i++) {

for (int j = 0; j < w; j++) {

cout<

}

cout<

}

cout<

if(isPassway()){

Step();

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值