Submission details
Task:Hypyt
Sender:rottis
Submission time:2025-10-30 19:56:53 +0200
Language:C++ (C++17)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
#40
#50
Test results
testverdicttimegroup
#10.00 s1, 2, 3, 4, 5details
#20.00 s1, 2, 3, 4, 5details
#30.00 s1, 2, 3, 4, 5details
#40.00 s1, 2, 3, 4, 5details
#50.00 s1, 2, 3, 4, 5details
#60.01 s2, 5details
#70.01 s2, 5details
#80.01 s2, 5details
#90.08 s3, 4, 5details
#100.09 s3, 4, 5details
#110.10 s3, 4, 5details
#120.09 s4, 5details
#130.10 s4, 5details
#140.12 s4, 5details
#150.15 s5details
#160.17 s5details
#170.20 s5details
#180.24 s5details
#190.53 s5details
#200.76 s5details
#21--5details
#220.00 s1, 2, 3, 4, 5details
#230.00 s1, 2, 3, 4, 5details
#240.06 s5details
#250.06 s5details
#260.13 s5details
#270.07 s5details

Code

#include <iostream>
#include <unordered_map>
#include <string.h> // memset
#include <vector>
#include <ctime>


typedef unsigned char uchar;



unsigned short height, width;
unsigned long query_count;
int main() {
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(NULL);

    std::vector<uchar> rows[250];
    std::vector<uchar> columns[250];

    // what is connected (and not connected) to what?
    // speeds up calculations resulting in -1
    // index with column_index or x
    int networks[250];
    memset(&networks, -1, 250 * sizeof(int));

    int cur_network_num = 0;

    std::cin >> height >> width >> query_count;
    std::clock_t start_time = std::clock();

    for (unsigned short i = 0; i < height; i++) {
        char buf[251];
        std::cin >> buf;

        for (int j = 0; j < width; j++) {
            if (buf[j] == '.') {
                rows[i].push_back(j);
                columns[j].push_back(i);
                //std::cout << "Connect row idx " << i << " to column idx " << j << std::endl;
            }
        }
    }
    std::clock_t input_complete_time = std::clock();

    
    std::vector<uchar> just_added_rows;
    std::vector<uchar> just_added_cols;
    just_added_rows.reserve(height);
    just_added_cols.reserve(width);

    bool available_rows[250];
    bool available_cols[250];

    std::vector<uchar> cols_to_add;    
    cols_to_add.reserve(width);
    
    std::string output;

    unsigned short start_y, start_x, end_y, end_x;
    //std::vector<std::clock_t> query_begin_times;
    for (unsigned long i = 0; i < query_count; i++) {
        //query_begin_times.push_back(std::clock());
        
        std::cin >> start_y >> start_x >> end_y >> end_x;
        start_y--; start_x--; end_y--; end_x--;

        if (start_x == end_x && start_y == end_y) {
            output.append("0\n");
            continue;
        }

        if (start_x == end_x || start_y == end_y) {
            output.append("1\n");
            continue;
        }

        // if both are -1, neither are set already, we can't do anything with that info
        // if one is not -1, we know they are not in the same network or else the
        // code that set the other to not be -1 would have set them both to not be -1
        if (networks[start_x] != networks[end_x] && (networks[start_x] != -1 || networks[end_x] != -1)) {
            output.append("-1\n");
            continue;
        }

        memset(&available_rows, 0, height);
        memset(&available_cols, 0, width);

        //memset(&just_added_rows, 0, height);
        //memset(&just_added_cols, 0, width);

        just_added_rows.clear();
        just_added_cols.clear();

        just_added_rows.push_back(start_y);
        just_added_cols.push_back(start_x);

        cols_to_add.clear();
        for (int cost = 2;; cost++) {
        
            // set to false when modifying rows_to_add and cols_to_add, if no modifications are made then break out with -1
            bool no_solution = true;

            for (uchar row_idx : just_added_rows) {
                if (available_rows[row_idx]) { continue; }
                
                //std::cout << "Row " << (int) row_idx << std::endl;
                //just_added_rows[row_idx] = false;
                available_rows[row_idx] = true;

                // if no cols to add, don't set no_solution to false
                no_solution = no_solution && rows[row_idx].size() == 0;

                for (uchar col_idx : rows[row_idx]) {
                    if (available_cols[col_idx]) { continue; }

                    if (col_idx == end_x) {
                        output.append(std::to_string(cost));
                        output.push_back('\n');
                        goto complete;
                    }
                    //std::cout << "Add col " << (int) col_idx << std::endl;
                    cols_to_add.push_back(col_idx);
                }
            }
            just_added_rows.clear();

            for (uchar col_idx : just_added_cols) {
                if (available_cols[col_idx]) { continue; }

                //std::cout << "Col " << (int) col_idx << std::endl;
                //just_added_cols[col_idx] = false;
                available_cols[col_idx] = true;

                // if no cols to add, don't set no_solution to false
                no_solution = no_solution && columns[col_idx].size() == 0;

                for (uchar row_idx : columns[col_idx]) {
                    if (available_rows[row_idx]) { continue; }

                    if (row_idx == end_y) {
                        output.append(std::to_string(cost));
                        output.push_back('\n');
                        goto complete;
                    }
                    //std::cout << "Add row " << (int) row_idx << std::endl;
                    just_added_rows.push_back(row_idx);
                }
            }
            just_added_cols.clear();

            if (no_solution) {
                output.append("-1\n");
                // store network map for use in later calls
                for (uchar i = 0; i < 250; i++) {
                    if (available_cols[i]) {
                        networks[i] = cur_network_num;
                    }
                }
                cur_network_num++;
                goto complete;
            }

            while (cols_to_add.size()) {
                just_added_cols.push_back(cols_to_add.back());
                cols_to_add.pop_back();
            }
        }
complete:
        continue;
    }
    std::cout << output;

    std::clock_t complete_time = std::clock();

    std::cout << "First split (input parsing) completed in " << (input_complete_time - start_time) / (double) CLOCKS_PER_SEC << std::endl;
    std::cout << "Processing completed in " << (complete_time - input_complete_time) / (double) CLOCKS_PER_SEC << std::endl;
    std::cout << "Program finished in " << (complete_time - start_time) / (double) CLOCKS_PER_SEC << std::endl;
    
    std::cout << "\nExtrapolated time splits:\n";
    std::cout << "First split (input parsing) completed in " << (100*input_complete_time - 100*start_time) / (double) CLOCKS_PER_SEC << std::endl;
    std::cout << "Processing completed in " << (100*complete_time - 100*input_complete_time) / (double) CLOCKS_PER_SEC << std::endl;
    std::cout << "Program finished in " << (100*complete_time - 100*start_time) / (double) CLOCKS_PER_SEC << std::endl;
    //*/
    return 0;
}

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
1
0
3
3
-1
...

