QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#140982#6739. TeleportUFRJ#AC ✓571ms48288kbC++201.5kb2023-08-17 03:09:342023-08-17 03:09:35

Judging History

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

  • [2023-08-17 03:09:35]
  • 评测
  • 测评结果:AC
  • 用时:571ms
  • 内存:48288kb
  • [2023-08-17 03:09:34]
  • 提交

answer

#include<bits/stdc++.h>

using lint = int64_t;
using namespace std;

int main(void) {
  cin.tie(nullptr)->sync_with_stdio(false);
  int n, k;
  cin>>n>>k;
  vector<string>s(n);
  for(int i=0;i<n;i++) cin>>s[i];
  vector<pair<int, int>>cur;
  vector<vector<int>>dist(n, vector<int>(n, -1));
  dist[0][0] = 0;
  cur.push_back({0, 0});
  vector<int>dx = {0, 1, 0, -1};
  vector<int>dy = {1, 0, -1, 0};
  
  while(!cur.empty()){
    vector<pair<int, int>>nxt;
    sort(cur.begin(), cur.end(), [&](pair<int, int>a, pair<int, int>b){
      return a.first + a.second > b.first + b.second;
    });
    for(auto [a, b] : cur){
      for(int i=2;i<=k;i+=2){
        int na = a + i/2, nb = b + i/2;
        if(na < 0 || na >= n || nb < 0 || nb >= n || s[na][nb] == '*') continue;
        if(dist[na][nb] != -1) break;
        dist[na][nb] = dist[a][b] + 1;
        nxt.push_back({na, nb});
      }
      for(int i=1;i<=k;i+=2){
        int na = b + 1 + i/2, nb = a + i / 2;
        if(na < 0 || na >= n || nb < 0 || nb >= n || s[na][nb] == '*') continue;
        if(dist[na][nb] != -1) break;
        dist[na][nb] = dist[a][b] + 1;
        nxt.push_back({na, nb});
      }
    }
    for(auto [a, b] : cur){
      for(int k=0;k<4;k++){
        int na = a + dx[k], nb = b + dy[k];
        if(na < 0 || na >= n || nb < 0 || nb >= n || s[na][nb] == '*' || dist[na][nb] != -1) continue;
        dist[na][nb] = dist[a][b] + 1;
        nxt.push_back({na, nb});
      }
    }
    swap(nxt, cur);
  }
  cout<<dist[n-1][n-1]<<"\n";

  return 0;
}

详细

Test #1:

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

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

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

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 45ms
memory: 7880kb

input:

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

output:

540

result:

ok 1 number(s): "540"

Test #4:

score: 0
Accepted
time: 53ms
memory: 7976kb

input:

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

output:

5

result:

ok 1 number(s): "5"

Test #5:

score: 0
Accepted
time: 50ms
memory: 7940kb

input:

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

output:

104

result:

ok 1 number(s): "104"

Test #6:

score: 0
Accepted
time: 56ms
memory: 7800kb

input:

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

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 52ms
memory: 8008kb

input:

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

output:

21

result:

ok 1 number(s): "21"

Test #8:

score: 0
Accepted
time: 498ms
memory: 48288kb

input:

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

output:

3354

result:

ok 1 number(s): "3354"

Test #9:

score: 0
Accepted
time: 548ms
memory: 45608kb

input:

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

output:

6

result:

ok 1 number(s): "6"

Test #10:

score: 0
Accepted
time: 571ms
memory: 47820kb

input:

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

output:

58

result:

ok 1 number(s): "58"