Skip to content

Commit

Permalink
#4 add HanMoTa problem
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaTianliang committed Oct 23, 2016
1 parent d578066 commit 9065e93
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/main/java/dp/HanMoTa.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package dp;

/**
* Created by tianliangxia on 16-10-23.
*/

/***
* 汗莫塔问题
* 给定三个柱子 src mid des
* 输出将n个圆盘从src移动到des上的路径
*/

class StackElement{
public String src, mid, des;
public int n;
public StackElement(int n, String src, String mid, String des){
this.n = n;
this.src = src;
this.mid = mid;
this.des = des;
}
}

public class HanMoTa {
public static void solve(int n, StringBuilder stringBuilder, String src, String mid, String des){
if(stringBuilder == null)
throw new NullPointerException("stringBuilder is null");
if(n == 1){
stringBuilder.append(src+"->"+des+",");
return;
}

solve(n-1, stringBuilder, src, des, mid);
stringBuilder.append(src+"->"+des+",");
solve(n-1, stringBuilder,mid, src, des);
}

public static void solveWithStack(int n, StringBuilder stringBuilder, String src, String mid, String des){
if(stringBuilder == null)
throw new NullPointerException("stringBuilder is null");

MyStack<StackElement> stack = new MyStack<>();
stack.push(new StackElement(n, src, mid, des));
StackElement element;
while (!stack.isEmpty()){
element = stack.pop();
if(element.n == 1){
stringBuilder.append(element.src+"->"+element.des+",");
}else {
stack.push(new StackElement(element.n-1, element.mid, element.src, element.des));
stack.push(new StackElement(1, element.src, element.mid, element.des));
//stringBuilder.append(element.src+"->"+element.des+","); 递归时上面返回才会执行这一步 stack会直接执行
stack.push(new StackElement(element.n-1, element.src, element.des, element.mid));
}
}
}


}
28 changes: 28 additions & 0 deletions src/main/java/dp/MyStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dp;


import java.util.LinkedList;

/**
* Created by tianliangxia on 16-10-23.
*/
public class MyStack<T> extends LinkedList<T> implements MyStackInterface<T>{
public MyStack(){
super();
}
public void push(T t){
addLast(t);
}
public T top(){
return getLast();
}
public T pop(){
return removeLast();
}
public int size(){
return super.size();
}
public boolean isEmpty(){
return super.isEmpty();
}
}
12 changes: 12 additions & 0 deletions src/main/java/dp/MyStackInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dp;

/**
* Created by tianliangxia on 16-10-23.
*/
public interface MyStackInterface<T> {
void push(T t);
T top();
T pop();
int size();
boolean isEmpty();
}
36 changes: 36 additions & 0 deletions src/test/java/dp/HanMoTaTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package dp;

import org.junit.Test;

import static org.junit.Assert.*;

/**
* Created by tianliangxia on 16-10-23.
*/
public class HanMoTaTest {

@Test
public void test(){
String res = "src->des,";
StringBuilder stringBuilder = new StringBuilder();
HanMoTa.solve(1, stringBuilder, "src", "mid", "des");
assertEquals(res, stringBuilder.toString());

res = "src->mid,src->des,mid->des,";
stringBuilder = new StringBuilder();
HanMoTa.solve(2, stringBuilder, "src", "mid", "des");
assertEquals(res, stringBuilder.toString());
}

@Test
public void test2(){
StringBuilder stringBuilder1 = new StringBuilder();
StringBuilder stringBuilder2 = new StringBuilder();
int n = 10;
HanMoTa.solve(n, stringBuilder1, "src","mid","des");
//System.out.println(stringBuilder1.toString());
HanMoTa.solveWithStack(n, stringBuilder2, "src","mid","des");
assertEquals(stringBuilder1.toString(), stringBuilder2.toString());
}

}

0 comments on commit 9065e93

Please sign in to comment.