Submission details
Task:Hypyt
Sender:MattiDragon
Submission time:2025-10-27 20:34:37 +0200
Language:Java
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
#40
#50
Test results
testverdicttimegroup
#10.14 s1, 2, 3, 4, 5details
#20.14 s1, 2, 3, 4, 5details
#30.14 s1, 2, 3, 4, 5details
#40.14 s1, 2, 3, 4, 5details
#50.14 s1, 2, 3, 4, 5details
#6--2, 5details
#7--2, 5details
#8--2, 5details
#9--3, 4, 5details
#10--3, 4, 5details
#11--3, 4, 5details
#12--4, 5details
#13--4, 5details
#14--4, 5details
#15--5details
#16--5details
#17--5details
#18--5details
#19--5details
#20--5details
#21--5details
#220.14 s1, 2, 3, 4, 5details
#230.14 s1, 2, 3, 4, 5details
#24--5details
#25--5details
#26--5details
#27--5details

Code

import java.util.Arrays;
import java.util.BitSet;
import java.util.Random;
import java.util.Scanner;

public class E {
    private final BitSet[] rows;
    private final BitSet[] columns;

    private final int width;
    private final int height;

    private final short[] distanceToCol;
    private final short[] distanceToRow;

    private E(BitSet[] rows, BitSet[] columns) {
        this.rows = rows;
        this.columns = columns;
        height = rows.length;
        width = columns.length;
        distanceToCol = new short[width * height * width];
        distanceToRow = new short[width * height * height];

        Arrays.fill(distanceToRow, (short) -1);
        Arrays.fill(distanceToCol, (short) -1);
    }

    public static void main(String[] args) {
        var sc = new Scanner(System.in);
        var height = sc.nextInt();
        var width = sc.nextInt();
        var nQueries = sc.nextInt();
        sc.nextLine();

        var rows = new BitSet[height];
        for (var i = 0; i < height; i++) {
            var chars = sc.nextLine().toCharArray();
            var bitset = new BitSet();
            for (var j = 0; j < chars.length; j++) {
                if (chars[j] != '*') {
                    bitset.set(j);
                }
            }
            rows[i] = bitset;
        }

        var columns = new BitSet[width];
        for (var i = 0; i < columns.length; i++) {
            var bitset = new BitSet();
            for (var j = 0; j < rows.length; j++) {
                bitset.set(j, rows[j].get(i));
            }
            columns[i] = bitset;
        }

        var state = new E(rows, columns);
        for (var row = 0; row < height; row++) {
            for (var col = 0; col < width; col++) {
//                System.out.println(row + " " + col);
                state.doPathfind(row, col);
            }
        }

//        var row1 = sc.nextInt() - 1;
//        var col1 = sc.nextInt() - 1;
//        var row2 = sc.nextInt() - 1;
//        var col2 = sc.nextInt() - 1;
//        sc.nextLine();
        var random = new Random();
        for (var i = 0; i < nQueries; i++) {
            var row1 = random.nextInt(height);
            var col1 = random.nextInt(width);
            var row2 = random.nextInt(height);
            var col2 = random.nextInt(width);

            var colDistance = state.distanceToCol[col2 + row1 * width + col1 * width * height];
            var rowDistance = state.distanceToRow[row2 + row1 * height + col1 * height * height];
//            System.out.printf("%s, %s -> ", colDistance, rowDistance);
            if (colDistance == 0 && rowDistance == 0) {
                System.out.println(0);
            } else if (colDistance == -1 && rowDistance == -1) {
                System.out.println(-1);
            } else {
                System.out.println(Math.min(rowDistance, colDistance) + 1);
            }
        }
    }

    private void doPathfind(int row, int col) {
        // Visited rows and columns, don't touch these
        var visitedRows = new BitSet();
        var visitedCols = new BitSet();

        // Current distance from start cell
        short distance = 0;

        // The rows and columns that we are currently checking
        var activeRows = new BitSet();
        activeRows.set(row);
        var activeCols = new BitSet();
        activeCols.set(col);

        // Here we store rows and columns that we can reach from the active ones
        var nextRows = new BitSet();
        var nextCols = new BitSet();

        while (!activeRows.isEmpty() || !activeCols.isEmpty()) {
            //System.out.printf("d: %s, ar: %s, ac: %s%n", distance, activeRows, activeCols);
            for (var activeRow = activeRows.nextSetBit(0); activeRow != -1; activeRow = activeRows.nextSetBit(activeRow + 1)) {
                distanceToRow[activeRow + row * height + col * height * height] = distance;
                nextCols.or(rows[activeRow]);
            }
            for (var activeCol = activeCols.nextSetBit(0); activeCol != -1; activeCol = activeCols.nextSetBit(activeCol + 1)) {
                distanceToCol[activeCol + row * width + col * width * height] = distance;
                nextRows.or(columns[activeCol]);
            }

            {
                // Mark active as visited so that we don't go back
                visitedRows.or(activeRows);

                // Swap active and next to avoid allocations
                var temp = activeRows;
                activeRows = nextRows;
                nextRows = temp;

                // Remove visited from active
                activeRows.andNot(visitedRows);
                // Empty next
                nextRows.clear();
            }
            {
                // Same as above
                visitedCols.or(activeCols);

                var temp = activeCols;
                activeCols = nextCols;
                nextCols = temp;

                activeCols.andNot(visitedCols);
                nextCols.clear();
            }
            distance++;
        }
    }
}

