JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 新手區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 雙色河內塔的問題...
光月





發文: 26
積分: 0
於 2004-11-19 13:32 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我最近下了新手區裡的一個題目檔,裡面有一題是雙色河內塔&3色河內塔,我照著裡面的演算法寫,可是RUN的結果只有再4個DISC時才會正確,其它盤數都會亂掉,我想了好多天........站內只有單一色河內塔的解法,我快想破頭了.....以下是小弟自己寫的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.io.*;
class K
{
  static char a='a',b='b',c='c';
  public static int disc,sum;
  public static void ch(int n,char a,char b,char c){
    if(n==1)
      System.out.println("Disc "+n+" do "+a+" to "+c);
    else{
      ch(n-1,a,c,b);        
      if(n>2) ch(n-2,c,a,b);
      if(n>4)  ch(n-3,a,c,b);
      if(n>3)  ch(1,a,c,b);    
      System.out.println("Disc "+n+" do "+a+" to "+c);
        if(n>sum-1&&n>2){
          ch(n-2,b,c,a);
          ch(n-3,c,b,a);
          }          
      }
    
    }
  public static void main(String args[])throws IOException{
    System.out.print("Plz enter one math:");
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    disc=Integer.parseInt(br.readLine());
    sum=disc;
    while(sum>0){
      ch(sum,a,b,c);
      sum-=2;
    }
  }
}
      

拜託有會的大大能幫我指出哪裡錯,或是PO上另一種解法讓我參考也可,最好還有3色的解法,感謝。


tekwei edited on 2004-11-19 15:16
reply to postreply to post
作者 Re:雙色河內塔的問題... [Re:光月]
lin0_o





發文: 12
積分: 1
於 2004-11-19 15:50 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
http://www.javaworld.com.tw/jute/post/view?bid=29&id=76999&sty=1&tpg=1&age=0

這裡面有...

老掉牙 / 雙色河內塔與三色河內塔


lin0_o edited on 2004-11-19 16:21
reply to postreply to post
作者 Re:雙色河內塔的問題... [Re:lin0_o]
光月





發文: 26
積分: 0
於 2004-11-19 16:44 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
lin0_o wrote:
http://www.javaworld.com.tw/jute/post/view?bid=29&id=76999&sty=1&tpg=1&age=0

這裡面有...

老掉牙 / 雙色河內塔與三色河內塔

我就是從那邊找來的題目啊........我上面的文章裡有說了,
我就是找不到正確解答才來這邊問的,
它裡面只提供了演算法,可能是我太笨了,怎麼寫就是寫不出來正確的,
希望有會的大大能幫忙一下,感謝。


reply to postreply to post
作者 Re:雙色河內塔的問題... [Re:光月]
caterpillar

良葛格

版主

發文: 2612
積分: 70
於 2004-11-23 16:52 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Sorry....這篇的算法有問題,我臨時找不到參考的原始資料。。。。不知道是不是我筆誤或是原資料有誤。。。。我現在又暫時沒時間來研究如何解出來。。。

PS. 大家要不要試著自己提出可用的算法?。。。。


reply to postreply to post
良葛格學習筆記
作者 Re:雙色河內塔的問題... [Re:光月]
T55555

Java, Ruby, Haskell

版主

發文: 1026
積分: 24
於 2004-11-23 22:17 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
http://www.javaworld.com.tw/jute/post/view?bid=5&id=40273&sty=3&keywords=Hanoi

reply to postreply to post
作者 Re:雙色河內塔的問題... [Re:光月]
光月





發文: 26
積分: 0
於 2004-11-24 02:43 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
非常謝謝上面幾位大大的回覆跟指點,謝謝良葛格大大提供的題目,
還有上面這位大大你提供的網頁資料好像只有說單一河內塔.........
而且還是英文的.......這對於我這種初學者來說,似乎太困難了點,
而且那個討論我很早就有去爬過......但跟雙色的河內塔好像沒什麼很大的關係,或許是我太笨,我想了快1星期了.......每次只要多加2個DISC的數目,
就要改寫程式,這太不符合時間效率了......,所以希望有高手們能抽空寫個範例給我們這些新手參考一下,其實還有蠻多題目我有很多疑問....,但網路上找的到資料的,我當然就不會問囉,還是我太執卓於一些題目呢?
可是如果有一題解不出來,我就會覺得很難過=.=
還是希望有大大能寫個範例給我們這些新手參考一下,謝謝^^"


