79239939

Date: 2024-11-30 15:58:44
Score: 4.5
Natty:
Report link

tu solución me ayudo mucho, dejo el código equivalente en java:

import java.util.ArrayList;
import java.util.List;

public class BoatMovements {
    /**
     * @return boolean The destination is reachable or not
     */
    public static boolean canTravelTo(boolean[][] gameMatrix, int fromRow, int fromColumn, 
                                    int toRow, int toColumn) {
        // Out of bounds
        if (toRow > gameMatrix.length - 1
            || fromRow > gameMatrix.length - 1
            || toColumn > gameMatrix[0].length - 1
            || fromColumn > gameMatrix[0].length - 1) {
            return false;
        }

        // Moving illegally
        if (
            // ... within the same column, up or down
            (fromRow != toRow && Math.abs(toRow - fromRow) > 1)
              // ... within the same row, left or once/twice (at most) right
              || (fromColumn != toColumn && ((fromColumn - toColumn) > 1 || (toColumn - fromColumn) > 2
            ))
        ) {
            return false;
        }
        
        if (fromRow == toRow) { // Moving horizontally
            List<Integer> toCheck = range(fromColumn, toColumn);
            for (int col : toCheck) {
                if (!gameMatrix[fromRow][col])
                    return false; // Path is blocked
            }
            return true;
        } else if (fromColumn == toColumn) { // Moving vertically
            List<Integer> toCheck = range(fromRow, toRow);
            for (int row : toCheck) {
                if (!gameMatrix[row][fromColumn])
                    return false; // Path is blocked
            }
            return true;
        } else {
            // Moving diagonally is not allowed
            return false;
        }
    }

    // Implementación de la función range de PHP
    private static List<Integer> range(int start, int end) {
        List<Integer> result = new ArrayList<>();
        if (start <= end) {
            for (int i = start; i <= end; i++) {
                result.add(i);
            }
        } else {
            for (int i = start; i >= end; i--) {
                result.add(i);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        boolean[][] gameMatrix = {
            {false, true,  true,  false, false, false},
            {true,  true,  true,  false, false, false},
            {true,  true,  true,  true,  true,  true},
            {false, true,  true,  false, true,  true},
            {false, true,  true,  true,  false, true},
            {false, false, false, false, false, false}
        };

        System.out.println(canTravelTo(gameMatrix, 3, 2, 2, 2)); // true, Valid move
        System.out.println(canTravelTo(gameMatrix, 3, 2, 3, 4)); // false, Can't travel through land
        System.out.println(canTravelTo(gameMatrix, 3, 2, 6, 2)); // false, Out of bounds
    }
}
Reasons:
  • Blacklisted phrase (2): código
  • Blacklisted phrase (3): solución
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Carlos Villegas