QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#348466#6739. Teleportwtz2333AC ✓283ms115124kbC++172.4kb2024-03-09 18:40:452024-03-09 18:40:46

Judging History

你现在查看的是最新测评结果

  • [2024-03-09 18:40:46]
  • 评测
  • 测评结果:AC
  • 用时:283ms
  • 内存:115124kb
  • [2024-03-09 18:40:45]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 5100;
int dis[maxn][maxn];
int dx[4] = {1,0,-1,0};
int dy[4] = {0,-1,0,1};
struct node{
    int x,y;
};
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,k;
    cin >> n >> k;
    vector<string> s(n);
    for(int i = 0;i < n;i ++) {
        cin >> s[i];
    }
    memset(dis,0x3f,sizeof dis);
    int inf = dis[0][0];
    dis[0][0] = 0;
    queue<node> q;
    q.push({0,0});
    // cerr << "!!!!\n";
    while(!q.empty()) {
        auto [x,y] = q.front();
        // cerr << x << " " << y << " " <<  opt << endl;
        q.pop();
        // cerr <<": asdfasd\n";
        if(x == n - 1 && y == n - 1) {
            cout << dis[x][y] << endl;
            return 0;
        }
        auto check = [&](int x,int y) -> bool {
            if(x < 0 || x >= n) return false;
            if(y < 0 || y >= n) return false;
            if(s[x][y] == '*') return false;
            if(dis[x][y] != inf) return false;
            return true;
        };
        

        for(int i = 0;i < k;i += 2) {
            int xx = y + 1 + (i / 2);
            int yy = x + (i / 2);
            if(check(xx,yy)) {
                if(dis[xx][yy] < dis[x][y] + 1) break;
                if(dis[xx][yy] <= dis[x][y] + 1)continue;
                dis[xx][yy] = dis[x][y] + 1;
                q.push({xx,yy});
            }
        }
        for(int i = 2;i <= k;i += 2) {
            int xx = x + (i / 2);
            int yy = y + (i / 2);
            if(check(xx,yy)) {
                if(dis[xx][yy] < dis[x][y] + 1) break;
                if(dis[xx][yy] == dis[x][y] + 1)continue;

                dis[xx][yy] = dis[x][y] + 1;
                // cerr << "!!!! :" << xx << " " << yy << endl;
                q.push({xx,yy});
            }
        }
        
         // cerr <<": asdfasd\n";
        for(int i = 0;i < 4;i ++) {
            int xx = x + dx[i];
            int yy = y + dy[i];
            if(check(xx,yy)) {
                if(dis[xx][yy] <= dis[x][y] + 1)continue;

                // cerr << xx << " " << yy << endl;
                dis[xx][yy] = dis[x][y] + 1;
                q.push({xx,yy});
            }
        }
        // cerr << "asdarewrew\n";
        // cerr << "asdfgasd\n";
    }
    cout << -1 << endl;
    return 0;
}

// 1 0 0 1 0 0 
// 0 1 0 0 1 0
// 0 0 0 0 0 0
// 1 0 0 1 0 0

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 105472kb

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 7ms
memory: 105188kb

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 27ms
memory: 106192kb

input:

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

output:

540

result:

ok 1 number(s): "540"

Test #4:

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

input:

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

output:

5

result:

ok 1 number(s): "5"

Test #5:

score: 0
Accepted
time: 22ms
memory: 106132kb

input:

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

output:

104

result:

ok 1 number(s): "104"

Test #6:

score: 0
Accepted
time: 8ms
memory: 106220kb

input:

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

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 8ms
memory: 106232kb

input:

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

output:

21

result:

ok 1 number(s): "21"

Test #8:

score: 0
Accepted
time: 283ms
memory: 114896kb

input:

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

output:

3354

result:

ok 1 number(s): "3354"

Test #9:

score: 0
Accepted
time: 68ms
memory: 114364kb

input:

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

output:

6

result:

ok 1 number(s): "6"

Test #10:

score: 0
Accepted
time: 91ms
memory: 115124kb

input:

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

output:

58

result:

ok 1 number(s): "58"