QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#71500#2662. Bombermanpibuxd0 2ms3592kbC++144.6kb2023-01-10 23:21:092023-01-10 23:21:11

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-10 23:21:11]
  • 评测
  • 测评结果:0
  • 用时:2ms
  • 内存:3592kb
  • [2023-01-10 23:21:09]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define all(x) x.begin(), x.end()

int n, INF = 1e8;
array<int, 2> P, K;
vector<vector<char>> a;
vector<vector<int>> dp, dk; // dist od (p,k)
vector<vector<vector<int>>> DPP, DPK; // dp od (p,k)

bool e(int i, int j, const vector<vector<int>> &d, const vector<vector<char>> &A){ // ok
  return (1 <= i && i <= n && 1 <= j && j <= n && d[i][j] == INF && A[i][j] != 'X' && A[i][j] != '#');
}

void bfsd(array<int, 2> &start, vector<vector<int>> &d, vector<vector<char>> &A){ // ok
  queue<array<int, 2>> Q;
  Q.push(start);
  d[start[0]][start[1]] = 0;

  while(!Q.empty()){
    auto v = Q.front();
    Q.pop();

    if(e(v[0]+1, v[1], d, A)){
      Q.push({v[0]+1, v[1]});
      d[v[0]+1][v[1]] = d[v[0]][v[1]] + 1;
    }
    if(e(v[0], v[1]+1, d, A)){
      Q.push({v[0], v[1]+1});
      d[v[0]][v[1]+1] = d[v[0]][v[1]] + 1;
    }
    if(e(v[0]-1, v[1], d, A)){
      Q.push({v[0]-1, v[1]});
      d[v[0]-1][v[1]] = d[v[0]][v[1]] + 1;
    }
    if(e(v[0], v[1]-1, d, A)){
      Q.push({v[0], v[1]-1});
      d[v[0]][v[1]-1] = d[v[0]][v[1]] + 1;
    }
  }
}

bool e2(int i, int j, const vector<vector<char>> &A){ // ok
  return (1 <= i && i <= n && 1 <= j && j <= n && A[i][j] != 'X');
}

bool e3(int I, int J, int i, int j, const vector<vector<int>> &DS){ // ok
  return (1 <= i && i <= n && 1 <= j && j <= n && 
          1 <= I && I <= n && 1 <= J && J <= n &&
          DS[I][J] == DS[i][j] + 1);
}

char ju(array<int, 2> &PK, const vector<vector<int>> &DS){ // ok
  if(e3(PK[0], PK[1], PK[0]-1, PK[1], DS)){ // G (D)
    PK[0]--;
    return 'D';
  }
  if(e3(PK[0], PK[1], PK[0]+1, PK[1], DS)){ // D (G)
    PK[0]++;
    return 'G';
  }
  if(e3(PK[0], PK[1], PK[0], PK[1]-1, DS)){ // L (P)
    PK[1]--;
    return 'P';
  }
  if(e3(PK[0], PK[1], PK[0], PK[1]+1, DS)){ // P (L)
    PK[1]++;
    return 'L';
  }
  return 'X';
}

vector<char> droga(array<int, 2> &B){ // ok
  vector<vector<char>> A = a;
  int o = B[1];
  while(e2(B[0], o, a)){
    A[B[0]][o] = '.';
    o--;
  }
  o = B[1];
  while(e2(B[0], o, a)){
    A[B[0]][o] = '.';
    o++;
  }
  o = B[0];
  while(e2(o, B[1], a)){
    A[o][B[1]] = '.';
    o--;
  }
  o = B[0];
  while(e2(o, B[1], a)){
    A[o][B[1]] = '.';
    o++;
  }
  vector<vector<int>> DS(n+2, vector<int>(n+2, INF));
  bfsd(P, DS, A);
  //cout << DS[K[0]][K[1]] << "\n";

  vector<char> dro;
  array<int, 2> PK = K;
  while(PK[0] != P[0] || PK[1] != P[1])
    dro.push_back(ju(PK, DS));
  reverse(all(dro));
  return dro;
}

int check(array<int, 2> B){
  return (min({
      DPP[1][B[0]][B[1]],
      DPP[2][B[0]][B[1]],
      DPP[3][B[0]][B[1]],
      DPP[4][B[0]][B[1]] 
  }) + min({
      DPK[1][B[0]][B[1]],
      DPK[2][B[0]][B[1]],
      DPK[3][B[0]][B[1]],
      DPK[4][B[0]][B[1]]
  }));
}

void preDP(vector<vector<vector<int>>> &DP, vector<vector<int>> &d){
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){ // L
      if(a[i][j] == 'X') DP[1][i][j] = INF;
      else DP[1][i][j] = min({d[i][j]-1, DP[1][i][j-1], d[i-1][j], d[i+1][j], d[i][j-1], d[i][j+1]}) + 1;
    }
  }
  for(int i = 1; i <= n; i++){
    for(int j = n; j >= 1; j--){ // P
      if(a[i][j] == 'X') DP[2][i][j] = INF;
      else DP[2][i][j] = min({d[i][j]-1, DP[2][i][j+1], d[i-1][j], d[i+1][j], d[i][j-1], d[i][j+1]}) + 1;
    }
  }
  for(int j = 1; j <= n; j++){
    for(int i = 1; i <= n; i++){ // G
      if(a[i][j] == 'X') DP[3][i][j] = INF;
      else DP[3][i][j] = min({d[i][j]-1, DP[3][i-1][j], d[i-1][j], d[i+1][j], d[i][j-1], d[i][j+1]}) + 1;
    }
  }
  for(int j = 1; j <= n; j++){
    for(int i = n; i >= 1; i--){ // D
      if(a[i][j] == 'X') DP[4][i][j] = INF;
      else DP[4][i][j] = min({d[i][j]-1, DP[4][i+1][j], d[i-1][j], d[i+1][j], d[i][j-1], d[i][j+1]}) + 1;
    }
  }
}

