java实现迷宫算法

 

package com;

 

import java.util.LinkedList;

 

public class labyrinth{

      public static void main(String[] args) { 

            String[][] sArrays={

                        {"K","S","T","Q","D","S","T","B"},

                        {"G","3","H","W","H","J","Z","L"},

                        {"D","K","S","T","Q","W","O","G"},

                        {"F","S","T","L","L","K","S","H"},

                        {"I","S","B","Q","W","S","H","A"},

                        {"J","D","V","R","I","D","L","D"},

                        {"K","E","U","I","T","N","Q","A"},

                        {"J","6","O","C","N","Y","O","V"}

            };

            getLabyrinth(sArrays, "K,3,S,L,W,D,Q,V");

            System.out.println("***************************************************");

            getLabyrinth(sArrays, "K,4,S,L,W,D,Q,V");

      }

     

      public static void getLabyrinth(String[][] arrays,String target)

      {

            String[] targetArr=target.split(",");

            //final result

            LinkedList list=new LinkedList();

            //cycle the column

            for(int i=0;i<arrays.length;i++)

            {

                  for(int j=0;j<arrays[i].length;j++)

                  {

                        if(targetArr[0].equals(arrays[i][j]))

                        {

                              //the total linkedlist

                              LinkedList allLink=new LinkedList();

                              //the list which order to find news

                              LinkedList currLink=new LinkedList();

                              //record the next cycle

                              LinkedList tmpLink=new LinkedList();

                              currLink.add(i+","+j);

                              allLink.add(currLink);

                              tmpLink.add(currLink);

                              String[] tmp=new String[targetArr.length];

                              tmp[0]=arrays[i][j];

                              //cycle the special String

                              for(int n=1;n<targetArr.length;n++)

                              {

                                   

                                    int tmpNum=tmpLink.size();

                                    if(tmpNum!=0)

                                    {

                                          tmpLink=new LinkedList();

                                    }

                                    //cycle the matching String

                                    for(int m=0;m<tmpNum;m++)

                                    {

                                          if(allLink.size()==0)

                                          {

                                                break;

                                          }

                                          currLink=(LinkedList)allLink.get(allLink.size()-1);

                                          String currValue=(String)currLink.get(currLink.size()-1);

                                          int currI=Integer.parseInt(currValue.substring(0,currValue.indexOf(",")));

                                          int currY=Integer.parseInt(currValue.substring(currValue.indexOf(",")+1,currValue.length()));

                                          //above

                                          if(currI-1>0)

                                          {

                                                if(arrays[currI-1][currY].equals(targetArr[n]))

                                                {

                                                      LinkedList tempLinkList=(LinkedList)currLink.clone();

                                                      tmpLink.add((currI-1)+","+currY);

                                                      tempLinkList.add((currI-1)+","+currY);

                                                      allLink.add(0,tempLinkList);

                                                }

                                          }

                                          //left

                                          if(currY-1>0)

                                          {

                                                if(arrays[currI][currY-1].equals(targetArr[n]))

                                                {

                                                      LinkedList tempLinkList=(LinkedList)currLink.clone();

                                                      tmpLink.add((currI)+","+(currY-1));

                                                      tempLinkList.add((currI)+","+(currY-1));

                                                      allLink.add(0,tempLinkList);

                                                }

                                          }

                                          //blow

                                          if(currI+1<arrays.length)

                                          {

                                                if(arrays[currI+1][currY].equals(targetArr[n]))

                                                {

                                                      LinkedList tempLinkList=(LinkedList)currLink.clone();

                                                      tmpLink.add((currI+1)+","+currY);

                                                      tempLinkList.add((currI+1)+","+currY);

                                                      allLink.add(0,tempLinkList);

                                                }

                                          }

                                          //right

                                          if(currY+1<arrays[i].length)

                                          {

                                                if(arrays[currI][currY+1].equals(targetArr[n]))

                                                {

                                                      LinkedList tempLinkList=(LinkedList)currLink.clone();

                                                      tmpLink.add((currI)+","+(currY+1));

                                                      tempLinkList.add((currI)+","+(currY+1));

                                                      allLink.add(0,tempLinkList);

                                                }

                                          }

                                          //top left corner

                                         

                                          if(currI-1>0&&currY-1>0)

                                          {

                                                if(arrays[currI-1][currY-1].equals(targetArr[n]))

                                                {

                                                      LinkedList tempLinkList=(LinkedList)currLink.clone();

                                                      tmpLink.add((currI-1)+","+(currY-1));

                                                      tempLinkList.add((currI-1)+","+(currY-1));

                                                      allLink.add(0,tempLinkList);

                                                }

                                               

                                          }

                                          //lower right corner

                                          if(currI+1<arrays.length&&currY+1<arrays[i].length)

                                          {

                                                if(arrays[currI+1][currY+1].equals(targetArr[n]))

                                                {

                                                      LinkedList tempLinkList=(LinkedList)currLink.clone();

                                                      tmpLink.add((currI+1)+","+(currY+1));

                                                      tempLinkList.add((currI+1)+","+(currY+1));

                                                      allLink.add(0,tempLinkList);

                                                }

                                               

                                          }

                                          //lower left corner

                                          if(currI+1<arrays.length&&currY-1>0)

                                          {

                                                if(arrays[currI+1][currY-1].equals(targetArr[n]))

                                                {

                                                      LinkedList tempLinkList=(LinkedList)currLink.clone();

                                                      tmpLink.add((currI+1)+","+(currY-1));

                                                      tempLinkList.add((currI+1)+","+(currY-1));

                                                      allLink.add(0,tempLinkList);

                                                }

                                          }

                                          //top right corner

                                          if(currI-1>0&&currY+1<arrays[i].length)

                                          {

                                                if(arrays[currI-1][currY+1].equals(targetArr[n]))

                                                {

                                                      LinkedList tempLinkList=(LinkedList)currLink.clone();

                                                      tmpLink.add((currI-1)+","+(currY+1));

                                                      tempLinkList.add((currI-1)+","+(currY+1));

                                                      allLink.add(0,tempLinkList);

                                                }

                                          }

                                          //move of the current line

                                          allLink.removeLast();

                                    }

                                   

 

                              }

                              for(int s=0;s<allLink.size();s++)

                              {

                                    list.add(allLink.get(s));

                              }

                        }

                  }

            }

           

            if(list.size()==0)

            {

                  System.out.println("There is no match String!");

            }

            else

            {

                  for(int i=0;i<list.size();i++)

                  {

                        System.out.println("The "+(i+1)+" array is:");

                        LinkedList aimList=(LinkedList)list.get(i);

                        for(int j=0;j<aimList.size();j++)

                        {

                              System.out.println(aimList.get(j)+"    "+targetArr[j]+"/n");

                        }

                  }

            }

      }

}


 

Output:

 

 

The 1 array is:

0,0    K

1,1    3

2,2    S

3,3    L

4,4    W

5,5    D

6,6    Q

7,7    V

The 2 array is:

2,1    K

1,1    3

2,2    S

3,3    L

4,4    W

5,5    D

6,6    Q

7,7    V

***************************************************

There is no match String!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值