• + 0 comments

    import java.io.; import java.util.; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList;

    class Result {

    /*
     * Complete the 'cavityMap' function below.
     *
     * The function is expected to return a STRING_ARRAY.
     * The function accepts STRING_ARRAY grid as parameter.
     */
    
    public static List<String> cavityMap(List<String> grid) {
        int n = grid.size();
        List<String> result = new ArrayList<>(grid);
    
        for (int i = 1; i < n - 1; i++) {
            char[] row = grid.get(i).toCharArray();
            for (int j = 1; j < row.length - 1; j++) {
                int current = row[j] - '0';
                int top = grid.get(i - 1).charAt(j) - '0';
                int bottom = grid.get(i + 1).charAt(j) - '0';
                int left = row[j - 1] - '0';
                int right = row[j + 1] - '0';
    
                if (current > top && current > bottom && current > left && current > right) {
                    row[j] = 'X';
                }
            }
            result.set(i, new String(row));
        }
    
        return result;
    }
    

    }

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int n = Integer.parseInt(bufferedReader.readLine().trim());
    
        List<String> grid = IntStream.range(0, n).mapToObj(i -> {
            try {
                return bufferedReader.readLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }).collect(toList());
    
        List<String> result = Result.cavityMap(grid);
    
        bufferedWriter.write(
            result.stream()
                .collect(joining("\n")) + "\n"
        );
    
        bufferedReader.close();
        bufferedWriter.close();
    }
    

    }