-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
XiaTianliang
committed
Oct 23, 2016
1 parent
d578066
commit 9065e93
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |