java数据结构-递归算法之迷宫问题

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


public class migongTest {
	public static void main(String[] args) throws IOException
	{
		Mazz mz=new Mazz("C:/1.txt");
		if(mz.search(1))
		{
			System.out.println();
			System.out.println("successfully!");
		}
		else
			System.out.println("failed!!!");
	}
}

class StreetPort
{
	int center;
	int left;
	int right;
	int front;
	public StreetPort(int center,int left, int right, int front) {
		super();
		this.center=center;
		this.left = left;
		this.right = right;
		this.front = front;
	}
}

class Mazz
{
	int exit;
	StreetPort[] sp;
	int portSize;
	
	Mazz(String filepath) throws IOException
	{
		Initiate(filepath);
	}
	
	public void Initiate(String filepath) throws IOException
	{
		BufferedReader br=new BufferedReader(new FileReader(filepath));
		String line1=br.readLine();
		this.portSize=new Integer(line1.trim());
		this.sp=new StreetPort[portSize];
		for(int i=0;i<portSize;i++)
		{
			String s=br.readLine().trim();
			String[] str=s.split(" ");
			Integer c=new Integer(str[0]);
			Integer l=new Integer(str[1]);
			Integer r=new Integer(str[2]);
			Integer f=new Integer(str[3]);
			StreetPort subsp=new StreetPort(c,l,r,f);
			sp[i]=subsp;
		}
		this.exit=new Integer(br.readLine().trim()).intValue();
		br.close();
	}
	
	public boolean search(int currentPort)
	{
		if(currentPort!=0)
		{
			if(currentPort==this.exit)
			{
				System.out.print(currentPort+"<--");
				return true;
			}
			else if(search(sp[currentPort-1].left))
			{
				System.out.print(currentPort+"<--");
				return true;
			}
			else if(search(sp[currentPort-1].front))
			{
				System.out.print(currentPort+"<--");
				return true;
			}
			else if(search(sp[currentPort-1].right))
			{
				System.out.print(currentPort+"<--");
				return true;
			}
			
			return false;
		}
		else
			return false;
		
	}
	
	
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值