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!