plduhoux
2/20/2018 - 1:37 AM

fillShortestPaths

char[][] fillShortestPaths(char[][] plan) {
    int targetX = -1;
    int targetY = -1;
    for (int i = 0; i < plan.length; i++) {
        for (int j = 0; j < plan[0].length; j++) {
            if (plan[i][j] == 's') {
                targetX = i;
                targetY = j;
            }
        }
    }
    
    if (targetX == -1 || targetY == -1) return plan;
    
    int length1 = plan.length;
    int length2 = plan[0].length;
    
    int lengthE = targetX;
    int lengthW = length1 - 1 - targetX;
    int lengthN = targetY;
    int lengthS = length2 - 1- targetY;
    
    int minLen = Math.min(lengthE, lengthW);
    minLen = Math.min(lengthN, minLen);
    minLen = Math.min(lengthS, minLen);
    if (lengthE == minLen) {
        for (int x = 0; x < targetX; x++) {
            int yOffset = targetX - x;
            for (int y = targetY - yOffset; y <= targetY + yOffset; y++) {
                int okY = Math.min(y, length2 - 1);
                okY = Math.max(0, okY);
                plan[x][okY] = '#';
            }
        }
    }
    if (lengthW == minLen) {
        for (int x = targetX + 1; x < length1; x++) {
            int yOffset = x - targetX;
            for (int y = targetY - yOffset; y <= targetY + yOffset; y++) {
                int okY = Math.min(y, length2 - 1);
                okY = Math.max(0, okY);
                plan[x][okY] = '#';
            }
        }
    }
    
    if (lengthN == minLen) {
        for (int y = 0; y < targetY; y++) {
            int xOffset = targetY - y;
            for (int x = targetX - xOffset; x <= targetX + xOffset; x++) {
                int okX = Math.min(x, length1 - 1);
                okX = Math.max(0, okX);
                plan[okX][y] = '#';
            }
        }
    }
    if (lengthS == minLen) {
        for (int y = targetY + 1; y < length2; y++) {
            int xOffset = y - targetY;
            for (int x = targetX - xOffset; x <= targetX + xOffset; x++) {
                int okX = Math.min(x, length1 - 1);
                okX = Math.max(0, okX);
                plan[okX][y] = '#';
            }
        }
    }
    
    return plan;
}