Test details

Test 1 (public)

Group: 1, 2, 3, 4, 5

Verdict:

input
4 6 5
.*.***
*...**
*****.
*..*.*
...

correct output
1
0
3
3
-1

user output
0
2
1
1
1

Feedback: Incorrect character on line 1 col 1: expected "1", got "0"

Test 2

Group: 1, 2, 3, 4, 5

Verdict:

input
10 10 10
..........
.....*....
........*.
*.*....*..
...

correct output
1
2
1
2
2
...

user output
2
2
2
1
2
...

Feedback: Incorrect character on line 1 col 1: expected "1", got "2"

Test 3

Group: 1, 2, 3, 4, 5

Verdict:

input
10 10 10
*...***.**
*****.*...
**..**.**.
..**.**.*.
...

correct output
1
2
2
1
2
...

user output
2
1
2
2
2
...

Feedback: Incorrect character on line 1 col 1: expected "1", got "2"

Test 4

Group: 1, 2, 3, 4, 5

Verdict:

input
10 10 10
***.*.****
**********
*.********
.*.***.**.
...

correct output
3
4
2
3
4
...

user output
0
0
3
1
1
...

Feedback: Incorrect character on line 1 col 1: expected "3", got "0"

Test 5

Group: 1, 2, 3, 4, 5

Verdict:

input
10 10 1
.****.****
**.**..***
**********
*******..*
...

correct output
7

user output
0

Feedback: Incorrect character on line 1 col 1: expected "7", got "0"

Test 6

Group: 2, 5

Verdict:

input
250 250 250
.*...*.....*******..**...*.......

correct output
2
3
3
2
2
...

user output
(empty)

Test 7

Group: 2, 5

Verdict:

input
250 250 250
...*......**.**.*.*..**..*..**...

correct output
2
2
2
2
3
...

user output
(empty)

Test 8

Group: 2, 5

Verdict:

input
250 250 250
**..**..****.****.*.***.***..*...

correct output
2
3
3
3
3
...

user output
(empty)

Test 9

Group: 3, 4, 5

Verdict:

input
40 40 200000
...*.**.*..*.............*.*.....

correct output
2
2
2
2
2
...

user output
(empty)

Test 10

Group: 3, 4, 5

Verdict:

input
40 40 200000
**.**..*.*.*.******....****.*....

correct output
2
1
3
2
2
...

user output
(empty)

Test 11

Group: 3, 4, 5

Verdict:

input
40 40 200000
.*.*.**.*****.***.*.****.**.**...

correct output
3
3
3
3
3
...

user output
(empty)

Test 12

Group: 4, 5

Verdict:

input
80 80 200000
*....**.***..****...*.....*......

correct output
2
2
2
2
2
...

user output
(empty)

Test 13

Group: 4, 5

Verdict:

input
80 80 200000
.***.*..*.***..*****....**...*...

correct output
3
2
2
3
2
...

user output
(empty)

Test 14

Group: 4, 5

Verdict:

input
80 80 200000
*******.*****.*..*..****...***...

correct output
2
3
1
2
2
...

user output
(empty)

Test 15

Group: 5

Verdict:

input
250 250 200000
*....*..*..*..**..*.........**...

correct output
3
2
2
2
2
...

user output
(empty)

Test 16

Group: 5

Verdict:

input
250 250 200000
..*....*..*......*.**.*.*..***...

correct output
2
2
2
2
2
...

user output
(empty)

Test 17

Group: 5

Verdict:

input
250 250 200000
*..*.*****.*********.****.****...

correct output
3
3
2
2
2
...

user output
(empty)

Test 18

Group: 5

Verdict:

input
250 250 200000
*********.**********.******.**...

correct output
3
3
3
3
3
...

user output
(empty)

Test 19

Group: 5

Verdict:

input
250 250 200000
.*****************************...

correct output
104
422
145
93
65
...

user output
(empty)

Test 20

Group: 5

Verdict:

input
250 250 200000
..****************************...

correct output
57
155
38
65
98
...

user output
(empty)

Test 21

Group: 5

Verdict:

input
250 250 200000
.*****************************...

correct output
498
498
498
498
498
...

user output
(empty)

Test 22

Group: 1, 2, 3, 4, 5

Verdict:

input
10 1 10
*
*
.
*
...

correct output
0
1
1
0
0
...

user output
0
0
1
1
0
...

Feedback: Incorrect character on line 2 col 1: expected "1", got "0"

Test 23

Group: 1, 2, 3, 4, 5

Verdict:

input
1 10 10
........*.
1 7 1 10
1 4 1 7
1 5 1 1
...

correct output
1
1
1
1
1
...

user output
1
1
1
1
1
...

Feedback: Incorrect character on line 7 col 1: expected "0", got "1"

Test 24

Group: 5

Verdict:

input
250 1 200000
*
.
*
.
...

correct output
1
1
1
1
1
...

user output
(empty)

Test 25

Group: 5

Verdict:

input
1 250 200000
*.*.*...*.*.**.***..**.*.*..**...

correct output
1
1
1
1
1
...

user output
(empty)

Test 26

Group: 5

Verdict:

input
250 250 200000
.................................

correct output
2
2
2
2
2
...

user output
(empty)

Test 27

Group: 5

Verdict:

input
250 250 200000
******************************...

correct output
0
0
0
0
0
...

user output
(empty)