栈实现迷宫问题

很久没写代码了,主要是现在学了一下JAVA,顺便把上课的内容实现一下。栈实现迷宫问题也就是栈实现dfs,主要是顺便把路径记录在栈里,所以稍微有些复杂,下面是代码:

JAVA实现:

package com.stack;

import java.util.Stack;

class node{
	int x,y,dir;
	node(int x,int y,int dir){
		this.x=x;
		this.y=y;
		this.dir=dir;
	}
}

public class StackDfs{
	private static int[][] move={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
	public static int path(int[][] maze,Stack<node> s){
		node temp=new node(1,1,-1);//一开始标记为-1,方便后面+1后从0开始
		s.push(temp);
		while(!s.isEmpty()){
			node now=s.peek();
			int x=now.x,y=now.y,dir=now.dir+1;//注意这里dir+1,因为回溯后从下一个方向开始
			while(dir<8){					  //dir之前的方向之前已经处理过了
				int xx=x+move[dir][0];
				int yy=y+move[dir][1];
				if(maze[xx][yy]==0){
					temp=new node(xx,yy,dir);
					s.push(temp);
					x=xx;
					y=yy;
					maze[x][y]=-1;
					if(x==6&&y==8){
						return 1;
					}else{
						dir=0;//前进一步变化方向从0开始
					}
				}else{
					dir++;//变化方向为下一个
				}
			}
			s.pop();//没有路了就回溯
		}
		return 0;
	}
	public static void main(String[] args){
		int maze[][]={{1,1,1,1,1,1,1,1,1,1},
					{1,0,1,1,1,0,1,1,1,1},
					{1,1,0,1,0,1,1,1,1,1},
					{1,0,1,0,0,0,0,0,1,1},
					{1,0,1,1,1,0,1,1,1,1},
					{1,1,0,0,1,1,0,0,0,1},
					{1,0,1,1,0,0,1,1,0,1},
					{1,1,1,1,1,1,1,1,1,1}};
		Stack<node> s=new Stack<node>();
		int ans=path(maze,s);
		while(!s.isEmpty()){
			node temp=s.pop();
			System.out.println(temp.x+"  "+temp.y+"  "+temp.dir);
		}
	}
}

C++实现:

#include<iostream>
#include<cstdio>
#include<string>
#include<regex>
#include<iomanip>
#include<iterator>
#include<random>
#include<ctime>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
const int MAX = 50;
int maze[8][10] = { { 1,1,1,1,1,1,1,1,1,1 },
		{ 1,0,1,1,1,0,1,1,1,1 },
		{ 1,1,0,1,0,1,1,1,1,1 },
		{ 1,0,1,0,0,0,0,0,1,1 },
		{ 1,0,1,1,1,0,1,1,1,1 },
		{ 1,1,0,0,1,1,0,0,0,1 },
		{ 1,0,1,1,0,0,1,1,0,1 },
		{ 1,1,1,1,1,1,1,1,1,1 } };
int mov[8][2] = { { 0,1 },{ 1,1 },{ 1,0 },{ 1,-1 },{ 0,-1 },{ -1,-1 },{ -1,0 },{ -1,1 } };

typedef struct node {
	int x, y;
	int dir;
};
stack<node> S;

bool bound(int x, int y) {
	if (x < 0 || x >= MAX || y < 0 && y >= MAX)
		return false;
	return true;
}

bool dfs(int sx, int sy,int ex,int ey) {
	S.push(node{ sx,sy,-1});
	while (!S.empty()) {
		node p = S.top();
		int x = p.x, y = p.y,dir=p.dir+1;
		while (dir < 8) {
			int xx = x + mov[dir][0];
			int yy = y + mov[dir][1];
			if (maze[xx][yy] == 0) {
				S.push(node{ xx,yy,dir });
				x = xx;
				y = yy;
				maze[x][y] = -1;
				if (x == ex&&y == ey) {
					return true;
				}
				else {
					dir = 0;
				}
			}
			else {
				dir++;
			}
		}
		S.pop();
	}
	return false;
}

int main() {
	dfs(1, 1, 6, 8);
	while(!S.empty()){
		node temp = S.top(); S.pop();
		cout << temp.x << "   " << temp.y << "  " << temp.dir << endl;
	}
	return 0;
}


转载于:https://www.cnblogs.com/seasonal/p/10343643.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值