java - Android - 递归检查 map 是否可解的算法

标签 java android algorithm

我正在制作一个机器人 Hashikawekero 益智游戏,我已经实现了一种算法,使用二维数组在随机位置生成节点(岛屿)这工作正常它在随机位置创建节点但大多数时候无法解决 map 。 map 节点随机生成。

BoardCreation.java 类 - 这会生成 map 。

package Island_and_Bridges.Hashi;

import android.annotation.TargetApi;
import android.os.Build;
import android.util.Log;

import java.util.Random;

import static junit.framework.Assert.*;
//This class Creates the map by random using a 2d array

public class BoardCreation {
  // This class member is used for random initialization purposes.
  static private final Random random = new Random();

  // The difficulty levels.
  private static final int EASY = 0;
  static public final int MEDIUM = 1;
  static public final int HARD = 2;

  static public final int EMPTY = 0;

  private static int ConnectionFingerprint(BoardElement start, BoardElement end) {
    int x = start.row * 100 + start.col;
    int y = end.row * 100 + end.col;
    // Swap to get always the same fingerprint independent whether we are called
    // start-end or end-start
    if (x > y ) {
      int temp = x;
      x = y;
      y = temp;
    }
    Log.d("", String.format("%d %d" , x ,y));
    return x ^ y;
  }

  public class State {
    // The elements of the board are stored in this array.
    // A value defined by "EMPTY" means that its not set yet.
    public BoardElement [][] board_elements = null;

    public int [][] cell_occupied = null;

    // The width of the board. We only assume squared boards.
    public int board_width=0;


    public State(int width) {
      board_width = width;
      board_elements = new BoardElement[width][width];
      cell_occupied = new int[width][width];
    }

    public State CloneWithoutConnections() {
      State newstate = new State(board_width);
      if (board_elements != null) {
    newstate.board_elements = new BoardElement[board_elements.length][board_elements.length];
    for (int i = 0; i < board_elements.length; ++i) {
      for (int j = 0; j < board_elements.length; ++j) {
        if (board_elements[i][j] == null)
              continue;
        newstate.board_elements[i][j] = board_elements[i][j].clone();
      }
    }
      }
      if (cell_occupied != null) {
          assert board_elements != null;
          newstate.cell_occupied = new int[board_elements.length][board_elements.length];
    for (int i = 0; i < board_elements.length; ++i) {
        System.arraycopy(cell_occupied[i], 0, newstate.cell_occupied[i], 0, board_elements.length);
    }
      }
      return newstate;
    }

    public void AddToBridgeCache(BoardElement first, BoardElement second) {
      if (first == null || second == null) { return; }
      final int fingerprint = ConnectionFingerprint(first, second);
      Log.d(getClass().getName(),
          String.format("Fingerprint of this bridge %d", fingerprint));
      // mark the end points as occupied.
      cell_occupied[first.row][first.col] = fingerprint;
      cell_occupied[second.row][second.col] = fingerprint;

      int dcol = second.col - first.col;
      int drow = second.row - first.row;

      if (first.row == second.row) {
    for (int i = (int) (first.col + Math.signum(dcol)); i != second.col; i += Math.signum(dcol)) {
      cell_occupied[first.row][i] = fingerprint;
        String.format("deleting bridge");
    }
      } else {
    assert first.col == second.col;
    for (int i = (int) (first.row + Math.signum(drow)); i != second.row; i+= Math.signum(drow)) {
      cell_occupied[i][first.col] = fingerprint;

    }
      }
    }
  } // end of state

  private State current_state, old_state;

  static private final int WIDTH_EASY = 7;

  private void NewGame(int hardness) {
    switch(hardness) {
      case EASY:
    Log.d(getClass().getName(), "Initializing new easy game");
    InitializeEasy();
    old_state = getCurrentState().CloneWithoutConnections();
    break;
    }
  }

  public void ResetGame() {
    if (old_state != null) {
      Log.d(getClass().getName(), "Setting board_elements to old_elements");
      setCurrentState(old_state.CloneWithoutConnections());
    } else {
      Log.d(getClass().getName(), "old_lements are zero");
    }
  }

  public BoardCreation(int hardness) {
    NewGame(hardness);
  }

  public boolean TryAddNewBridge(BoardElement start, BoardElement end, int count) {
    assertEquals(count, 1);
    assert (start != null);
    assert (end != null);
    final int fingerprint = ConnectionFingerprint(start, end);

    Log.d(getClass().getName(),
    String.format("considering (%d,%d) and (%d,%d)", start.row,start.col, end.row,end.col));
    if (start.row == end.row && start.col == end.col) {
      Log.d(getClass().getName(), "Same nodes selected!");
      return false;
    }
    assert count > 0;

    int dcol = end.col - start.col;
    int drow = end.row - start.row;

    // It must be a vertical or horizontal bridge:
    if (Math.abs(dcol) > 0 && Math.abs(drow) > 0) {
      Log.d(getClass().getName(), "not a horizontal or vertical bridge.");
      return false;
    }

    // First we check whether start and end elements can take the specified bridge counts.
    int count_start = start.GetCurrentCount();
    int count_end = end.GetCurrentCount();

    if (count_start  + count > start.max_connecting_bridges ||
    count_end + count > end.max_connecting_bridges) {
      Log.d(getClass().getName(), "This Bridge is not allowed");
      return false;
    }

    Log.d(getClass().getName(),
     String.format("Sums:%d @ (%d,%d)  and %d @ (%d,%d)",
       count_start, start.row, start.col,
       count_end, end.row, end.col));

    Connection start_connection = null;
    Connection end_connection = null;

    // Next we check whether we are crossing any lines.
    if (start.row == end.row) {
      for (int i = (int) (start.col + Math.signum(dcol)); i != end.col; i += Math.signum(dcol)) {
    if (getCurrentState().cell_occupied[start.row][i] > 0 &&
            getCurrentState().cell_occupied[start.row][i] != fingerprint) {
      Log.d(getClass().getName(), "Crossing an occupied cell.");
      return false;
    }
      }
      assert start.col != end.col;
      if (start.col > end.col) {
    start.connecting_east = GetOrCreateConnection(end, start.connecting_east);
    end.connecting_west = GetOrCreateConnection(start, end.connecting_west);
    start_connection = start.connecting_east;
    end_connection = end.connecting_west;
      } else {
    start.connecting_west = GetOrCreateConnection(end, start.connecting_west);
    end.connecting_east = GetOrCreateConnection(start, end.connecting_east);
    start_connection = start.connecting_west;
    end_connection = end.connecting_east;
      }
    } else {
      assert start.col == end.col;
      for (int i = (int) (start.row + Math.signum(drow)); i != end.row ; i += Math.signum(drow)) {
    if (getCurrentState().cell_occupied[i][start.col] > 0 &&
            getCurrentState().cell_occupied[i][start.col] != fingerprint) {
      Log.d(getClass().getName(), "Crossing an occupied cell.");
      return false;
    }
      }
      if (start.row > end.row ) {
    start.connecting_north = GetOrCreateConnection(end, start.connecting_north);
    end.connecting_south = GetOrCreateConnection(start, end.connecting_south);
    start_connection = start.connecting_north;
    end_connection = end.connecting_south;
      } else {
    start.connecting_south= GetOrCreateConnection(end, start.connecting_south);
    end.connecting_north = GetOrCreateConnection(start, end.connecting_north);
    start_connection = start.connecting_south;
    end_connection = end.connecting_north;
      }
    }
    start_connection.destination = end;
    end_connection.destination = start;
    start_connection.second += count;
    end_connection.second += count;

    getCurrentState().AddToBridgeCache(start, end);

    Log.d(getClass().getName(),
        String.format("New bridge added. Sums:%d @ (%d,%d)  and %d @ (%d,%d)",
         count_start, start.row,start.col,
         count_end, end.row,end.col));
    return true;
  }

  private Connection GetOrCreateConnection(
      BoardElement end,
      Connection connection) {
    if (connection!= null) { return connection; }
    return new Connection();
  }

  @TargetApi(Build.VERSION_CODES.N)
  private void InitializeEasy() {
      Random rand = new Random();

      String[][] debug_board_state = new String[7][7];
      setCurrentState(new State(WIDTH_EASY));
      for (int row = 0; row < debug_board_state.length; row++) {
          for (int column = 0; column < debug_board_state[row].length; column++) {
              debug_board_state[row][column] = String.valueOf(rand.nextInt(5));

          }
      }

      for (int row = 0; row < debug_board_state.length; row++) {
          for (int column = 0; column < debug_board_state[row].length; column++) {
              System.out.print(debug_board_state[row][column] + " ");
          }
          System.out.println();
      }
      for (int row = 0; row < WIDTH_EASY; ++row) {
          for (int column = 0; column < WIDTH_EASY; ++column) {

                  getCurrentState().board_elements[row][column] = new BoardElement();
                  getCurrentState().board_elements[row][column].max_connecting_bridges = Integer.parseInt(debug_board_state[row][column]);
                  getCurrentState().board_elements[row][column].row = row;
                  getCurrentState().board_elements[row][column].col = column;

                  if (getCurrentState().board_elements[row][column].max_connecting_bridges > 0) {
                      getCurrentState().board_elements[row][column].is_island = true;
                  }
              }
          }
      }



  private void setCurrentState(State new_state) {
    this.current_state = new_state;
  }

  public State getCurrentState() {
    return current_state;
  }
}

我可以使用什么算法来确保在生成节点之前可以解决 map (岛屿与桥梁相连)。

这就是 map 的样子(不要介意设计)

最佳答案

需要考虑的一件事是从空白板开始。放置一个岛屿。然后放置另一个可以连接到第一个的岛屿(即在四个主要方向之一)。用桥连接两者,并增加每个岛的数量。

现在,选择两个岛屿中的一个并放置另一个它可以连接的岛屿。添加桥梁和增量。

以这种方式继续,直到您放置了您想要放置的岛屿数量。

这里的美妙之处在于您从一个空板开始,并且在构建过程中该板始终有效。

您必须确保在放置新岛时不会过桥,但这很容易做到,因为您知道现有桥梁的位置。

关于java - Android - 递归检查 map 是否可解的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51497041/

相关文章:

java - java .properties 文件中的括号?

java - apache wicket 中 SignInPage 的本地化

JavaScript 处理设备触摸屏被按住

AndroidRuntime android.database.SQLiteException : near "=": syntax error

java - 不确定这段代码的时间复杂度

c# - 阵列阵列之间的距离

python - 条件循环橄榄球抽签的列表操作

java - 如何移动 Primefaces-Upload-Temp-Files 而不是进行耗时的复制过程?

android - 是否有任何基于 Java 的 C/C++ 编译器?

java - 使用 sysdate 更新日期列会引发 Oracle 中的唯一约束冲突。由于日期是主键而随机发生