演算法訓練題

來源:互聯網
上載者:User

標籤:[]   代碼   port   int   有一個   java   等於   util   訓練   

問題:

有一個X*Y的網路,一個機器人只能走格點且只能向右或向下走,要從左上方走到右下角。請設計一個演算法,電腦器人有多少種走法。

給定兩個正整數int x, int y ,請返回機器人的走法數目。保證x+y小於等於12.

測試範例:

2,2

返回:2

代碼:

import java.util.Scanner;

public class newWeb {
  static int t = 0;
  public static void main(String[] args) {
    int x=new Scanner(System.in).nextInt();
    int y=new Scanner(System.in).nextInt();

    int[][] a = new int[x][y];
    int m = 0;
    int n = 0;
    a[m][n] = 1;

    xun(a,m,n,x,y);
    System.out.println(t);
  }

  private static void xun(int[][] a, int m, int n, int x, int y) {

    if((m+1)<x||(n+1)<y){
      if((n+1)<y&&a[m][n+1]!=1){
        a[m][n+1] = 1;
        xun(a,m,n+1,x,y);
        a[m][n+1] = 0;
      }

      if ((m+1)<x&&a[m+1][n] != 1) {
        a[m+1][n] = 1;
        xun(a,m+1,n,x,y);
        a[m+1][n] = 0;
      }
    }else{
      t++;
      for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < a[i].length; j++) {
          System.out.print(a[i][j]);
        }
        System.out.println();
      }
      System.out.println();
    }
  }
}

演算法訓練題

聯繫我們

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