Advertisement
Guest User

Untitled

a guest
May 10th, 2022
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.57 KB | None | 0 0
  1. static int N;
  2. static boolean[][] lattice;
  3. static final long func(int remainSteps, int lastX, int lastY) {
  4.     long count = 0;
  5.    
  6.     int nextX = lastX-1;
  7.     if (nextX >= 0 && !lattice[nextX][lastY]) {
  8.         if (remainSteps == 1) {
  9.             count++;
  10.         } else {
  11.             lattice[nextX][lastY] = true;
  12.             count += func(remainSteps-1, nextX, lastY);
  13.             lattice[nextX][lastY] = false;
  14.         }
  15.     }
  16.    
  17.     nextX = lastX+1;
  18.     if (nextX <= N && !lattice[nextX][lastY]) {
  19.         if (remainSteps == 1) {
  20.             count++;
  21.         } else {
  22.             lattice[nextX][lastY] = true;
  23.             count += func(remainSteps-1, nextX, lastY);
  24.             lattice[nextX][lastY] = false;
  25.         }
  26.     }
  27.    
  28.     int nextY = lastY-1;
  29.     if (nextY >= 0 && !lattice[lastX][nextY]) {
  30.         if (remainSteps == 1) {
  31.             count++;
  32.         } else {
  33.             lattice[lastX][nextY] = true;
  34.             count += func(remainSteps-1, lastX, nextY);
  35.             lattice[lastX][nextY] = false;
  36.         }
  37.     }
  38.    
  39.     nextY = lastY+1;
  40.     if (nextY <= N && !lattice[lastX][nextY]) {
  41.         if (remainSteps == 1) {
  42.             count++;
  43.         } else {
  44.             lattice[lastX][nextY] = true;
  45.             count += func(remainSteps-1, lastX, nextY);
  46.             lattice[lastX][nextY] = false;
  47.         }
  48.     }
  49.     return count;
  50. }
  51.  
  52. //调用的时候
  53. for (int n = 2; n <= 25; n++) {
  54.     N = n;
  55.     lattice = new boolean[N+1][N+1];
  56.     lattice[0][0] = true;
  57.     lattice[1][0] = true;
  58.     long nPaths = func(n-1, 1, 0);
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement