java - 确定板上的两个方格是否由相同的方格连续/连接 turn()
问题描述
我编写了下面的代码以及 isAdjacentColor 方法。如果两个方格相邻(例如,一个方格的皇后彼此远离)并且具有相同的颜色或 turn(),则 isAdjacentColor 方法返回 true。
我想我已经实现了广度优先搜索。
我想知道如何使用递归调用,因为当前返回递归调用将在探索所有相邻选项之前返回 false。
boolean isContiguous(Square from, Square to, boolean[][] visited) {
if (get(from) != get(to)) {
return false;
}
if (from == to) {
return true;
}
int col = from.col();
int row = from.row();
if (!visited[col][row]) {
visited[col][row] = true;
if (col - 1 >= 0 && row - 1 >= 0) {
Square curr = sq(col - 1, row - 1);
if (isAdjacentColor(from, curr, turn())) {
return isContiguous(curr, to, visited);
}
}
if (row - 1 >= 0) {
Square curr = sq(col, row - 1);
if (isAdjacentColor(from, curr, get(from))) {
return isContiguous(curr, to, visited);
}
}
if (col + 1 < BOARD_SIZE && row - 1 >= 0) {
Square curr = sq(col + 1, row - 1);
if (isAdjacentColor(from, curr, get(from))) {
return isContiguous(curr, to, visited);
}
}
if (col - 1 >= 0) {
Square curr = sq(col - 1, row);
if (isAdjacentColor(from, curr, get(from))) {
return isContiguous(curr, to, visited);
}
}
if (col + 1 < BOARD_SIZE) {
Square curr = sq(col + 1, row);
if (isAdjacentColor(from, curr, get(from))) {
return isContiguous(curr, to, visited);
}
}
if (col - 1 >= 0 && row + 1 < BOARD_SIZE) {
Square curr = sq(col - 1, row + 1);
if (isAdjacentColor(from, curr, get(from))) {
return isContiguous(curr, to, visited);
}
}
if (row + 1 < BOARD_SIZE) {
Square curr = sq(col, row + 1);
if (isAdjacentColor(from, curr, get(from))) {
return isContiguous(curr, to, visited);
}
}
if (col + 1 < BOARD_SIZE && row + 1 < BOARD_SIZE) {
Square curr = sq(col + 1, row + 1);
if (isAdjacentColor(from, curr, get(from))) {
return isContiguous(curr, to, visited);
}
}
}
return false;
}
解决方案
我想我已经通过创建一个存储每个递归调用答案的 ArrayList 解决了我的问题。然后在最后而不是返回 false,如果 ArrayList 包含 true,则返回。
boolean isContiguous(Square from, Square to, boolean[][] visited) {
if (get(from) != get(to)) {
return false;
}
if (from == to) {
return true;
}
int col = from.col();
int row = from.row();
if (!visited[col][row]) {
visited[col][row] = true;
if (col - 1 >= 0 && row - 1 >= 0) {
Square curr = sq(col - 1, row - 1);
if (isAdjacentColor(from, curr, turn())) {
contain.add(isContiguous(curr, to, visited));
}
}
if (row - 1 >= 0) {
Square curr = sq(col, row - 1);
if (isAdjacentColor(from, curr, get(from))) {
contain.add(isContiguous(curr, to, visited));
}
}
if (col + 1 < BOARD_SIZE && row - 1 >= 0) {
Square curr = sq(col + 1, row - 1);
if (isAdjacentColor(from, curr, get(from))) {
contain.add(isContiguous(curr, to, visited));
}
}
if (col - 1 >= 0) {
Square curr = sq(col - 1, row);
if (isAdjacentColor(from, curr, get(from))) {
contain.add(isContiguous(curr, to, visited));
}
}
if (col + 1 < BOARD_SIZE) {
Square curr = sq(col + 1, row);
if (isAdjacentColor(from, curr, get(from))) {
contain.add(isContiguous(curr, to, visited));
}
}
if (col - 1 >= 0 && row + 1 < BOARD_SIZE) {
Square curr = sq(col - 1, row + 1);
if (isAdjacentColor(from, curr, get(from))) {
contain.add(isContiguous(curr, to, visited));
}
}
if (row + 1 < BOARD_SIZE) {
Square curr = sq(col, row + 1);
if (isAdjacentColor(from, curr, get(from))) {
contain.add(isContiguous(curr, to, visited));
}
}
if (col + 1 < BOARD_SIZE && row + 1 < BOARD_SIZE) {
Square curr = sq(col + 1, row + 1);
if (isAdjacentColor(from, curr, get(from))) {
contain.add(isContiguous(curr, to, visited));
}
}
}
return contain.contains(true);
}
推荐阅读
- php - Wordpress 自定义字段顺序
- swift - 列出您附近的所有信标
- azure - 间歇性 HttpClient 问题 - SslStream 已处理
- php - 000webhost 中的 tmp_name null 使用 Lumen
- php - 语法错误';' 查询数据库到表时
- angular - 错误类型错误:无法读取未定义的属性“无效”
- javascript - 谷歌地图 addDomListener 不起作用。如何解决?
- javascript - 当我已经声明了属性时,无法读取未定义 reactjs 的属性“地图”
- python - 在 Linux 上从源代码构建 PyTorch 的问题
- django - __init__() 缺少 1 个必需的位置参数:'model_field'