QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#259801 | #2265. Short Coding | PixelCat# | AC ✓ | 2ms | 3868kb | C++20 | 4.1kb | 2023-11-21 14:06:44 | 2023-11-21 14:06:44 |
Judging History
answer
#include <bits/stdc++.h>
#define For(i, a, b) for(int i = a; i <= b; i++)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sz(x) ((int)x.size())
#define eb emplace_back
// #define int LL
using namespace std;
using LL = long long;
using pii = pair<int, int>;
/*
0 1 forward
0 2 right
0 3 left
1 k goto <k>
2 k if-open <k>
*/
inline bool inside(int x, int y, int n, int m) {
return x >= 0 && y >= 0 && x < n && y < m;
}
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
char grid[10][10];
int vis[10][10][4][5];
using TP = tuple<int, int, int, int>;
TP parse(TP pos, int op, int n, int m, int tot_op) {
int x, y, dir, pc;
tie(x, y, dir, pc) = pos;
int o1 = op / 10;
int o2 = op % 10;
if(o1 == 0 && o2 == 1) {
x += dx[dir]; y += dy[dir];
pc = (pc + 1) % tot_op;
if(!inside(x, y, n, m) || grid[x][y] == '#') {
x -= dx[dir]; y -= dy[dir];
}
} else if(o1 == 0 && o2 == 2) {
dir = (dir + 1) % 4;
pc = (pc + 1) % tot_op;
} else if(o1 == 0 && o2 == 3) {
dir = (dir + 3) % 4;
pc = (pc + 1) % tot_op;
} else if(o1 == 1) {
pc = o2;
} else if(o1 == 2) {
int x2 = x + dx[dir], y2 = y + dy[dir];
if(inside(x2, y2, n, m) && grid[x2][y2] != '#') {
pc = o2;
} else {
pc = (pc + 1) % tot_op;
}
}
return make_tuple(x, y, dir, pc);
}
vector<int> get_ops(int tot_op) {
vector<int> op;
For(i, 1, 3) op.eb(i);
For(i, 10, 10 + tot_op - 1) op.eb(i);
For(i, 20, 20 + tot_op - 1) op.eb(i);
return op;
}
bool check_program(const pii dim, const pii start, const vector<int> &prog) {
memset(vis, 0, sizeof(vis));
queue<TP> que;
auto walk = [&](TP pos) {
int x, y, dir, pc;
tie(x, y, dir, pc) = pos;
// cerr << x << " " << y << " " << dir << " " << pc << "\n";
if(grid[x][y] == 'G') {
return 2;
}
if(!vis[x][y][dir][pc]) {
vis[x][y][dir][pc] = 1;
return 1;
}
return 0;
};
TP pos(start.F, start.S, 1, 0);
int res;
while((res = walk(pos)) == 1) {
pos = parse(pos, prog[get<3>(pos)], dim.F, dim.S, sz(prog));
if(get<0>(pos) < 0) return false;
}
if(res == 2) return true;
return false;
}
void dfs(const pii dim, const pii start, int tot_op, const vector<int> &ops, vector<int> &prog) {
if(sz(prog) == tot_op) {
if(check_program(dim, start, prog)) {
cout << tot_op << "\n";
for(auto &op:prog) {
int o1 = op / 10, o2 = op % 10;
if(o1 == 0 && o2 == 1) {
cout << "FORWARD\n";
} else if(o1 == 0 && o2 == 2) {
cout << "RIGHT\n";
} else if(o1 == 0 && o2 == 3) {
cout << "LEFT\n";
} else if(o1 == 1) {
cout << "GOTO " << (o2 + 1) << "\n";
} else if(o1 == 2) {
cout << "IF-OPEN " << (o2 + 1) << "\n";
}
}
// for(auto &i:prog) cout << i << " ";
// cout << "\n";
exit(0);
}
return;
}
for(auto &i:ops) {
if(i / 10 != 0 && i % 10 == sz(prog)) continue;
prog.eb(i);
dfs(dim, start, tot_op, ops, prog);
prog.pop_back();
}
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m; cin >> n >> m;
pii start(-1, -1);
For(i, 0, n - 1) For(j, 0, m - 1) {
cin >> grid[i][j];
if(grid[i][j] == 'S') start = pii(i, j);
}
// cout << start.F << " " << start.S << "\n";
// return 0;
For(i, 1, 5) {
vector<int> prog;
dfs(pii(n, m), start, i, get_ops(i), prog);
}
cout << "6\n";
cout << "RIGHT\n";
cout << "IF-OPEN 6\n";
cout << "LEFT\n";
cout << "LEFT\n";
cout << "GOTO 1\n";
cout << "FORWARD\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3800kb
input:
4 2 S# .# .# G.
output:
1 FORWARD
result:
ok correct answer!
Test #2:
score: 0
Accepted
time: 0ms
memory: 3868kb
input:
3 6 ##S..# #..##. .G..#.
output:
2 FORWARD RIGHT
result:
ok correct answer!
Test #3:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
3 7 ....S## .#.#... ##.#.G#
output:
2 FORWARD LEFT
result:
ok correct answer!
Test #4:
score: 0
Accepted
time: 0ms
memory: 3800kb
input:
4 8 ...S.#.# ##..#.#. ###...#. #.#.#G.#
output:
3 FORWARD FORWARD LEFT
result:
ok correct answer!
Test #5:
score: 0
Accepted
time: 0ms
memory: 3512kb
input:
3 5 .S#.. ..... ..#G.
output:
3 FORWARD LEFT FORWARD
result:
ok correct answer!
Test #6:
score: 0
Accepted
time: 1ms
memory: 3640kb
input:
10 10 .....S#... ...#...... ....##.... .#.....#.. ....#..... ..#....... ...#...... .......... .#...#.... G.#..#...#
output:
3 LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #7:
score: 0
Accepted
time: 0ms
memory: 3564kb
input:
7 9 .##S.#..# ......... ......... ...#...## ###..#### ........# .....##G#
output:
3 RIGHT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #8:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
3 9 ..#.#.#S# #...#...# .G....#.#
output:
3 FORWARD RIGHT FORWARD
result:
ok correct answer!
Test #9:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
4 5 #S... ..#.. .#..# .#.G#
output:
3 RIGHT FORWARD FORWARD
result:
ok correct answer!
Test #10:
score: 0
Accepted
time: 1ms
memory: 3632kb
input:
6 8 ..S..#.# ...#...# ....#..# #.#.##.. .......# .#..#G.#
output:
4 FORWARD LEFT FORWARD RIGHT
result:
ok correct answer!
Test #11:
score: 0
Accepted
time: 1ms
memory: 3572kb
input:
9 10 S......... .#..#....# ........#. .......... .....#.#.. .#...#.... #..#.##.#. ..#..#...# ..#.#....G
output:
4 LEFT FORWARD RIGHT FORWARD
result:
ok correct answer!
Test #12:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
10 4 #.S. .#.# ##.# ##.. .#.. #... #.## ..#. .#.. G.##
output:
4 FORWARD RIGHT FORWARD LEFT
result:
ok correct answer!
Test #13:
score: 0
Accepted
time: 1ms
memory: 3864kb
input:
9 9 #..#...S# .#...##.# .....##.. ..#..#... ..#..#..# .#..#.#.# .###..... ..#.#.##. G#....#.#
output:
4 RIGHT FORWARD LEFT FORWARD
result:
ok correct answer!
Test #14:
score: 0
Accepted
time: 0ms
memory: 3552kb
input:
7 3 .S# #.. ..# ... ..# .#. .G.
output:
4 FORWARD FORWARD FORWARD LEFT
result:
ok correct answer!
Test #15:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
6 6 ..#S.. #.#.#. .....# #.#... ..#... ..G##.
output:
4 FORWARD FORWARD LEFT FORWARD
result:
ok correct answer!
Test #16:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
6 8 ....#S.. ......#. ..##..#. ###.#.## ..#...#. ##.G#..#
output:
4 FORWARD FORWARD RIGHT FORWARD
result:
ok correct answer!
Test #17:
score: 0
Accepted
time: 0ms
memory: 3636kb
input:
10 8 #..S.### .#....## .#...... ....#.#. #...#..# ........ ....#.## ...#.### .#.#...# .#...G.#
output:
4 FORWARD LEFT FORWARD IF-OPEN 3
result:
ok correct answer!
Test #18:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
7 6 ...##S #..... ....#. ...#.# ....#. #..... .....G
output:
4 FORWARD RIGHT FORWARD IF-OPEN 3
result:
ok correct answer!
Test #19:
score: 0
Accepted
time: 1ms
memory: 3612kb
input:
4 8 .##..S.. #.###..# .#...#.. ...#G...
output:
4 RIGHT FORWARD FORWARD FORWARD
result:
ok correct answer!
Test #20:
score: 0
Accepted
time: 1ms
memory: 3636kb
input:
3 10 ##.......S ####.####. ...#..G##.
output:
4 RIGHT FORWARD LEFT IF-OPEN 2
result:
ok correct answer!
Test #21:
score: 0
Accepted
time: 0ms
memory: 3672kb
input:
2 7 .#S.### .#.G..#
output:
2 FORWARD RIGHT
result:
ok correct answer!
Test #22:
score: 0
Accepted
time: 0ms
memory: 3672kb
input:
2 7 .S#.#.# G#.##.#
output:
2 FORWARD LEFT
result:
ok correct answer!
Test #23:
score: 0
Accepted
time: 0ms
memory: 3664kb
input:
2 4 ...S ##G#
output:
2 FORWARD LEFT
result:
ok correct answer!
Test #24:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
2 7 S...... .G.....
output:
2 FORWARD RIGHT
result:
ok correct answer!
Test #25:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
2 3 #S# ..G
output:
2 FORWARD LEFT
result:
ok correct answer!
Test #26:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
2 4 #.S# #G##
output:
2 FORWARD LEFT
result:
ok correct answer!
Test #27:
score: 0
Accepted
time: 0ms
memory: 3516kb
input:
3 6 .#S### #....# #G.#..
output:
2 FORWARD RIGHT
result:
ok correct answer!
Test #28:
score: 0
Accepted
time: 0ms
memory: 3668kb
input:
2 4 S.#. #G##
output:
2 FORWARD RIGHT
result:
ok correct answer!
Test #29:
score: 0
Accepted
time: 0ms
memory: 3800kb
input:
2 5 ##..S ...G.
output:
2 FORWARD RIGHT
result:
ok correct answer!
Test #30:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
2 9 #S###.... G..##..##
output:
2 FORWARD RIGHT
result:
ok correct answer!
Test #31:
score: 0
Accepted
time: 0ms
memory: 3824kb
input:
6 10 ....S...## .#...#.... .......... .#.#...#.. ...#.##... .G#...#...
output:
3 LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #32:
score: 0
Accepted
time: 0ms
memory: 3800kb
input:
3 3 S.. ##. ##G
output:
3 FORWARD FORWARD RIGHT
result:
ok correct answer!
Test #33:
score: 0
Accepted
time: 0ms
memory: 3800kb
input:
5 5 ..S.. .##.. .#### ..#.. G.#.#
output:
3 FORWARD IF-OPEN 1 LEFT
result:
ok correct answer!
Test #34:
score: 0
Accepted
time: 0ms
memory: 3524kb
input:
4 6 S#.... ....#. #..... #...G.
output:
3 FORWARD FORWARD LEFT
result:
ok correct answer!
Test #35:
score: 0
Accepted
time: 0ms
memory: 3800kb
input:
3 6 .#..S# ..#..# .##G.#
output:
3 FORWARD FORWARD RIGHT
result:
ok correct answer!
Test #36:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
4 3 ..S .#. ..# G..
output:
3 FORWARD FORWARD LEFT
result:
ok correct answer!
Test #37:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
5 3 S.. ##. ... ... .G.
output:
3 FORWARD FORWARD RIGHT
result:
ok correct answer!
Test #38:
score: 0
Accepted
time: 0ms
memory: 3828kb
input:
8 2 .S .. .. .. .. .. .# G.
output:
3 FORWARD IF-OPEN 1 LEFT
result:
ok correct answer!
Test #39:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
3 7 ...#..S #.#.... ....G..
output:
3 FORWARD FORWARD RIGHT
result:
ok correct answer!
Test #40:
score: 0
Accepted
time: 0ms
memory: 3828kb
input:
3 9 #......S. ...#.#... .G.#.#...
output:
3 FORWARD IF-OPEN 1 LEFT
result:
ok correct answer!
Test #41:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
2 9 .##..S... ##..##G.#
output:
4 FORWARD FORWARD FORWARD RIGHT
result:
ok correct answer!
Test #42:
score: 0
Accepted
time: 0ms
memory: 3636kb
input:
4 6 S..#.# ..#... ...... ..#G#.
output:
4 FORWARD FORWARD LEFT FORWARD
result:
ok correct answer!
Test #43:
score: 0
Accepted
time: 1ms
memory: 3572kb
input:
4 9 #..##S..# ##..#...# #.......# ####.#G##
output:
4 FORWARD IF-OPEN 4 LEFT LEFT
result:
ok correct answer!
Test #44:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
3 8 ....S... .....#.. ..#G#...
output:
4 FORWARD RIGHT FORWARD LEFT
result:
ok correct answer!
Test #45:
score: 0
Accepted
time: 1ms
memory: 3608kb
input:
4 9 .#.S##### ..#.###.. .#....#.# .####.G.#
output:
4 FORWARD LEFT FORWARD RIGHT
result:
ok correct answer!
Test #46:
score: 0
Accepted
time: 0ms
memory: 3856kb
input:
4 3 ..S ... ... #G#
output:
4 FORWARD IF-OPEN 4 RIGHT RIGHT
result:
ok correct answer!
Test #47:
score: 0
Accepted
time: 0ms
memory: 3516kb
input:
2 7 #...S.. ..#G##.
output:
4 FORWARD FORWARD FORWARD LEFT
result:
ok correct answer!
Test #48:
score: 0
Accepted
time: 1ms
memory: 3572kb
input:
5 6 ...S.. ...#.. ...#.. ...#.# #G..#.
output:
4 RIGHT FORWARD IF-OPEN 1 RIGHT
result:
ok correct answer!
Test #49:
score: 0
Accepted
time: 1ms
memory: 3624kb
input:
4 8 .#...#.S ..###... ........ ....G..#
output:
4 FORWARD RIGHT FORWARD LEFT
result:
ok correct answer!
Test #50:
score: 0
Accepted
time: 1ms
memory: 3640kb
input:
3 4 #S.. ##.. .#G#
output:
4 LEFT FORWARD RIGHT IF-OPEN 2
result:
ok correct answer!
Test #51:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
4 6 #.S.#. #..#.. ..#... ..G##.
output:
3 FORWARD IF-OPEN 1 LEFT
result:
ok correct answer!
Test #52:
score: 0
Accepted
time: 1ms
memory: 3668kb
input:
4 5 #..S. #.#.. ..#.. G#..#
output:
4 RIGHT FORWARD LEFT FORWARD
result:
ok correct answer!
Test #53:
score: 0
Accepted
time: 2ms
memory: 3832kb
input:
5 5 #.#S# #...# ##.## ##..# .##.G
output:
5 FORWARD RIGHT IF-OPEN 1 RIGHT RIGHT
result:
ok correct answer!
Test #54:
score: 0
Accepted
time: 0ms
memory: 3636kb
input:
5 6 ##S..# ...#.. ...#.# ...#.. ..#G.#
output:
3 RIGHT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #55:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
6 4 ...S ..#. .... .... .... G...
output:
3 FORWARD IF-OPEN 1 RIGHT
result:
ok correct answer!
Test #56:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
5 6 ....S. ...... ...... ....#. ....G.
output:
3 FORWARD IF-OPEN 1 RIGHT
result:
ok correct answer!
Test #57:
score: 0
Accepted
time: 0ms
memory: 3796kb
input:
4 5 S###. .#### .#### ..G##
output:
3 FORWARD IF-OPEN 1 LEFT
result:
ok correct answer!
Test #58:
score: 0
Accepted
time: 0ms
memory: 3576kb
input:
6 5 ...S. ...#. .#... ..##. .##.. .G#..
output:
3 FORWARD IF-OPEN 1 LEFT
result:
ok correct answer!
Test #59:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
5 4 ..S. #... .#.. ..#. G...
output:
3 FORWARD IF-OPEN 1 RIGHT
result:
ok correct answer!
Test #60:
score: 0
Accepted
time: 0ms
memory: 3796kb
input:
5 5 ..#S# .#..# ....# ..#.# ###G.
output:
1 FORWARD
result:
ok correct answer!
Test #61:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
10 10 #...S..#.. ..#...#... ..#.#.#... ...#....## ##.....#.# .....#.##. .....#...# ...###.... ..#.#..... .#.#....#G
output:
4 FORWARD LEFT FORWARD RIGHT
result:
ok correct answer!
Test #62:
score: 0
Accepted
time: 0ms
memory: 3860kb
input:
10 10 .#..S#...# .....#.... ...#...#.# .##..#.... #....###.# ........## ...#..#... ##.#....## ...#....#. #.#G...#.#
output:
3 FORWARD IF-OPEN 1 RIGHT
result:
ok correct answer!
Test #63:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
10 10 .......S.. .......... .......... .......... ........#. .......... .......... .......... .......... ........G.
output:
3 FORWARD IF-OPEN 1 RIGHT
result:
ok correct answer!
Test #64:
score: 0
Accepted
time: 0ms
memory: 3604kb
input:
10 10 ....#S.#.# .#....#... .....#..#. ....#..#.. .#.....#.# .......### #....#.#.. .......... .#.#.##... #....G.#..
output:
5 FORWARD RIGHT IF-OPEN 1 LEFT GOTO 3
result:
ok correct answer!
Test #65:
score: 0
Accepted
time: 2ms
memory: 3628kb
input:
10 10 #...#..#.S .##..#.... ..###.#... ##.##.##.. #.#..##..# .##.##...# ..#.##..#. ...#...##. .#.#....## #.##..G##.
output:
5 FORWARD LEFT IF-OPEN 1 RIGHT GOTO 3
result:
ok correct answer!
Test #66:
score: 0
Accepted
time: 0ms
memory: 3460kb
input:
10 10 .........S .......... ..#....... .......... .....#.... .#........ .......... .......... .......#.. .G.......#
output:
3 FORWARD IF-OPEN 1 LEFT
result:
ok correct answer!
Test #67:
score: 0
Accepted
time: 2ms
memory: 3568kb
input:
10 10 .....#...S ...#....#. ......#.## ..#......# #.#.##.#.. ..#.##.#.. .......#.. #...#..... #.#....#.. .#...#..#G
output:
5 FORWARD RIGHT IF-OPEN 1 LEFT GOTO 3
result:
ok correct answer!
Test #68:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
10 10 ......S... .......... .......... .......... .......... ...#....#. .......... #.....#... .......... .....#G...
output:
3 FORWARD IF-OPEN 1 RIGHT
result:
ok correct answer!
Test #69:
score: 0
Accepted
time: 2ms
memory: 3636kb
input:
10 10 ......##S. ........#. ....###... ....#.#..# .##....#.# .....#.... .....#.... ..##...... ....#.#.#. .#....G...
output:
5 FORWARD RIGHT IF-OPEN 1 LEFT GOTO 3
result:
ok correct answer!
Test #70:
score: 0
Accepted
time: 2ms
memory: 3556kb
input:
10 10 #.S#..#... .......#.# #..##..#.# #...#...#. #.###..##. .....#.#.# #.#..#...# ..##.#.#.# ####..#... ##..G#.#.#
output:
5 FORWARD LEFT IF-OPEN 1 RIGHT GOTO 3
result:
ok correct answer!
Test #71:
score: 0
Accepted
time: 2ms
memory: 3568kb
input:
10 10 S#...#.... .#.#.#.##. .#.#.#.#.. .#.#.#.#.# .#.#.#.#.. .#.#.#.##. .#.#.#.#.. .#.#.#.#.# .#.#.#.#.. ...#...##G
output:
5 FORWARD RIGHT IF-OPEN 1 RIGHT RIGHT
result:
ok correct answer!
Test #72:
score: 0
Accepted
time: 0ms
memory: 3828kb
input:
10 10 S#..#...#. .#.....#.. .#.....#.. .........# ##.#.#.#.. .#.#.#.#.. .#.#.#.#.# .#.#.#.#.. .#.#.#.#.. ...#...#.G
output:
3 FORWARD IF-OPEN 1 LEFT
result:
ok correct answer!
Test #73:
score: 0
Accepted
time: 0ms
memory: 3864kb
input:
10 10 S.#.#.#.#. #..#.#.#.# .#..#.#.#. #.#..#.#.# .#.#..#.#. #.#.#..#.# .#.#.#..#. #.#.#.#..# .#.#.#.#.. #.#.#.#.#G
output:
4 FORWARD LEFT FORWARD RIGHT
result:
ok correct answer!
Test #74:
score: 0
Accepted
time: 0ms
memory: 3668kb
input:
10 10 S......... .########. .#......#. .#......#. .#......#. .#......#. .#......#. .#......#. .########. .........G
output:
3 FORWARD IF-OPEN 1 RIGHT
result:
ok correct answer!
Test #75:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
10 10 S.#....... #.###.#### #......#.# ##.###.... .#.#...#.# ...#####.# .#.#.#...# .#.#...### ##.#####.# .........G
output:
4 FORWARD LEFT FORWARD RIGHT
result:
ok correct answer!
Test #76:
score: 0
Accepted
time: 2ms
memory: 3568kb
input:
10 10 S.#....... #.###.#### #......#.# ##.###.... .#.#...#.# ...#####.# .#.#.#...# .#.#...### ##.#####.# G.........
output:
5 FORWARD RIGHT IF-OPEN 1 RIGHT RIGHT
result:
ok correct answer!