답안 #648705

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
648705 2022-10-07T14:05:14 Z alvinpiter Nautilus (BOI19_nautilus) C++17
100 / 100
292 ms 157304 KB
/*
dp[r][c][idx] -> Is it possible to reach cell (r, c) after executing idx-th command. idx starts from 1.

Base case:
dp[r][c][0] = 1 if isWater(r, c), 0 otherwise.

Transition:
command[idx] = 'N' -> dp[r][c][idx] = dp[r + 1][c][idx - 1] && isWater(r, c)
command[idx] = 'E' -> dp[r][c][idx] = dp[r][c - 1][idx - 1] && isWater(r, c)
command[idx] = 'S' -> dp[r][c][idx] = dp[r - 1][c][idx - 1] && isWater(r, c)
command[idx] = 'W' -> dp[r][c][idx] = dp[r][c + 1][idx - 1] && isWater(r, c)
command[idx] = '?' -> dp[r][c][idx] = (d[r + 1][c][idx - 1] & isWater(r, c)) | ...

Naive implementation will get TLE/MLE, because there are R*C*M states. We need C++'s bitset.

isWater(r) -> a bitset that represents whether a certain column in row r is filled with water.
dp[r][idx] -> a bitset that represents whether a certain column in row r can be reached after executing idx-th command.

command[idx] = 'N' -> dp[r][idx] = dp[r + 1][idx - 1] && isWater(r)
command[idx] = 'E' -> dp[r][idx] = (dp[r][idx - 1]) << 1) && isWater(r)
command[idx] = 'S' -> dp[r][idx] = dp[r - 1][idx - 1] && isWater(r)
command[idx] = 'W' -> dp[r][idx] = (dp[r][idx - 1] >> 1) && isWater(r)
command[idx] = '?' -> dp[r][idx] = (dp[r + 1][idx - 1] && isWater(r)) | ...

Notice the direction of the left shift and right shift. It is reversed because the bitset is indexed starting from 0
from the right.
*/

#include<bits/stdc++.h>
using namespace std;
#define MAXR 500
#define MAXM 5000

int R, C, M;
string command;
bitset<MAXR + 3> isWater[MAXR + 3], dp[MAXR + 3][MAXM + 3];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  cin >> R >> C >> M;

  for (int r = 1; r <= R; r++) {
    for (int c = 0; c < C; c++) {
      char cell;
      cin >> cell;

      if (cell == '.') {
        isWater[r].set(c, 1);
        dp[r][0].set(c, 1);
      }
    }
  }

  cin >> command;

  for (int idx = 1; idx <= M; idx++) {
    for (int r = 1; r <= R; r++) {
      switch (command[idx - 1]) {
        case 'N':
          dp[r][idx] = dp[r + 1][idx - 1] & isWater[r];
          break;
        case 'E':
          dp[r][idx] = (dp[r][idx - 1] << 1) & isWater[r];
          break;
        case 'S':
          dp[r][idx] = dp[r - 1][idx - 1] & isWater[r];
          break;
        case 'W':
          dp[r][idx] = (dp[r][idx - 1] >> 1) & isWater[r];
          break;
        case '?':
          dp[r][idx] = (dp[r + 1][idx - 1] & isWater[r]) | ((dp[r][idx - 1] << 1) & isWater[r]) | (dp[r - 1][idx - 1] & isWater[r]) | ((dp[r][idx - 1] >> 1) & isWater[r]);
          break;
      }
    }
  }

  int ans = 0;
  for (int r = 1; r <= R; r++) {
    for (int c = 0; c < C; c++) {
      if (dp[r][M].test(c)) {
        ans += 1;
      }
    }
  }

  cout << ans << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1364 KB Output is correct
2 Correct 2 ms 1360 KB Output is correct
3 Correct 2 ms 1364 KB Output is correct
4 Correct 2 ms 1364 KB Output is correct
5 Correct 1 ms 1364 KB Output is correct
6 Correct 1 ms 1364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1364 KB Output is correct
2 Correct 2 ms 1360 KB Output is correct
3 Correct 2 ms 1364 KB Output is correct
4 Correct 2 ms 1364 KB Output is correct
5 Correct 1 ms 1364 KB Output is correct
6 Correct 1 ms 1364 KB Output is correct
7 Correct 3 ms 1420 KB Output is correct
8 Correct 2 ms 1364 KB Output is correct
9 Correct 2 ms 1364 KB Output is correct
10 Correct 2 ms 1364 KB Output is correct
11 Correct 2 ms 1364 KB Output is correct
12 Correct 2 ms 1412 KB Output is correct
13 Correct 2 ms 1364 KB Output is correct
14 Correct 2 ms 1364 KB Output is correct
15 Correct 2 ms 1368 KB Output is correct
16 Correct 2 ms 1364 KB Output is correct
17 Correct 2 ms 1364 KB Output is correct
18 Correct 2 ms 1364 KB Output is correct
19 Correct 3 ms 1364 KB Output is correct
20 Correct 2 ms 1364 KB Output is correct
21 Correct 2 ms 1364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1364 KB Output is correct
2 Correct 2 ms 1360 KB Output is correct
3 Correct 2 ms 1364 KB Output is correct
4 Correct 2 ms 1364 KB Output is correct
5 Correct 1 ms 1364 KB Output is correct
6 Correct 1 ms 1364 KB Output is correct
7 Correct 3 ms 1420 KB Output is correct
8 Correct 2 ms 1364 KB Output is correct
9 Correct 2 ms 1364 KB Output is correct
10 Correct 2 ms 1364 KB Output is correct
11 Correct 2 ms 1364 KB Output is correct
12 Correct 2 ms 1412 KB Output is correct
13 Correct 2 ms 1364 KB Output is correct
14 Correct 2 ms 1364 KB Output is correct
15 Correct 2 ms 1368 KB Output is correct
16 Correct 2 ms 1364 KB Output is correct
17 Correct 2 ms 1364 KB Output is correct
18 Correct 2 ms 1364 KB Output is correct
19 Correct 3 ms 1364 KB Output is correct
20 Correct 2 ms 1364 KB Output is correct
21 Correct 2 ms 1364 KB Output is correct
22 Correct 217 ms 157228 KB Output is correct
23 Correct 161 ms 157276 KB Output is correct
24 Correct 147 ms 157252 KB Output is correct
25 Correct 162 ms 157304 KB Output is correct
26 Correct 166 ms 157172 KB Output is correct
27 Correct 221 ms 157160 KB Output is correct
28 Correct 219 ms 157196 KB Output is correct
29 Correct 229 ms 157212 KB Output is correct
30 Correct 240 ms 157256 KB Output is correct
31 Correct 213 ms 157148 KB Output is correct
32 Correct 268 ms 157136 KB Output is correct
33 Correct 256 ms 157172 KB Output is correct
34 Correct 258 ms 157240 KB Output is correct
35 Correct 253 ms 157232 KB Output is correct
36 Correct 292 ms 157164 KB Output is correct