QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#94585 | #2265. Short Coding | whatever# | AC ✓ | 3ms | 3508kb | C++23 | 2.4kb | 2023-04-06 18:45:54 | 2023-04-06 18:45:58 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 12;
int n, m, ans, use[N][N];
string str[N];
struct oper {
int type;
int l;
}a[N];
constexpr int D[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
int sx, sy, tx, ty;
bool check() {
int x = sx, y = sy, dir = 3, pc = 1;
static int vis[N][N][4][N], ts;
ts ++;
while(vis[x][y][dir][pc] != ts) {
vis[x][y][dir][pc] = ts;
if(a[pc].type == 1) {
pc = a[pc].l;
}
else if(a[pc].type == 2) {
int dx = x + D[dir][0], dy = y + D[dir][1];
if(use[dx][dy]) {
pc = a[pc].l;
} else {
pc = pc % ans + 1;
}
}
else if(a[pc].type == 3) {
int dx = x + D[dir][0], dy = y + D[dir][1];
if(use[dx][dy]) {
x = dx, y = dy;
}
pc = pc % ans + 1;
}
else if(a[pc].type == 4) {
dir = (dir + 1) % 4;
pc = pc % ans + 1;
}
else if(a[pc].type == 5) {
dir = (dir + 3) % 4;
pc = pc % ans + 1;
}
if(x == tx && y == ty) return true;
}
// cout << "-------\n";
return false;
}
bool dfs(int i) {
if(i == ans + 1) {
return check();
}
for(int u = 1; u <= ans; u ++) {
a[i].type = 1;
a[i].l = u;
if(dfs(i + 1)) return true;
}
for(int u = 1; u <= ans; u ++) {
a[i].type = 2;
a[i].l = u;
if(dfs(i + 1)) return true;
}
for(int u = 3; u <= 5; u ++) {
a[i].type = u;
if(dfs(i + 1)) return true;
}
return false;
}
int main() {
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i ++) {
cin >> str[i];
str[i] = "#" + str[i];
}
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= m; j ++) {
use[i][j] = str[i][j] != '#';
if(str[i][j] == 'S') sx = i, sy = j;
if(str[i][j] == 'G') tx = i, ty = j;
}
}
for(ans = 1; ; ans ++) {
if(ans == 5) {
cout << "5\n";
cout << "LEFT\n";
cout << "IF-OPEN 5\n";
cout << "RIGHT\n";
cout << "GOTO 2\n";
cout << "FORWARD\n";
exit(0);
}
if(dfs(1)) {
cout << ans << '\n';
for(int i = 1; i <= ans; i ++) {
if(a[i].type == 1) {
cout << "GOTO " << a[i].l << '\n';
} else if(a[i].type == 2) {
cout << "IF-OPEN " << a[i].l << '\n';
} else if(a[i].type == 3) {
cout << "FORWARD\n";
} else if(a[i].type == 4) {
cout << "LEFT\n";
} else if(a[i].type == 5) {
cout << "RIGHT\n";
}
}
exit(0);
}
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 3444kb
input:
4 2 S# .# .# G.
output:
1 FORWARD
result:
ok correct answer!
Test #2:
score: 0
Accepted
time: 1ms
memory: 3496kb
input:
3 6 ##S..# #..##. .G..#.
output:
2 FORWARD RIGHT
result:
ok correct answer!
Test #3:
score: 0
Accepted
time: 0ms
memory: 3340kb
input:
3 7 ....S## .#.#... ##.#.G#
output:
2 FORWARD LEFT
result:
ok correct answer!
Test #4:
score: 0
Accepted
time: 2ms
memory: 3424kb
input:
4 8 ...S.#.# ##..#.#. ###...#. #.#.#G.#
output:
3 FORWARD FORWARD LEFT
result:
ok correct answer!
Test #5:
score: 0
Accepted
time: 2ms
memory: 3424kb
input:
3 5 .S#.. ..... ..#G.
output:
3 FORWARD LEFT FORWARD
result:
ok correct answer!
Test #6:
score: 0
Accepted
time: 2ms
memory: 3364kb
input:
10 10 .....S#... ...#...... ....##.... .#.....#.. ....#..... ..#....... ...#...... .......... .#...#.... G.#..#...#
output:
3 LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #7:
score: 0
Accepted
time: 2ms
memory: 3412kb
input:
7 9 .##S.#..# ......... ......... ...#...## ###..#### ........# .....##G#
output:
3 RIGHT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #8:
score: 0
Accepted
time: 2ms
memory: 3480kb
input:
3 9 ..#.#.#S# #...#...# .G....#.#
output:
3 FORWARD RIGHT FORWARD
result:
ok correct answer!
Test #9:
score: 0
Accepted
time: 2ms
memory: 3440kb
input:
4 5 #S... ..#.. .#..# .#.G#
output:
3 RIGHT FORWARD FORWARD
result:
ok correct answer!
Test #10:
score: 0
Accepted
time: 0ms
memory: 3472kb
input:
6 8 ..S..#.# ...#...# ....#..# #.#.##.. .......# .#..#G.#
output:
4 FORWARD LEFT FORWARD RIGHT
result:
ok correct answer!
Test #11:
score: 0
Accepted
time: 2ms
memory: 3456kb
input:
9 10 S......... .#..#....# ........#. .......... .....#.#.. .#...#.... #..#.##.#. ..#..#...# ..#.#....G
output:
4 LEFT FORWARD RIGHT FORWARD
result:
ok correct answer!
Test #12:
score: 0
Accepted
time: 0ms
memory: 3484kb
input:
10 4 #.S. .#.# ##.# ##.. .#.. #... #.## ..#. .#.. G.##
output:
4 FORWARD RIGHT FORWARD LEFT
result:
ok correct answer!
Test #13:
score: 0
Accepted
time: 3ms
memory: 3436kb
input:
9 9 #..#...S# .#...##.# .....##.. ..#..#... ..#..#..# .#..#.#.# .###..... ..#.#.##. G#....#.#
output:
4 RIGHT FORWARD LEFT FORWARD
result:
ok correct answer!
Test #14:
score: 0
Accepted
time: 3ms
memory: 3452kb
input:
7 3 .S# #.. ..# ... ..# .#. .G.
output:
4 FORWARD FORWARD FORWARD LEFT
result:
ok correct answer!
Test #15:
score: 0
Accepted
time: 3ms
memory: 3368kb
input:
6 6 ..#S.. #.#.#. .....# #.#... ..#... ..G##.
output:
4 FORWARD FORWARD LEFT FORWARD
result:
ok correct answer!
Test #16:
score: 0
Accepted
time: 2ms
memory: 3508kb
input:
6 8 ....#S.. ......#. ..##..#. ###.#.## ..#...#. ##.G#..#
output:
4 FORWARD FORWARD RIGHT FORWARD
result:
ok correct answer!
Test #17:
score: 0
Accepted
time: 2ms
memory: 3416kb
input:
10 8 #..S.### .#....## .#...... ....#.#. #...#..# ........ ....#.## ...#.### .#.#...# .#...G.#
output:
4 FORWARD LEFT FORWARD IF-OPEN 3
result:
ok correct answer!
Test #18:
score: 0
Accepted
time: 2ms
memory: 3460kb
input:
7 6 ...##S #..... ....#. ...#.# ....#. #..... .....G
output:
4 FORWARD RIGHT FORWARD IF-OPEN 3
result:
ok correct answer!
Test #19:
score: 0
Accepted
time: 2ms
memory: 3428kb
input:
4 8 .##..S.. #.###..# .#...#.. ...#G...
output:
4 RIGHT FORWARD FORWARD FORWARD
result:
ok correct answer!
Test #20:
score: 0
Accepted
time: 3ms
memory: 3364kb
input:
3 10 ##.......S ####.####. ...#..G##.
output:
4 RIGHT FORWARD LEFT IF-OPEN 2
result:
ok correct answer!
Test #21:
score: 0
Accepted
time: 2ms
memory: 3424kb
input:
2 7 .#S.### .#.G..#
output:
2 FORWARD LEFT
result:
ok correct answer!
Test #22:
score: 0
Accepted
time: 2ms
memory: 3364kb
input:
2 7 .S#.#.# G#.##.#
output:
2 FORWARD LEFT
result:
ok correct answer!
Test #23:
score: 0
Accepted
time: 2ms
memory: 3396kb
input:
2 4 ...S ##G#
output:
2 FORWARD LEFT
result:
ok correct answer!
Test #24:
score: 0
Accepted
time: 2ms
memory: 3400kb
input:
2 7 S...... .G.....
output:
2 FORWARD LEFT
result:
ok correct answer!
Test #25:
score: 0
Accepted
time: 1ms
memory: 3436kb
input:
2 3 #S# ..G
output:
2 FORWARD LEFT
result:
ok correct answer!
Test #26:
score: 0
Accepted
time: 2ms
memory: 3492kb
input:
2 4 #.S# #G##
output:
2 FORWARD LEFT
result:
ok correct answer!
Test #27:
score: 0
Accepted
time: 2ms
memory: 3440kb
input:
3 6 .#S### #....# #G.#..
output:
2 FORWARD LEFT
result:
ok correct answer!
Test #28:
score: 0
Accepted
time: 2ms
memory: 3400kb
input:
2 4 S.#. #G##
output:
2 FORWARD RIGHT
result:
ok correct answer!
Test #29:
score: 0
Accepted
time: 2ms
memory: 3444kb
input:
2 5 ##..S ...G.
output:
2 FORWARD LEFT
result:
ok correct answer!
Test #30:
score: 0
Accepted
time: 2ms
memory: 3444kb
input:
2 9 #S###.... G..##..##
output:
2 FORWARD RIGHT
result:
ok correct answer!
Test #31:
score: 0
Accepted
time: 0ms
memory: 3376kb
input:
6 10 ....S...## .#...#.... .......... .#.#...#.. ...#.##... .G#...#...
output:
3 LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #32:
score: 0
Accepted
time: 2ms
memory: 3432kb
input:
3 3 S.. ##. ##G
output:
3 IF-OPEN 3 RIGHT FORWARD
result:
ok correct answer!
Test #33:
score: 0
Accepted
time: 2ms
memory: 3408kb
input:
5 5 ..S.. .##.. .#### ..#.. G.#.#
output:
3 IF-OPEN 3 LEFT FORWARD
result:
ok correct answer!
Test #34:
score: 0
Accepted
time: 2ms
memory: 3400kb
input:
4 6 S#.... ....#. #..... #...G.
output:
3 IF-OPEN 3 LEFT FORWARD
result:
ok correct answer!
Test #35:
score: 0
Accepted
time: 2ms
memory: 3360kb
input:
3 6 .#..S# ..#..# .##G.#
output:
3 IF-OPEN 3 RIGHT FORWARD
result:
ok correct answer!
Test #36:
score: 0
Accepted
time: 2ms
memory: 3368kb
input:
4 3 ..S .#. ..# G..
output:
3 IF-OPEN 3 LEFT FORWARD
result:
ok correct answer!
Test #37:
score: 0
Accepted
time: 2ms
memory: 3404kb
input:
5 3 S.. ##. ... ... .G.
output:
3 IF-OPEN 3 RIGHT FORWARD
result:
ok correct answer!
Test #38:
score: 0
Accepted
time: 0ms
memory: 3452kb
input:
8 2 .S .. .. .. .. .. .# G.
output:
3 IF-OPEN 3 LEFT FORWARD
result:
ok correct answer!
Test #39:
score: 0
Accepted
time: 2ms
memory: 3380kb
input:
3 7 ...#..S #.#.... ....G..
output:
3 IF-OPEN 3 LEFT FORWARD
result:
ok correct answer!
Test #40:
score: 0
Accepted
time: 2ms
memory: 3468kb
input:
3 9 #......S. ...#.#... .G.#.#...
output:
3 IF-OPEN 3 LEFT FORWARD
result:
ok correct answer!
Test #41:
score: 0
Accepted
time: 0ms
memory: 3360kb
input:
2 9 .##..S... ##..##G.#
output:
4 IF-OPEN 3 LEFT FORWARD RIGHT
result:
ok correct answer!
Test #42:
score: 0
Accepted
time: 3ms
memory: 3368kb
input:
4 6 S..#.# ..#... ...... ..#G#.
output:
4 FORWARD FORWARD LEFT FORWARD
result:
ok correct answer!
Test #43:
score: 0
Accepted
time: 2ms
memory: 3404kb
input:
4 9 #..##S..# ##..#...# #.......# ####.#G##
output:
4 IF-OPEN 3 LEFT LEFT FORWARD
result:
ok correct answer!
Test #44:
score: 0
Accepted
time: 0ms
memory: 3496kb
input:
3 8 ....S... .....#.. ..#G#...
output:
4 FORWARD RIGHT FORWARD LEFT
result:
ok correct answer!
Test #45:
score: 0
Accepted
time: 2ms
memory: 3424kb
input:
4 9 .#.S##### ..#.###.. .#....#.# .####.G.#
output:
4 FORWARD LEFT FORWARD RIGHT
result:
ok correct answer!
Test #46:
score: 0
Accepted
time: 0ms
memory: 3444kb
input:
4 3 ..S ... ... #G#
output:
4 IF-OPEN 3 LEFT LEFT FORWARD
result:
ok correct answer!
Test #47:
score: 0
Accepted
time: 2ms
memory: 3440kb
input:
2 7 #...S.. ..#G##.
output:
4 IF-OPEN 3 RIGHT FORWARD LEFT
result:
ok correct answer!
Test #48:
score: 0
Accepted
time: 3ms
memory: 3484kb
input:
5 6 ...S.. ...#.. ...#.. ...#.# #G..#.
output:
4 RIGHT FORWARD IF-OPEN 1 RIGHT
result:
ok correct answer!
Test #49:
score: 0
Accepted
time: 2ms
memory: 3324kb
input:
4 8 .#...#.S ..###... ........ ....G..#
output:
4 FORWARD IF-OPEN 4 LEFT LEFT
result:
ok correct answer!
Test #50:
score: 0
Accepted
time: 2ms
memory: 3400kb
input:
3 4 #S.. ##.. .#G#
output:
4 IF-OPEN 3 LEFT FORWARD RIGHT
result:
ok correct answer!
Test #51:
score: 0
Accepted
time: 2ms
memory: 3476kb
input:
4 6 #.S.#. #..#.. ..#... ..G##.
output:
3 IF-OPEN 3 LEFT FORWARD
result:
ok correct answer!
Test #52:
score: 0
Accepted
time: 2ms
memory: 3356kb
input:
4 5 #..S. #.#.. ..#.. G#..#
output:
4 RIGHT FORWARD LEFT FORWARD
result:
ok correct answer!
Test #53:
score: 0
Accepted
time: 3ms
memory: 3476kb
input:
5 5 #.#S# #...# ##.## ##..# .##.G
output:
5 LEFT IF-OPEN 5 RIGHT GOTO 2 FORWARD
result:
ok correct answer!
Test #54:
score: 0
Accepted
time: 0ms
memory: 3480kb
input:
5 6 ##S..# ...#.. ...#.# ...#.. ..#G.#
output:
3 RIGHT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #55:
score: 0
Accepted
time: 0ms
memory: 3412kb
input:
6 4 ...S ..#. .... .... .... G...
output:
3 IF-OPEN 3 LEFT FORWARD
result:
ok correct answer!
Test #56:
score: 0
Accepted
time: 2ms
memory: 3480kb
input:
5 6 ....S. ...... ...... ....#. ....G.
output:
3 IF-OPEN 3 LEFT FORWARD
result:
ok correct answer!
Test #57:
score: 0
Accepted
time: 2ms
memory: 3444kb
input:
4 5 S###. .#### .#### ..G##
output:
3 IF-OPEN 3 LEFT FORWARD
result:
ok correct answer!
Test #58:
score: 0
Accepted
time: 2ms
memory: 3364kb
input:
6 5 ...S. ...#. .#... ..##. .##.. .G#..
output:
3 IF-OPEN 3 LEFT FORWARD
result:
ok correct answer!
Test #59:
score: 0
Accepted
time: 2ms
memory: 3440kb
input:
5 4 ..S. #... .#.. ..#. G...
output:
3 IF-OPEN 3 RIGHT FORWARD
result:
ok correct answer!
Test #60:
score: 0
Accepted
time: 1ms
memory: 3392kb
input:
5 5 ..#S# .#..# ....# ..#.# ###G.
output:
1 FORWARD
result:
ok correct answer!
Test #61:
score: 0
Accepted
time: 2ms
memory: 3360kb
input:
10 10 #...S..#.. ..#...#... ..#.#.#... ...#....## ##.....#.# .....#.##. .....#...# ...###.... ..#.#..... .#.#....#G
output:
4 FORWARD LEFT FORWARD RIGHT
result:
ok correct answer!
Test #62:
score: 0
Accepted
time: 1ms
memory: 3420kb
input:
10 10 .#..S#...# .....#.... ...#...#.# .##..#.... #....###.# ........## ...#..#... ##.#....## ...#....#. #.#G...#.#
output:
3 IF-OPEN 3 RIGHT FORWARD
result:
ok correct answer!
Test #63:
score: 0
Accepted
time: 2ms
memory: 3420kb
input:
10 10 .......S.. .......... .......... .......... ........#. .......... .......... .......... .......... ........G.
output:
3 IF-OPEN 3 LEFT FORWARD
result:
ok correct answer!
Test #64:
score: 0
Accepted
time: 2ms
memory: 3360kb
input:
10 10 ....#S.#.# .#....#... .....#..#. ....#..#.. .#.....#.# .......### #....#.#.. .......... .#.#.##... #....G.#..
output:
5 LEFT IF-OPEN 5 RIGHT GOTO 2 FORWARD
result:
ok correct answer!
Test #65:
score: 0
Accepted
time: 0ms
memory: 3360kb
input:
10 10 #...#..#.S .##..#.... ..###.#... ##.##.##.. #.#..##..# .##.##...# ..#.##..#. ...#...##. .#.#....## #.##..G##.
output:
5 LEFT IF-OPEN 5 RIGHT GOTO 2 FORWARD
result:
ok correct answer!
Test #66:
score: 0
Accepted
time: 1ms
memory: 3380kb
input:
10 10 .........S .......... ..#....... .......... .....#.... .#........ .......... .......... .......#.. .G.......#
output:
3 IF-OPEN 3 LEFT FORWARD
result:
ok correct answer!
Test #67:
score: 0
Accepted
time: 2ms
memory: 3416kb
input:
10 10 .....#...S ...#....#. ......#.## ..#......# #.#.##.#.. ..#.##.#.. .......#.. #...#..... #.#....#.. .#...#..#G
output:
5 LEFT IF-OPEN 5 RIGHT GOTO 2 FORWARD
result:
ok correct answer!
Test #68:
score: 0
Accepted
time: 2ms
memory: 3380kb
input:
10 10 ......S... .......... .......... .......... .......... ...#....#. .......... #.....#... .......... .....#G...
output:
3 IF-OPEN 3 RIGHT FORWARD
result:
ok correct answer!
Test #69:
score: 0
Accepted
time: 2ms
memory: 3364kb
input:
10 10 ......##S. ........#. ....###... ....#.#..# .##....#.# .....#.... .....#.... ..##...... ....#.#.#. .#....G...
output:
5 LEFT IF-OPEN 5 RIGHT GOTO 2 FORWARD
result:
ok correct answer!
Test #70:
score: 0
Accepted
time: 2ms
memory: 3376kb
input:
10 10 #.S#..#... .......#.# #..##..#.# #...#...#. #.###..##. .....#.#.# #.#..#...# ..##.#.#.# ####..#... ##..G#.#.#
output:
5 LEFT IF-OPEN 5 RIGHT GOTO 2 FORWARD
result:
ok correct answer!
Test #71:
score: 0
Accepted
time: 3ms
memory: 3356kb
input:
10 10 S#...#.... .#.#.#.##. .#.#.#.#.. .#.#.#.#.# .#.#.#.#.. .#.#.#.##. .#.#.#.#.. .#.#.#.#.# .#.#.#.#.. ...#...##G
output:
5 LEFT IF-OPEN 5 RIGHT GOTO 2 FORWARD
result:
ok correct answer!
Test #72:
score: 0
Accepted
time: 2ms
memory: 3484kb
input:
10 10 S#..#...#. .#.....#.. .#.....#.. .........# ##.#.#.#.. .#.#.#.#.. .#.#.#.#.# .#.#.#.#.. .#.#.#.#.. ...#...#.G
output:
3 IF-OPEN 3 LEFT FORWARD
result:
ok correct answer!
Test #73:
score: 0
Accepted
time: 1ms
memory: 3460kb
input:
10 10 S.#.#.#.#. #..#.#.#.# .#..#.#.#. #.#..#.#.# .#.#..#.#. #.#.#..#.# .#.#.#..#. #.#.#.#..# .#.#.#.#.. #.#.#.#.#G
output:
4 FORWARD LEFT FORWARD RIGHT
result:
ok correct answer!
Test #74:
score: 0
Accepted
time: 2ms
memory: 3380kb
input:
10 10 S......... .########. .#......#. .#......#. .#......#. .#......#. .#......#. .#......#. .########. .........G
output:
3 IF-OPEN 3 LEFT FORWARD
result:
ok correct answer!
Test #75:
score: 0
Accepted
time: 0ms
memory: 3436kb
input:
10 10 S.#....... #.###.#### #......#.# ##.###.... .#.#...#.# ...#####.# .#.#.#...# .#.#...### ##.#####.# .........G
output:
4 FORWARD LEFT FORWARD RIGHT
result:
ok correct answer!
Test #76:
score: 0
Accepted
time: 2ms
memory: 3428kb
input:
10 10 S.#....... #.###.#### #......#.# ##.###.... .#.#...#.# ...#####.# .#.#.#...# .#.#...### ##.#####.# G.........
output:
5 LEFT IF-OPEN 5 RIGHT GOTO 2 FORWARD
result:
ok correct answer!