reply to postreply to post
作者 Re:雙色河內塔的問題... [Re:光月]
T55555

Java, Ruby, Haskell

版主

發文: 1026
積分: 24
於 2004-11-24 13:58 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Here is my solution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
 *  Hanoi with 2 colors disks
 * 
 *  @author T55555
 *  @version 1.0 2004-11-24
 */
public class Hanoi2Colors {
     
    public static void help() {
        System.out.println("Usage: java Hanoi2Colors number_of_disks");
        System.out.println("\t number_of_disks: must be a even number.");
        System.exit(0);
    }
    
    public static void main(String[] args) {
        int disks = 0;        
        try {
            disks = Integer.parseInt(args[0]);
        } catch (Exception e) {
            help();
        }
        if ((disks <= 0) || (disks % 2 != 0)) { 
            help(); 
        }
        hanoi2colors(disks);
    }
     
    public static void hanoi(int disks, char source, char temp, char target) {
        if (disks == 1) {
            System.out.println("move disk from " + source + " to " + target);
            System.out.println("move disk from " + source + " to " + target);
        } else {        
            hanoi(disks-1, source, target, temp);
            hanoi(1, source, temp, target);
            hanoi(disks-1, temp, source, target);
        }
    }
 
    public static void hanoi2colors(int disks) {
        char source = 'A';
        char temp = 'B';
        char target = 'C';
        for (int i = disks / 2; i > 1; i--) {
            hanoi(i-1, source, temp, target);
            System.out.println("move disk from " + source + " to " + temp);
            System.out.println("move disk from " + source + " to " + temp); 
            hanoi(i-1, target, temp, source);
            System.out.println("move disk from " + temp + " to " + target);
        }
        System.out.println("move disk from " + source + " to " + temp);
        System.out.println("move disk from " + source + " to " + target);
    }
}


BTW: 3 colors are similar to 2 colors, once you know how to solve 2 colors, 3 colors will be trivial.


T55555 edited on 2004-11-26 00:54
reply to postreply to post
作者 Re:雙色河內塔的問題... [Re:光月]
光月





發文: 26
積分: 0
於 2004-11-24 23:10 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
謝謝版主大大寫的程式碼,我大概看了一下,
發現有幾個寫法我看不太懂..........
不過我想書裡面應該找的到答案才是,
另外我發現一件事情,版主大大是不是外國人啊?
怎麼您回覆的文章都是英文的.........
如果不是,那我可要好好加強一下英文囉.....(哇英文白痴)
不然會錯過很多好文章,
看來今晚有的研究囉~呵


光月 edited on 2004-11-25 01:24
reply to postreply to post
作者 Re:雙色河內塔的問題... [Re:光月]
T55555

Java, Ruby, Haskell

版主

發文: 1026
積分: 24
於 2004-11-26 02:37 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Here is my solution for the 3 colors.
Enjoy!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
 * Hanoi tower with 3 colors disks.
 *
 * @author T55555
 * @version 1.0 2004-11-25
 */
public class Hanoi3Colors {
    
    public static void help() {
        System.out.println("Usage: java Hanoi3Colors number_of_disks");
        System.out.println("\tnumber_of_disks: must be a number divisible by 3.");
        System.exit(0);
    }
    
    public static void main(String[] args) {
        int disks = 0;        
        try {
            disks = Integer.parseInt(args[0]);
        } catch (Exception e) {
            help();
        }
        if ((disks <= 0) || (disks % 3 != 0)) { 
            help(); 
        }
        hanoi3colors(disks);
    }
    
    public static void hanoi(int disks, char source, char temp, char target) {
        if (disks == 1) {
            System.out.println("move disk from " + source + " to " + target);
            System.out.println("move disk from " + source + " to " + target);
            System.out.println("move disk from " + source + " to " + target);
        } else {        
            hanoi(disks-1, source, target, temp);
            hanoi(1, source, temp, target);
            hanoi(disks-1, temp, source, target);
        }
    }
    
