馬的走法

來源:互聯網
上載者:User

馬的走法
Time Limit: 1000MS Memory limit: 65536K
題目描述
在一個4*5的棋盤上,馬的初始位置座標(縱 橫)位置由鍵盤輸入,求馬能返回初始位置的所有不同走法的總數(馬走過的位置不能重複,馬走“日”字)。如果馬的初始位置座標超過棋盤的邊界,則輸出ERROR。例如初始位置為4 6,則輸出ERROR。
輸入
輸入資料只有一行,有兩個用空格分開的整數,表示馬所在的初始位置座標。首行首列位置編號為(1 1)。
輸出
輸出一行,只有一個整數,表示馬能返回初始位置的所有不同走法的總數。

如果輸入的馬的初始位置超出棋盤邊界,則輸出ERROR。
樣本輸入
2 2
樣本輸出
4596

import java.io.BufferedInputStream;import java.io.BufferedReader;import java.util.*;public class Ma{private static int f[][]={{-2,-2,-1,-1,2,2,1,1},{-1,1,-2,2,-1,1,-2,2}};private static int start;private static int end;private static int total=0;private static int maze[][]=new int[5][6];public static void main(String args[]){long time1=System.currentTimeMillis();Scanner sc=new Scanner(new BufferedInputStream(System.in));start=sc.nextInt();end=sc.nextInt();if(start<1||end>=6)System.out.println("ERROR");else{maze[start][end]=1;fun(start,end);System.out.println(total);}long time2=System.currentTimeMillis();System.out.println((time2-time1)+"ms");}public static void fun(int p1,int p2){int px;int py;for(int i=0;i<8;i++){px=p1+f[0][i];py=p2+f[1][i];if((px==start)&&(py==end)) total++;else if((px>=1&&px<5)&&(py>=1&&py<6)&&(maze[px][py]==0)){maze[px][py]=1;fun(px,py);maze[px][py]=0;}}}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.