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;

    }


}

猜你喜欢

转载自blog.csdn.net/m0_37942202/article/details/82078171
今日推荐