java實現馬踏棋盤問題

來源:互聯網
上載者:User

標籤:描述   post   完整   java   imp   util   通過   printf   print   

1.問題描述:

  在國際象棋中,馬走日,使用者輸入棋盤的起始位置從1-8,輸出從這一點開始,馬走完整個棋盤的各個方案,並輸出方案數

 

2.輸入樣式:

  請輸入棋盤馬的起始位置:

    1  1

3.輸出樣式:

    1   20   11   14    3    6    9   16
   12   23    2   19   10   15    4    7
   21   30   13   24    5    8   17   26
   32   35   22   29   18   25   54   45
   39   48   31   34   55   46   27   60
   36   33   38   47   28   59   44   53
   49   40   63   56   51   42   61   58
   64   37   50   41   62   57   52   43

 

   1   20   11   14    3    6    9   16
   12   23    2   19   10   15    4    7
   21   30   13   24    5    8   17   26
   32   35   22   29   18   25   57   45
   39   63   31   34   56   46   27   51
   36   33   38   47   28   52   44   58
   62   40   64   55   60   42   50   53
   64   37   61   41   49   54   59   43

.......

4.解題思路:

    我們用一個二維數組類比馬走的方向,通過函數move(x,y),來達到馬走。 如果棋盤x<0 || x>7 || y<0 || y>7表示,x,y不在棋盤內,則直接return。如果棋盤qipan[x][y] ! = 0表示已經走過了,也直接

  return. 另外一個變數step,記錄棋盤的步數,從1一直到64,如果step到了64,則直接輸出棋盤,並且count++;沒到64則遞迴調用move(x,y)函數.  最後走完一種方案再回溯.

 

5.代碼執行個體:

import java.util.Scanner;

public class Chess{

  static int weizhi[][] = {{-2,1},{-2,-1},{-1,2},{-1,-2},{1,2},{1,-2},{2,1},{2,-1}};

  static int step = 1;

  static int qipan[][] = new int[8][8];

  static int count = 0;//  全部走完棋盤的方案

  public static void main(String[] args){

 

    //先初始化棋盤
    for(int i=0;i<8;i++){

      for(int j=0;j<8;j++){

        qipan[i][j] = 0;

      }

    }

    System.out.println("請輸入棋盤馬的起始位置:");

    Scanner scn = new Scanner(System.in);

    int x = scn.nextInt();

    int y = scn.nextInt();

    x--;  //棋盤輸入是從1,1開始,而數組的起始位置是從0,0開始

    y--;

    move(x,y);

  }

  public static void move(int x,int y){

    int next_x;

    int next_y;

    //x,y越界了

    if(x<0 || x>7 || y<0 || y>7){

      return;

    }

    //表示棋盤上已經有馬走過了

    if(qipan[x][y] != 0){

      return;

    }

    qipan[x][y] = step;

    step++;

    //如果step大於64了,則直接輸出並計數
    if(step>64){

      for(int i=0;i<8;i++){

        for(int j=0;j<8;j++){

          System.out.printf("%5d",qipan[i][j]);

        }

        System.out.println();

      }

      System.out.println("===============");

      count++;

      return;

    }else{

      for(int i=0;i<8;i++){ //馬可以走的8個方向

        next_x = x + weizhi[i][0];

        next_y = y + weizhi[i][1];

        move(next_x,next_y);

      }

    }

    qipan[x][y] = 0;

    step --; //回溯

  }

}

 

java實現馬踏棋盤問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.