int main(){
  //fastio;
  cin >> n;

  a.resize(n+1, vector<char>(n+1));
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      cin >> a[i][j];
      if(a[i][j] == 'P') P = {i, j};
      else if(a[i][j] == 'K') K = {i, j};
    }
  }

  dp = dk = vector<vector<int>>(n+2, vector<int>(n+2, INF));
  bfsd(P, dp, a);
  bfsd(K, dk, a);
  
  DPP = DPK = vector<vector<vector<int>>>(5, vector<vector<int>>(n+2, vector<int>(n+2, INF)));
  preDP(DPP, dp);
  preDP(DPK, dk);

  int ilr = n*n+3;
  array<int, 2> B;

  for(int i = 1; i <= n; i++)
    for(int j = 1; j <= n; j++)
      if(a[i][j] != 'X'){
        int r = check({i, j});
        if(r < ilr)
          ilr = r, B = {i, j};
      }
  cout << ilr << "\n" << B[0] << " " << B[1] << "\n";
  vector<char> dro = droga(B);
  for(char x : dro) cout << x;
  cout << "\n";
}

詳細信息

Subtask #1:

score: 0
Memory Limit Exceeded

Test #1:

score: 10
Accepted
time: 0ms
memory: 3380kb

input:

5
P..X.
X....
..X..
....X
.X..K

output:

8
1 1
PPDPDDDP

result:

ok 

Test #2:

score: 0
Accepted
time: 0ms
memory: 3516kb

input:

5
....P
.XXXX
.....
XXXX.
K....

output:

16
1 1
LLLLDDPPPPDDLLLL

result:

ok 

Test #3:

score: 0
Accepted
time: 2ms
memory: 3448kb

input:

2
PX
.K

output:

2
1 1
DP

result:

ok 

Test #4:

score: 0
Accepted
time: 2ms
memory: 3416kb

input:

2
P.
K.

output:

1
1 1
D

result:

ok 

Test #5:

score: 0
Accepted
time: 2ms
memory: 3484kb

input:

2
P.
.K

output:

2
1 1
PD

result:

ok 

Test #6:

score: -10
Memory Limit Exceeded

input:

2
PX
XK

output:


result:


Subtask #2:

score: 0
Time Limit Exceeded

Test #28:

score: 20
Accepted
time: 1ms
memory: 3452kb

input:

10
..........
.########.
.#......#.
.#X##X#.#.
.#X#P.#X#.
.#X#..#X#.
.#X####X#.
.#XXXXX.#.
.#X####X#.
..X..K....

output:

16
4 7
PPGPPPDDDDDDLLLL

result:

ok 

Test #29:

score: 0
Accepted
time: 2ms
memory: 3352kb

input:

2
PK
X#

output:

1
1 1
P

result:

ok 

Test #30:

score: 0
Accepted
time: 2ms
memory: 3356kb

input:

2
#K
P#

output:

2
1 1
GP

result:

ok 

Test #31:

score: 0
Accepted
time: 2ms
memory: 3592kb

input:

50
..................................................
.#..............................................X.
..############################################..X.
#.............................................##X.
.############################################...X.
.............................................

output:

285
22 5
GGGGGGGGGGGGGGGGGGGGGGGLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLDDPDPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPDPPDDLLLGLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLDDDDDDDDDPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPDDDDDDDDDLLLLLLLLLGGLLLLLLLLLLLLLLLLLLLLLDDDDDDDDDDDDDDDDDDDDDDDDDDDD

result:

ok 

Test #32:

score: 0
Accepted
time: 2ms
memory: 3552kb

input:

50
K......#####X#####################################
###..#.....#X#####################################
......####.#X#####################################
...###.###.#X#####################################
......#.....X#####################################
#####.#..###X#########.................###...

output:

64
11 27
DDDDPPGGGGGGGGGGGGGGGGGGLLLLLLLLLLGGLLLLLGLGLLGGPPGGGLLLLGLLLLLL

result:

ok 

Test #33:

score: 0
Accepted
time: 0ms
memory: 3428kb

input:

10
P########X
##########
##########
##########
##########
##########
##########
##########
##########
X########K

output:

18
1 9
PPPPPPPPDDDDDDDDDP

result:

ok 

Test #34:

score: 0
Accepted
time: 0ms
memory: 3392kb

input:

11
K..........
XXXXXXXXXX.
#..........
.XXXXXXXXXX
#.........#
XXXXXXXXXX.
...........
.XXXXXXXXXX
...........
XXXXXXXXXX.
P..........

output:

70
5 1
PPPPPPPPPPGGLLLLLLLLLLGGPPPPPPPPPPGGLLLLLLLLLLGGPPPPPPPPPPGGLLLLLLLLLL

result:

ok 

Test #35:

score: 0
Accepted
time: 2ms
memory: 3440kb

input:

5
##..#
.PX#.
.XXX.
#.XK#
.#.#.

output:

8
1 5
GPPPDDDL

result:

ok 

Test #36:

score: -20
Time Limit Exceeded

input:

7
.......
.......
###X###
#PX.XK#
###X###
.......
.......

output:


result:


Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

0%