[关闭]
@iar 2015-10-26T15:35:22.000000Z 字数 5028 阅读 99

Report: Knight's Tour

Lintcode Recursion


题目

怎么想?

First trial: WRONG!

ACMer's solution

  1. public boolean helperACMer(int[][] steps, int step, int row, int col) {
  2. steps[row][col] = step;
  3. if (step == size) {
  4. return true;
  5. }
  6. for (int i = 0; i < 8; ++i) {
  7. int next_x = row + dx[i];
  8. int next_y = col + dy[i];
  9. if (isValid(steps, next_x, next_y)) {
  10. if (helperACMer(steps, step + 1, next_x, next_y) == true) {
  11. return true;
  12. }
  13. }
  14. }
  15. steps[row][col] = -1;
  16. return false;
  17. }
  1. [[1, 46, 35, 26, 3, 12, 17, 14]
  2. [36, 49, 2, 45, 28, 15, 4, 11]
  3. [47, 58, 27, 34, 25, 18, 13, 16]
  4. [50, 37, 48, 59, 44, 29, 10, 5]
  5. [57, 60, 51, 38, 33, 24, 19, 22]
  6. [52, 39, 54, 43, 30, 21, 6, 9]
  7. [61, 56, 41, 32, 63, 8, 23, 20]
  8. [40, 53, 62, 55, 42, 31, 64, 7]]
  9. [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]

GeekForGeek's solution

  1. public boolean helperGeek(int[][] steps, int step, int row, int col) {
  2. // save temp result
  3. if (step == size) {
  4. return true;
  5. }
  6. // backtracking with prune
  7. Integer[] shu = new Integer[]{4, 5, 6, 7, 3, 2, 1, 0}; //{0,1,2,3,4,5,6,7};
  8. List<Integer> list = Arrays.asList(shu);
  9. // Collections.shuffle(list); WHY the shuffle leads the helper gointo forever ???
  10. for (int s : list) { // recursion for all eight direction
  11. if (!isValid(steps, row + dx[s], col + dy[s])) {
  12. continue;
  13. }
  14. /**
  15. * I got confused: the next_x, next_y should have step number: step or step+1, or I should assign
  16. * current x,y with step or step+1?
  17. */
  18. steps[row + dx[s]][col + dy[s]] = step;
  19. /**
  20. * found a Knight traverse then return. NOTICE: this is recursion call, so ??? times recursion,
  21. * I got this recursion call result. Can be earlier if recursion failed.
  22. */
  23. if (helperGeek(steps, step + 1, row + dx[s], col + dy[s]) == true) {
  24. return true; // !!!
  25. } else {
  26. steps[row + dx[s]][col + dy[s]] = -1;
  27. }
  28. }
  29. return false; // !!!
  30. }
  1. [[0, 11, 24, 29, 52, 33, 50, 63]
  2. [13, 22, 27, 32, 25, 30, 53, 34]
  3. [10, 1, 12, 23, 28, 51, 62, 49]
  4. [5, 14, 21, 26, 31, 60, 35, 54]
  5. [2, 9, 4, 59, 44, 55, 48, 61]
  6. [15, 6, 17, 20, 39, 36, 43, 56]
  7. [18, 3, 8, 45, 58, 41, 38, 47]
  8. [7, 16, 19, 40, 37, 46, 57, 42]]
  9. [0, 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]

思念起

Helper Shuffle

  1. public boolean helperShuffle(int[][] steps, int step, int row, int col, List<Integer> list) {
  2. if (step == SIZE) {
  3. return true;
  4. }
  5. Collections.shuffle(list);
  6. for (int i : list) {
  7. int next_x = row + dx[i];
  8. int next_y = col + dy[i];
  9. if (isValid(steps, next_x, next_y)) {
  10. steps[next_x][next_y] = step;
  11. if (true == helperShuffle(steps, step+1, next_x, next_y, list)) {
  12. return true;
  13. }
  14. else {
  15. steps[next_x][next_y] = -1;
  16. }
  17. }
  18. }
  19. return false;
  20. }
  1. [[0, 9, 6, 13, 26, 3, 28, 19, 24, 21]
  2. [7, 14, 11, 2, 5, 16, 25, 22, 29, 18]
  3. [10, 1, 8, 15, 12, 27, 4, 17, 20, 23]]
  4. [0, 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]
  5. [[0, 3, 12, 15, 26, 7, 10, 21, 28, 23]
  6. [13, 16, 5, 2, 11, 18, 27, 24, 9, 20]
  7. [4, 1, 14, 17, 6, 25, 8, 19, 22, 29]]
  8. [0, 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]
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注