Feedback: Output is longer than expected

Test 2

Group: 1, 2, 3, 4, 5

Verdict:

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

correct output
1
2
1
2
2
...

user output
1
2
1
2
2
...

Feedback: Output is longer than expected

Test 3

Group: 1, 2, 3, 4, 5

Verdict:

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

correct output
1
2
2
1
2
...

user output
1
2
2
1
2
...

Feedback: Output is longer than expected

Test 4

Group: 1, 2, 3, 4, 5

Verdict:

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

correct output
3
4
2
3
4
...

user output
3
4
2
3
4
...

Feedback: Output is longer than expected

Test 5

Group: 1, 2, 3, 4, 5

Verdict:

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

correct output
7

user output
7
First split (input parsing) co...

Feedback: Output is longer than expected

Test 6

Group: 2, 5

Verdict:

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

correct output
2
3
3
2
2
...

user output
2
3
3
2
2
...

Feedback: Output is longer than expected

Test 7

Group: 2, 5

Verdict:

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

correct output
2
2
2
2
3
...

user output
2
2
2
2
3
...

Feedback: Output is longer than expected

Test 8

Group: 2, 5

Verdict:

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

correct output
2
3
3
3
3
...

user output
2
3
3
3
3
...

Feedback: Output is longer than expected

Test 9

Group: 3, 4, 5

Verdict:

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

correct output
2
2
2
2
2
...

user output
2
2
2
2
2
...

Feedback: Output is longer than expected

Test 10

Group: 3, 4, 5

Verdict:

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

correct output
2
1
3
2
2
...

user output
2
1
3
2
2
...

Feedback: Output is longer than expected

Test 11

Group: 3, 4, 5

Verdict:

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

correct output
3
3
3
3
3
...

user output
3
3
3
3
3
...

Feedback: Output is longer than expected

Test 12

Group: 4, 5

Verdict:

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

correct output
2
2
2
2
2
...

user output
2
2
2
2
2
...

Feedback: Output is longer than expected

Test 13

Group: 4, 5

Verdict:

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

correct output
3
2
2
3
2
...

user output
3
2
2
3
2
...

Feedback: Output is longer than expected

Test 14

Group: 4, 5

Verdict:

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

correct output
2
3
1
2
2
...

user output
2
3
1
2
2
...

Feedback: Output is longer than expected

Test 15

Group: 5

Verdict:

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

correct output
3
2
2
2
2
...

user output
3
2
2
2
2
...

Feedback: Output is longer than expected

Test 16

Group: 5

Verdict:

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

correct output
2
2
2
2
2
...

user output
2
2
2
2
2
...

Feedback: Output is longer than expected

Test 17

Group: 5

Verdict:

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

correct output
3
3
2
2
2
...

user output
3
3
2
2
2
...

Feedback: Output is longer than expected

Test 18

Group: 5

Verdict:

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

correct output
3
3
3
3
3
...

user output
3
3
3
3
3
...

Feedback: Output is longer than expected

Test 19

Group: 5

Verdict:

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

correct output
104
422
145
93
65
...

user output
104
422
145
93
65
...

Feedback: Output is longer than expected

Test 20

Group: 5

Verdict:

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

correct output
57
155
38
65
98
...

user output
57
155
38
65
98
...

Feedback: Output is longer than expected

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
1
1
0
0
...

Feedback: Output is longer than expected

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: Output is longer than expected

Test 24

Group: 5

Verdict:

input
250 1 200000
*
.
*
.
...

correct output
1
1
1
1
1
...

user output
1
1
1
1
1
...

Feedback: Output is longer than expected

Test 25

Group: 5

Verdict:

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

correct output
1
1
1
1
1
...

user output
1
1
1
1
1
...

Feedback: Output is longer than expected

Test 26

Group: 5

Verdict:

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

correct output
2
2
2
2
2
...

user output
2
2
2
2
2
...

Feedback: Output is longer than expected

Test 27

Group: 5

Verdict:

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

correct output
0
0
0
0
0
...

user output
0
0
0
0
0
...

Feedback: Output is longer than expected