栈可以用链表来实现,表现为后进先出。具体实现就不详细说了。下面直接看两个问题:
u 进制转换:
1)进制转换是基于以下公式1:
N = (N div d) * d + N mod d (div为整除运算,mod为求余运算)
2)一个d进制数n1n2n3n4n5 可表示为 ((((n1 * d + n2 ) * d ) + n3) * d + n4) * d) + n5或者
表示为:n1*d^4 + n2*d^3 + n3*d^2 + n4*d + n5
3)递归应用公式1,即可得到d进制的数。
4)Integer temp = n % 8; 该句牵涉到java的自动装箱机制,小提一下:
-128->127之间的数装箱会自动映射到同一内存区域,所以Integer t1 = 12; Integer t2 = 12;
则t1和t2是相等的。但如果把12改成128则不等,但equals函数可以相等。
DecToOctTest.java:
package stack.application;
import java.util.*;
class DecToOct{
private Stack<Integer> convertStack;
public DecToOct(){
convertStack = new Stack<Integer>();
}
public String convert(int n){
convertStack.clear();
String res = new String();
while(n != 0){
Integer temp = n % 8;
convertStack.push(temp);
n = n /8;
}
while(!convertStack.empty()){
Integer temp = convertStack.pop();
res += temp.toString();
}
return res;
}
}
class DecToOctTest{
public static void main(String[] args){
DecToOct convertor = new DecToOct();
int i = 0;
int j = 7;
while(i < 5){
String result = convertor.convert(j + i);
System.out.println(result);
i++;
}
}
}
u 迷宫问题
迷宫问题其实就是一个深度遍历的过程,但是有一个缺点:不能得到最短路径,如果要得到最短路径可用广度,或者广度优先权遍历。
迷宫如下:maze.txt (其中-1表示墙0表示通路)
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 0 0 -1 0 0 0 -1 0 -1
-1 0 0 -1 0 0 0 -1 0 -1
-1 0 0 0 0 -1 -1 0 0 -1
-1 0 -1 -1 -1 0 0 0 0 -1
-1 0 0 0 -1 0 0 0 0 -1
-1 0 -1 0 0 0 -1 0 0 -1
-1 0 -1 -1 -1 0 -1 0 -1 -1
-1 -1 0 0 0 0 0 0 0 -1
-1 -1 0 0 0 0 0 0 0 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
(这样看很不好看,如果复制到ultraedit中就很整齐了~~)
a) 由于如果一个节点被访问,则无论如何以后都不会访问了,所以直接访问了就直接在迷宫数组中置为-1;
b) 如果运行可以发现路径是一条非常差的路径,这也表明深度遍历的缺点。在此程序则是进入下一个路径节点的顺序不好导致。如果要用则应该将while中的几个if语句的顺序修改一下
MazePath.java:
package stack.application;
import java.util.*;
import java.io.*;
class MazeNode{
public int x;
public int y;
public int dir;
public MazeNode(int x, int y){
this.x = x;
this.y = y;
dir = -1;
}
}
class Maze{
private int[][] maze = null;
private Stack<MazeNode> thePath = null;
boolean flag = false;
private void initialMaze(int[][] maze){
this.maze = maze;
this.thePath.clear();
}
public Maze(){
thePath = new Stack<MazeNode>();
}
public void toProcess(int[][] theMaze){
flag = false;
initialMaze(theMaze);
int yEnd = maze[0].length - 2;
int xEnd = maze.length - 2;
System.out.println(xEnd + " " + yEnd);
MazeNode startNode = new MazeNode(1, 1);
thePath.push(startNode);
maze[1][1] = -1; //直接置为 -1 !!!
while(!thePath.empty()){
MazeNode currentNode = thePath.peek();
if(currentNode.x == xEnd && currentNode.y == yEnd){
flag = true;
printPath();
return ;
}
else if(currentNode.dir == 3){
thePath.pop();
}
else if(currentNode.dir == -1){
int x = currentNode.x;
int y = currentNode.y -1;
currentNode.dir++;
if(maze[x][y] != -1){
MazeNode pathNode = new MazeNode(x, y);
thePath.push(pathNode);
maze[x][y] = -1; //直接置为 -1 !!!
}
}
else if(currentNode.dir == 0){
int x = currentNode.x + 1;
int y = currentNode.y;
currentNode.dir++;
if(maze[x][y] != -1){
MazeNode pathNode = new MazeNode(x, y);
thePath.push(pathNode);
maze[x][y] = -1; //直接置为 -1 !!!
}
}
else if(currentNode.dir == 1){
int x = currentNode.x;
int y = currentNode.y + 1;
currentNode.dir++;
if(maze[x][y] != -1){
MazeNode pathNode = new MazeNode(x, y);
thePath.push(pathNode);
maze[x][y] = -1; //直接置为 -1 !!!
}
}
else if(currentNode.dir == 2){
int x = currentNode.x - 1;
int y = currentNode.y;
currentNode.dir++;
if(maze[x][y] != -1){
MazeNode pathNode = new MazeNode(x, y);
thePath.push(pathNode);
maze[x][y] = -1; //直接置为 -1 !!!
}
}
}
System.out.println("could not find a path ! please check your maze,or contact with me");
}
private void printPath(){
System.out.println("find a path :");
while(!thePath.empty()){
MazeNode pathNode = thePath.pop();
System.out.println(pathNode.x + " " + pathNode.y);
}
}
}
class MazePath{
public static void main(String[] args){
try{
FileInputStream fIn = new FileInputStream("maze.txt");
BufferedReader fReader = new BufferedReader(new InputStreamReader(fIn, "utf-8"));
String str;
ArrayList<String> mazeString = new ArrayList<String>();
while((str = fReader.readLine()) != null){
mazeString.add(str);
}
String[] mazeLine = mazeString.get(0).split("//s+");
int column = mazeString.size();
int row = mazeLine.length;
int[][] theMaze = new int[column][row];
/*for(int[] col : mazeString){
for(int e : x){
e = 0;
}
}*/
/*for(int i = 0; i < column; i++){
for(int j = 0; j < row; j++){
theMaze[i][j] = 0;
}
}*/
for(int i = 0; i < column; i++){
String[] everyMazeLine = mazeString.get(i).split("//s+");
for(int j = 0; j < everyMazeLine.length; j++){
theMaze[i][j] = Integer.parseInt(everyMazeLine[j]);
System.out.print(theMaze[i][j] + " ");
}
System.out.println();
}
Maze theProcessor = new Maze();
theProcessor.toProcess(theMaze);
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
}