QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#64640 | #4432. Jungle Trail | As3b_team_f_masr | AC ✓ | 1540ms | 293676kb | C++ | 5.6kb | 2022-11-25 04:58:39 | 2022-11-25 04:58:40 |
Judging History
answer
#include <bits/stdc++.h>
typedef long double ld;
typedef long long ll;
using namespace std;
int di[] = {1, 0, -1, -1, 0, 1, -1, 1};
int dj[] = {1, 1, 0, -1, -1, 0, 1, -1};
const ll oo = 1e18, MOD = 998244353;
const int N = 2005, M = 1e6 + 5;
const ld PI = acos(-1.0), EPS = 1e-9;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int t, n, m, vis[N][N][2][2][2][2], id;
char rows[N], cols[N], path[N * 2], a[N][N];
bool dp[N][N][2][2][2][2];
bool solve(int i, int j, int doner, int donec, int snaker, int snakec) {
bool &ret = dp[i][j][doner][donec][snaker][snakec];
if (i == n - 1 && j == m - 1) return ret = 1;
if (vis[i][j][doner][donec][snaker][snakec] == id) return ret;
vis[i][j][doner][donec][snaker][snakec] = id;
ret = 0;
if (i != n - 1 && a[i + 1][j] != '#') {
if (a[i + 1][j] == '.' || (a[i + 1][j] == 'O' && !donec) || (a[i + 1][j] == '@' && donec))
ret |= solve(i + 1, j, 0, donec, a[i + 1][j] == 'O', snakec | (a[i + 1][j] == 'O'));
if (a[i + 1][j] == '@' && !donec) ret |= solve(i + 1, j, 1, 0, 0, snakec);
if (a[i + 1][j] == '@' && !donec && !snakec) ret |= solve(i + 1, j, 0, 1, 0, 0);
if (a[i + 1][j] == 'O' && donec) ret |= solve(i + 1, j, 1, 1, 1, 1);
}
if (j != m - 1 && a[i][j + 1] != '#') {
if (a[i][j + 1] == '.' || (a[i][j + 1] == 'O' && !doner) || (a[i][j + 1] == '@' && doner))
ret |= solve(i, j + 1, doner, 0, snaker | (a[i][j + 1] == 'O'), a[i][j + 1] == 'O');
if (a[i][j + 1] == '@' && !doner) ret |= solve(i, j + 1, 0, 1, snaker, 0);
if (a[i][j + 1] == '@' && !doner && !snaker) ret |= solve(i, j + 1, 1, 0, 0, 0);
if (a[i][j + 1] == 'O' && doner) ret |= solve(i, j + 1, 1, 1, 1, 1);
}
return ret;
}
void build(int i, int j, int doner, int donec, int snaker, int snakec) {
if (i == n - 1 && j == m - 1) return;
if (i != n - 1 && a[i + 1][j] != '#') {
if (a[i + 1][j] == '.' || (a[i + 1][j] == 'O' && !donec) || (a[i + 1][j] == '@' && donec)) {
if (dp[i + 1][j][0][donec][a[i + 1][j] == 'O'][snakec | (a[i + 1][j] == 'O')]) {
path[i + j] = 'D';
build(i + 1, j, 0, donec, a[i + 1][j] == 'O', snakec | (a[i + 1][j] == 'O'));
return;
}
}
if (a[i + 1][j] == '@' && !donec) {
if (dp[i + 1][j][1][0][0][snakec]) {
path[i + j] = 'D', rows[i + 1] = 'T';
build(i + 1, j, 1, 0, 0, snakec);
return;
}
}
if (a[i + 1][j] == '@' && !donec && !snakec) {
if (dp[i + 1][j][0][1][0][0]) {
path[i + j] = 'D', cols[j] = 'T';
build(i + 1, j, 0, 1, 0, 0);
return;
}
}
if (a[i + 1][j] == 'O' && donec) {
if (dp[i + 1][j][1][1][1][1]) {
path[i + j] = 'D', rows[i + 1] = 'T';
build(i + 1, j, 1, 1, 1, 1);
return;
}
}
}
if (j != m - 1 && a[i][j + 1] != '#') {
if (a[i][j + 1] == '.' || (a[i][j + 1] == 'O' && !doner) || (a[i][j + 1] == '@' && doner)) {
if (dp[i][j + 1][doner][0][snaker | (a[i][j + 1] == 'O')][a[i][j + 1] == 'O']) {
path[i + j] = 'P';
build(i, j + 1, doner, 0, snaker | (a[i][j + 1] == 'O'), a[i][j + 1] == 'O');
return;
}
}
if (a[i][j + 1] == '@' && !doner) {
if (dp[i][j + 1][0][1][snaker][0]) {
path[i + j] = 'P', cols[j + 1] = 'T';
build(i, j + 1, 0, 1, snaker, 0);
return;
}
}
if (a[i][j + 1] == '@' && !doner && !snaker) {
if (dp[i][j + 1][1][0][0][0]) {
path[i + j] = 'P', rows[i] = 'T';
build(i, j + 1, 1, 0, 0, 0);
return;
}
}
if (a[i][j + 1] == 'O' && doner) {
if (dp[i][j + 1][1][1][1][1]) {
path[i + j] = 'P', cols[j + 1] = 'T';
build(i, j + 1, 1, 1, 1, 1);
return;
}
}
}
}
#define endl '\n'
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
//freopen("farm.in", "r", stdin);
//memset(dp, -1, sizeof dp);
cin >> t;
while (t--) {
id++;
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cin >> a[i][j];
}
bool no = 0;
for (int i = 0; i < n; i++) rows[i] = 'N';
for (int i = 0; i < m; i++) cols[i] = 'N';
if (solve(0, 0, 0, 0, a[0][0] == 'O', a[0][0] == 'O') && a[0][0] != '#' && a[0][0] != '@')
build(0, 0, 0, 0, a[0][0] == 'O', a[0][0] == 'O');
else if (a[0][0] == '@' && solve(0, 0, 1, 0, 0, 0))
build(0, 0, 1, 0, 0, 0), rows[0] = 'T';
else if (a[0][0] == '@' && solve(0, 0, 0, 1, 0, 0))
build(0, 0, 0, 1, 0, 0), cols[0] = 'T';
else no = 1;
if (no) cout << "NIE" << endl;
else {
cout << "TAK" << endl;
for (int i = 0; i < n; i++) cout << rows[i];
cout << endl;
for (int i = 0; i < m; i++) cout << cols[i];
cout << endl;
for (int i = 0; i < n + m - 2; i++) cout << path[i];
cout << endl;
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1540ms
memory: 293676kb
input:
486 4 5 ..#.. @@O@@ ##@#O ..@.@ 2 2 OO OO 2 2 @@ @@ 2 2 @@ #@ 2 2 @O #@ 2 2 @@ OO 2 2 O# @O 2 2 @# #@ 2 2 @. .@ 2 2 @# .O 2 2 OO .O 10 10 @O@O#O@@@# OO#@#@@#OO #@#@#O##O@ OO##@@O#@O O##@@#@O#@ OO@OO@@@O@ @O#@#@O#@O @OOOOO@##. O@OOO##O@@ OO@@OOOO#@ 10 10 @@#OOO#O@@ #@@OO@@.O@ #.O@@O#@@O OO@@#O@#O@ .#...
output:
TAK NTNN NNTNT DPPDDPP TAK NN NN DP TAK TT NN DP TAK TT NN PD TAK TN NT PD TAK TN NN DP TAK NT NT DP NIE TAK TN NT DP TAK TN NN DP TAK NN NN DP NIE TAK TTNNTTTTNT NNTTNNTTNN PDDDPPDDDDPPDDPPPP TAK NTTTNTNNNN NTTTNTNTTN DDDDDPDDDPPPPPDPPP TAK NNNTNTTTNT NNTTTTTNNT DDDPPDPDDDDDPPPPPP TAK NNNTNNNTNT NN...
result:
ok ac (486 test cases)
Extra Test:
score: 0
Extra Test Passed