    public static void hanoi3colors(int disks) {
        char source = 'A';
        char temp   = 'B';
        char target = 'C';
        if (disks == 3) {  // shorter version for the 3 disks case.
            System.out.println("move disk from " + source + " to " + temp);
            System.out.println("move disk from " + source + " to " + temp);
            System.out.println("move disk from " + source + " to " + target);
            System.out.println("move disk from " + temp + " to " + target);
            System.out.println("move disk from " + temp + " to " + source);
            System.out.println("move disk from " + target + " to " + temp);
        } else {
            hanoi(disks/3-1, source, temp, target);
            System.out.println("move disk from " + source + " to " + temp);
            System.out.println("move disk from " + source + " to " + temp);
            System.out.println("move disk from " + source + " to " + temp);
            hanoi(disks/3-1, target, temp, source);
            System.out.println("move disk from " + temp + " to " + target);
            System.out.println("move disk from " + temp + " to " + target);
            System.out.println("move disk from " + temp + " to " + target);
            hanoi(disks/3-1, source, target, temp);
            System.out.println("move disk from " + target + " to " + source);
            System.out.println("move disk from " + target + " to " + source);
            hanoi(disks/3-1, temp, source, target);
            System.out.println("move disk from " + source + " to " + temp);
        
            for (int i = disks / 3 - 1; i > 0; i--) {
                if (i>1) { hanoi(i-1, target, source, temp); }
                System.out.println("move disk from " + target + " to " + source);
                System.out.println("move disk from " + target + " to " + source);
                if (i>1) { hanoi(i-1, temp, source, target); }
                System.out.println("move disk from " + source + " to " + temp);
            }
        }
    }
}


T55555 edited on 2004-11-26 11:33
reply to postreply to post
作者 Re:雙色河內塔的問題... [Re:光月]
光月





發文: 26
積分: 0
於 2004-11-27 16:14 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
謝謝版主大大的熱心,看了您之前雙色河內塔的解法之後,
我也想出了3色的解法,只是跟版大的3色有點不同,
版大寫的有很多令人值得學習的地方,謝謝版大。


reply to postreply to post
作者 Re:雙色河內塔的問題... [Re:光月]
T55555

Java, Ruby, Haskell

版主

發文: 1026
積分: 24
於 2004-11-28 21:13 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
The version 1.0 respects caterpillar's chm file's image, which
top disk color at A, middle disk color at B, and bottom disk color at C.
If the order of colors is not important, for example, if we like to have:
bottom disk color at A, middle disk color at B, and top disk color at C.
The program can be simpler and shorter.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
 * Towers of Hanoi with 3 colors disks.
 *
 * @author T55555
 * @version 1.1 2004-11-28
 */
public class Hanoi3Colors {
    
    public static void help() {
        System.out.println("Usage: java Hanoi3Colors number_of_disks");
        System.out.println("\tnumber_of_disks: must be a number divisible by 3.");
        System.exit(0);
    }
    
    public static void main(String[] args) {
        int disks = 0;        
        try {
            disks = Integer.parseInt(args[0]);
        } catch (Exception e) {
            help();
        }
        if ((disks <= 0) || (disks % 3 != 0)) { 
            help(); 
        }
        hanoi3colors(disks);
    }
    
    public static void hanoi(int disks, char source, char temp, char target) {
        if (disks <= 0) {
            return;
        } else if (disks == 1) {
            System.out.println("move disk from " + source + " to " + target);
            System.out.println("move disk from " + source + " to " + target);
            System.out.println("move disk from " + source + " to " + target);
        } else {        
            hanoi(disks-1, source, target, temp);
            hanoi(1, source, temp, target);
            hanoi(disks-1, temp, source, target);
        }
    }
    
    public static void hanoi3colors(int disks) {
        char source = 'A';
        char temp   = 'B';
        char target = 'C';
        for (int i = disks/3; i > 0; i--) {
            hanoi(i-1, source, target, temp);
            System.out.println("move disk from " + source + " to " + target);
            System.out.println("move disk from " + source + " to " + target);
            hanoi(i-1, temp, target, source);
            System.out.println("move disk from " + target + " to " + temp);
        }
    }
}


reply to postreply to post
» JWorld@TW »  Java 新手區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8