QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#128428#6739. TeleportNicolas125841AC ✓450ms250764kbC++173.0kb2023-07-20 23:49:092023-07-20 23:49:12

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-20 23:49:12]
  • Judged
  • Verdict: AC
  • Time: 450ms
  • Memory: 250764kb
  • [2023-07-20 23:49:09]
  • Submitted

answer

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;

const int inf = 1e9;

int dr[4] = {0, 0, 1, -1};
int dc[4] = {-1, 1, 0, 0};

int main(){
    cin.tie(NULL)->sync_with_stdio(false);

    int n, k;
    cin >> n >> k;

    vector<string> grid(n);
    for(int i = 0; i < n; i++)
        cin >> grid[i];

    for(int i = 0; i < n; i++)
        for(int j = i; j < n; j++)
            swap(grid[i][j], grid[j][i]);

    //for(int i = 0; i < n; i++)
    //    cout << grid[i] << "\n";

    vector<vector<int>> paths(n, vector<int>());
    vector<vector<pii>> path(n, vector<pii>(n)), rpath(n, vector<pii>());

    for(int i = 0; i < n; i++){
        int r = i;
        int c = 0;
        int ind = 0;
        
        while(r < n && c < n){ 
            path[r][c] = {i, ind};
            rpath[i].push_back({r, c});
            paths[i].push_back(ind++);

            r++;

            swap(r, c);
        }
    }

    vector<vector<int>> d(n, vector<int>(n, inf));
    queue<pii> q;

    d[0][0] = 0;
    q.emplace(0, 0);
    
    //paths[0].erase(lower_bound(paths[0].begin(), paths[0].end(), path[0].second));

    while(!q.empty()){
        auto [row, col] = q.front();

        if(row == n-1 && col == n-1){
            cout << d[row][col] << "\n";
            return 0;
        }

        q.pop();

        for(int i = 0; i < 4; i++){
            int nrow = row + dr[i];
            int ncol = col + dc[i];

            if(nrow >= 0 && nrow < n && ncol >= 0 && ncol < n && grid[nrow][ncol] == '.' && d[nrow][ncol] == inf){
                d[nrow][ncol] = d[row][col] + 1;
                q.emplace(nrow, ncol);

                //vector<int> &path_ref = paths[path[compact(nrow, ncol, n)].first];
                //path_ref.erase(lower_bound(path_ref.begin(), path_ref.end(), path[compact(nrow, ncol, n)].second));
            }
        }

        int cnt = 0;
        int nrow = row;
        int ncol = col;

        while(cnt <= k){ 
            if(nrow < n && ncol < n){
                if(d[nrow][ncol] == inf && grid[nrow][ncol] == '.'){
                    d[nrow][ncol] = d[row][col] + 1;
                    q.emplace(nrow, ncol);
                }
            }

            nrow++;
            cnt++;

            swap(nrow, ncol);
        }

        /*vector<int> &path_ref = paths[path[loc].first];
        auto start_ind = lower_bound(path_ref.begin(), path_ref.end(), path[loc].second); 
        auto current_ind = start_ind;

        while(current_ind != path_ref.end() && *current_ind - path[loc].second <= k){
            int jmploc = rpath[path[loc].first][*current_ind];

            if(grid[get_r(jmploc, n)][get_c(jmploc, n)] == '.' && d[jmploc] == inf){
                d[jmploc] = d[loc] + 1;
                q.push(jmploc);
            }

            current_ind++;
        }

        if(start_ind != path_ref.end())
            path_ref.erase(start_ind, current_ind);*/
    }
    
    
    cout << "-1\n";
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3464kb

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3556kb

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 32ms
memory: 29788kb

input:

961 4
...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....

output:

540

result:

ok 1 number(s): "540"

Test #4:

score: 0
Accepted
time: 20ms
memory: 30492kb

input:

975 434
.*......*...*....*......*..*...*...**....*....*.......*...*.....*..*..*.*.*..*.*..*..*.*....*.*.*..*...*.*.....*......*.*...*......*..*..*......**..**.......*...*.*..*..*.*.**....*.*.*.....*....**.*..*..**.*..*.....*.*......*..*...*..*...*....**...*....*..*.*.*...........*..*.**.*.*..*.*..**...

output:

5

result:

ok 1 number(s): "5"

Test #5:

score: 0
Accepted
time: 23ms
memory: 30052kb

input:

966 19
..*.*.*........*.*..*.**..*....*..*..*....*.**..*.*.*..*.*.*..**....*.*.*....*.....*...........*.*..*.....*..*...*....*.*...*.*...*....*...*...*.*.*....*.*....**.*.......*....*.*.*...*..*..*..*.*...*...*...*..*..*..........*.*....*.*......*...*..**....*.*....**.....*..*..*..*.*....*...*..*.*....

output:

104

result:

ok 1 number(s): "104"

Test #6:

score: 0
Accepted
time: 26ms
memory: 30444kb

input:

973 199
..**.*...*.*...*.*.*.*.**..*.*.*..*......*.....*.*.*..*...**.....*.*..*.*..*...*....*..*...*....*.*...*.*......*..*.*.*.*......**......*.*.*.*.*.*...*...*...*....*......*.*.*.*..*..*..*...*..*.*....*....*.*...*..*..*.*..**.**..*.**....*.*...*..**..**...*......*.....*.....*..*.*.......*.*.*.....

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 11ms
memory: 31024kb

input:

984 95
.....*.*...*....*..*....*.*....**.**......*....*.*.*.......*.....*.*..*....*..*....*.*.....*..*.......*.*......*.*....*.....*......*...*....*....*......*....*.*...*........*......*.*....*.*...*....*..**....*.*.*.**....*..*.*..*..*.*......*..*......*.*......*...*......*.......*...*....*...**.....

output:

21

result:

ok 1 number(s): "21"

Test #8:

score: 0
Accepted
time: 450ms
memory: 250764kb

input:

2996 2
..*..*.....*..*.....*....**..*...........*..*........*..*..*.*..*..*.*.*.**.*.....**.*.......*.......*..*....*.....*..*.*.*....**.....*..**.....*.....*.......*.*.*.*....*...*..*.**......**..*.*...**..*..*......*..*.*...*......*.*.*.*...**.**..*.*........*.*..*...**......*.*..*.*..*....*.*.*.*...

output:

3354

result:

ok 1 number(s): "3354"

Test #9:

score: 0
Accepted
time: 144ms
memory: 236280kb

input:

2905 1023
.........*..*.**.....*...*.*....*.*.**.*..*...*..*....*...*.**.*.*.**..*......*...*.**..*...*..*.........*.........*.*.....**.*.*........*.....*.*....*..*.*.....*..*..*..*.*..*....**...*.*..*....*......*.........*.*.*.*......**.**.**..*.*.*..*..**..*..*...*...*.*.......*..*..*....*.*.........

output:

6

result:

ok 1 number(s): "6"

Test #10:

score: 0
Accepted
time: 167ms
memory: 247908kb

input:

2978 104
.*.........*...**.....*...*........*..*...*.*.*.....*........*.*...*.....*...*....*...*..*...*..*..*....*...*..*.*....*....*...*.......*.*.....*.*.......*.*..*...*.*.......*.*.*..........*.*..*.*..**.*....*.*.*..*...*.*.*.....*....*...*.*.*.*.........*.*.....**.*.*..*.*....*........*...*.**...

output:

58

result:

ok 1 number(s): "58"