QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#18566 | #2265. Short Coding | linmuhan# | AC ✓ | 17ms | 3836kb | C++14 | 3.5kb | 2022-01-20 15:50:14 | 2022-05-06 01:45:00 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 12;
const int dx[5][2] = {{0, 0}, {-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int n, m, Ans = 5;
int xs, ys, xt, yt;
string dic[N * 2] = {"LEFT", "RIGHT", "IF-OPEN 1", "IF-OPEN 2", "IF-OPEN 3"
, "IF-OPEN 4", "GOTO 1", "GOTO 2", "GOTO 3", "GOTO 4", "FORWARD"};
string opt[N * 2], ans[N * 2];
char s[N][N];
bool f[N][N][N][N], Flag, BUHEFA;
bool valid (int x, int y) {return (x >= 1 && x <= n && y >= 1 && y <= m && s[x][y] != '#');}
void dfss (int x, int y, int stp, int dir, int lim) {
if (Flag) return ;
if (BUHEFA) return ;
if (stp == lim + 1) stp = 1;
if (x == xt && y == yt) {
Flag = 1;
return ;
}
if (f[x][y][stp][dir]) {
BUHEFA = 1;
return ;
}
f[x][y][stp][dir] = 1;
if (opt[stp] == "LEFT") {
dir --;
if (dir == 0) dir = 4;
dfss (x, y, stp + 1, dir, lim);
}
if (opt[stp] == "RIGHT") {
dir ++;
if (dir == 5) dir = 1;
dfss (x, y, stp + 1, dir, lim);
}
if (opt[stp] == "GOTO 1") {
if (lim >= 1) {
dfss (x, y, 1, dir, lim);
} else {
BUHEFA = 1;
}
}
if (opt[stp] == "GOTO 2") {
if (lim >= 2) {
dfss (x, y, 2, dir, lim);
} else {
BUHEFA = 1;
}
}
if (opt[stp] == "GOTO 3") {
if (lim >= 3) {
dfss (x, y, 3, dir, lim);
} else {
BUHEFA = 1;
}
}
if (opt[stp] == "GOTO 4") {
if (lim >= 4) {
dfss (x, y, 4, dir, lim);
} else {
BUHEFA = 1;
}
}
if (opt[stp] == "IF-OPEN 1") {
int xx = x + dx[dir][0], yy = y + dx[dir][1];
if (lim >= 1) {
if (valid (xx, yy)) {
dfss (x, y, 1, dir, lim);
} else {
dfss (x, y, stp + 1, dir, lim);
}
} else {
BUHEFA = 1;
}
}
if (opt[stp] == "IF-OPEN 2") {
int xx = x + dx[dir][0], yy = y + dx[dir][1];
if (lim >= 2) {
if (valid (xx, yy)) {
dfss (x, y, 2, dir, lim);
} else {
dfss (x, y, stp + 1, dir, lim);
}
} else {
BUHEFA = 1;
}
}
if (opt[stp] == "IF-OPEN 3") {
int xx = x + dx[dir][0], yy = y + dx[dir][1];
if (lim >= 3) {
if (valid (xx, yy)) {
dfss (x, y, 3, dir, lim);
} else {
dfss (x, y, stp + 1, dir, lim);
}
} else {
BUHEFA = 1;
}
}
if (opt[stp] == "IF-OPEN 4") {
int xx = x + dx[dir][0], yy = y + dx[dir][1];
if (lim >= 4) {
if (valid (xx, yy)) {
dfss (x, y, 4, dir, lim);
} else {
dfss (x, y, stp + 1, dir, lim);
}
} else {
BUHEFA = 1;
}
}
if (opt[stp] == "FORWARD") {
int xx = x + dx[dir][0], yy = y + dx[dir][1];
if (valid (xx, yy)) {
dfss (xx, yy, stp + 1, dir, lim);
} else {
dfss (x, y, stp + 1, dir, lim);
}
}
}
bool chk (int len) {
memset (f, 0, sizeof f);
Flag = 0; BUHEFA = 0;
dfss (xs, ys, 1, 3, len);
return Flag && (! BUHEFA);
}
void dfs (int dep) {
if (dep > 5) return ;
if (dep > Ans) return ;
if (chk (dep - 1)) {
if (Ans > dep - 1) {
Ans = dep - 1;
for (int i=1;i<=dep-1;i++) ans[i] = opt[i];
}
}
for (int i=0;i<11;i++) {
opt[dep] = dic[i];
dfs (dep + 1);
}
}
int main () {
cin >> n >> m;
for (int i=1;i<=n;i++) scanf ("%s", (s[i] + 1));
for (int i=1;i<=n;i++) {
for (int j=1;j<=m;j++) {
if (s[i][j] == 'S') xs = i, ys = j;
if (s[i][j] == 'G') xt = i, yt = j;
}
// if (i == 9 || i == 10) {
// for (int j=1;j<=m;j++) cout << s[i][j]; cout << endl;
// }
}
dfs (1);
if (Ans < 5) {
cout << Ans << endl;
for (int i=1;i<=Ans;i++) cout << ans[i] << endl;
return 0;
}
puts ("5");
puts ("LEFT");
puts ("IF-OPEN 5");
puts ("RIGHT");
puts ("GOTO 2");
puts ("FORWARD");
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 3636kb
input:
4 2 S# .# .# G.
output:
1 FORWARD
result:
ok correct answer!
Test #2:
score: 0
Accepted
time: 4ms
memory: 3836kb
input:
3 6 ##S..# #..##. .G..#.
output:
2 FORWARD RIGHT
result:
ok correct answer!
Test #3:
score: 0
Accepted
time: 3ms
memory: 3700kb
input:
3 7 ....S## .#.#... ##.#.G#
output:
2 FORWARD LEFT
result:
ok correct answer!
Test #4:
score: 0
Accepted
time: 7ms
memory: 3696kb
input:
4 8 ...S.#.# ##..#.#. ###...#. #.#.#G.#
output:
3 FORWARD FORWARD LEFT
result:
ok correct answer!
Test #5:
score: 0
Accepted
time: 2ms
memory: 3628kb
input:
3 5 .S#.. ..... ..#G.
output:
3 FORWARD LEFT FORWARD
result:
ok correct answer!
Test #6:
score: 0
Accepted
time: 3ms
memory: 3780kb
input:
10 10 .....S#... ...#...... ....##.... .#.....#.. ....#..... ..#....... ...#...... .......... .#...#.... G.#..#...#
output:
3 LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #7:
score: 0
Accepted
time: 5ms
memory: 3704kb
input:
7 9 .##S.#..# ......... ......... ...#...## ###..#### ........# .....##G#
output:
3 RIGHT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #8:
score: 0
Accepted
time: 6ms
memory: 3572kb
input:
3 9 ..#.#.#S# #...#...# .G....#.#
output:
3 FORWARD RIGHT FORWARD
result:
ok correct answer!
Test #9:
score: 0
Accepted
time: 5ms
memory: 3836kb
input:
4 5 #S... ..#.. .#..# .#.G#
output:
3 RIGHT FORWARD FORWARD
result:
ok correct answer!
Test #10:
score: 0
Accepted
time: 13ms
memory: 3728kb
input:
6 8 ..S..#.# ...#...# ....#..# #.#.##.. .......# .#..#G.#
output:
4 FORWARD LEFT FORWARD RIGHT
result:
ok correct answer!
Test #11:
score: 0
Accepted
time: 5ms
memory: 3628kb
input:
9 10 S......... .#..#....# ........#. .......... .....#.#.. .#...#.... #..#.##.#. ..#..#...# ..#.#....G
output:
4 LEFT FORWARD RIGHT FORWARD
result:
ok correct answer!
Test #12:
score: 0
Accepted
time: 9ms
memory: 3832kb
input:
10 4 #.S. .#.# ##.# ##.. .#.. #... #.## ..#. .#.. G.##
output:
4 FORWARD RIGHT FORWARD LEFT
result:
ok correct answer!
Test #13:
score: 0
Accepted
time: 6ms
memory: 3668kb
input:
9 9 #..#...S# .#...##.# .....##.. ..#..#... ..#..#..# .#..#.#.# .###..... ..#.#.##. G#....#.#
output:
4 RIGHT FORWARD LEFT FORWARD
result:
ok correct answer!
Test #14:
score: 0
Accepted
time: 11ms
memory: 3664kb
input:
7 3 .S# #.. ..# ... ..# .#. .G.
output:
4 FORWARD FORWARD FORWARD LEFT
result:
ok correct answer!
Test #15:
score: 0
Accepted
time: 11ms
memory: 3576kb
input:
6 6 ..#S.. #.#.#. .....# #.#... ..#... ..G##.
output:
4 FORWARD FORWARD LEFT FORWARD
result:
ok correct answer!
Test #16:
score: 0
Accepted
time: 12ms
memory: 3732kb
input:
6 8 ....#S.. ......#. ..##..#. ###.#.## ..#...#. ##.G#..#
output:
4 FORWARD FORWARD RIGHT FORWARD
result:
ok correct answer!
Test #17:
score: 0
Accepted
time: 14ms
memory: 3696kb
input:
10 8 #..S.### .#....## .#...... ....#.#. #...#..# ........ ....#.## ...#.### .#.#...# .#...G.#
output:
4 FORWARD LEFT FORWARD IF-OPEN 3
result:
ok correct answer!
Test #18:
score: 0
Accepted
time: 10ms
memory: 3772kb
input:
7 6 ...##S #..... ....#. ...#.# ....#. #..... .....G
output:
4 FORWARD RIGHT FORWARD IF-OPEN 3
result:
ok correct answer!
Test #19:
score: 0
Accepted
time: 6ms
memory: 3772kb
input:
4 8 .##..S.. #.###..# .#...#.. ...#G...
output:
4 RIGHT FORWARD FORWARD FORWARD
result:
ok correct answer!
Test #20:
score: 0
Accepted
time: 7ms
memory: 3784kb
input:
3 10 ##.......S ####.####. ...#..G##.
output:
4 RIGHT FORWARD LEFT IF-OPEN 2
result:
ok correct answer!
Test #21:
score: 0
Accepted
time: 3ms
memory: 3776kb
input:
2 7 .#S.### .#.G..#
output:
2 LEFT FORWARD
result:
ok correct answer!
Test #22:
score: 0
Accepted
time: 4ms
memory: 3784kb
input:
2 7 .S#.#.# G#.##.#
output:
2 LEFT FORWARD
result:
ok correct answer!
Test #23:
score: 0
Accepted
time: 4ms
memory: 3784kb
input:
2 4 ...S ##G#
output:
2 LEFT FORWARD
result:
ok correct answer!
Test #24:
score: 0
Accepted
time: 4ms
memory: 3676kb
input:
2 7 S...... .G.....
output:
2 LEFT FORWARD
result:
ok correct answer!
Test #25:
score: 0
Accepted
time: 4ms
memory: 3636kb
input:
2 3 #S# ..G
output:
2 LEFT FORWARD
result:
ok correct answer!
Test #26:
score: 0
Accepted
time: 3ms
memory: 3780kb
input:
2 4 #.S# #G##
output:
2 LEFT FORWARD
result:
ok correct answer!
Test #27:
score: 0
Accepted
time: 3ms
memory: 3696kb
input:
3 6 .#S### #....# #G.#..
output:
2 LEFT FORWARD
result:
ok correct answer!
Test #28:
score: 0
Accepted
time: 3ms
memory: 3780kb
input:
2 4 S.#. #G##
output:
2 RIGHT FORWARD
result:
ok correct answer!
Test #29:
score: 0
Accepted
time: 3ms
memory: 3784kb
input:
2 5 ##..S ...G.
output:
2 LEFT FORWARD
result:
ok correct answer!
Test #30:
score: 0
Accepted
time: 3ms
memory: 3668kb
input:
2 9 #S###.... G..##..##
output:
2 RIGHT FORWARD
result:
ok correct answer!
Test #31:
score: 0
Accepted
time: 3ms
memory: 3780kb
input:
6 10 ....S...## .#...#.... .......... .#.#...#.. ...#.##... .G#...#...
output:
3 LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #32:
score: 0
Accepted
time: 1ms
memory: 3572kb
input:
3 3 S.. ##. ##G
output:
3 RIGHT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #33:
score: 0
Accepted
time: 3ms
memory: 3700kb
input:
5 5 ..S.. .##.. .#### ..#.. G.#.#
output:
3 LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #34:
score: 0
Accepted
time: 3ms
memory: 3684kb
input:
4 6 S#.... ....#. #..... #...G.
output:
3 LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #35:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
3 6 .#..S# ..#..# .##G.#
output:
3 RIGHT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #36:
score: 0
Accepted
time: 0ms
memory: 3752kb
input:
4 3 ..S .#. ..# G..
output:
3 LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #37:
score: 0
Accepted
time: 5ms
memory: 3688kb
input:
5 3 S.. ##. ... ... .G.
output:
3 RIGHT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #38:
score: 0
Accepted
time: 3ms
memory: 3780kb
input:
8 2 .S .. .. .. .. .. .# G.
output:
3 LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #39:
score: 0
Accepted
time: 3ms
memory: 3780kb
input:
3 7 ...#..S #.#.... ....G..
output:
3 LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #40:
score: 0
Accepted
time: 3ms
memory: 3700kb
input:
3 9 #......S. ...#.#... .G.#.#...
output:
3 LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #41:
score: 0
Accepted
time: 3ms
memory: 3832kb
input:
2 9 .##..S... ##..##G.#
output:
4 LEFT LEFT FORWARD LEFT
result:
ok correct answer!
Test #42:
score: 0
Accepted
time: 5ms
memory: 3628kb
input:
4 6 S..#.# ..#... ...... ..#G#.
output:
4 LEFT FORWARD RIGHT FORWARD
result:
ok correct answer!
Test #43:
score: 0
Accepted
time: 2ms
memory: 3832kb
input:
4 9 #..##S..# ##..#...# #.......# ####.#G##
output:
4 LEFT LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #44:
score: 0
Accepted
time: 13ms
memory: 3784kb
input:
3 8 ....S... .....#.. ..#G#...
output:
4 FORWARD RIGHT FORWARD LEFT
result:
ok correct answer!
Test #45:
score: 0
Accepted
time: 5ms
memory: 3784kb
input:
4 9 .#.S##### ..#.###.. .#....#.# .####.G.#
output:
4 LEFT FORWARD RIGHT FORWARD
result:
ok correct answer!
Test #46:
score: 0
Accepted
time: 1ms
memory: 3780kb
input:
4 3 ..S ... ... #G#
output:
4 LEFT LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #47:
score: 0
Accepted
time: 2ms
memory: 3704kb
input:
2 7 #...S.. ..#G##.
output:
4 LEFT LEFT FORWARD RIGHT
result:
ok correct answer!
Test #48:
score: 0
Accepted
time: 3ms
memory: 3784kb
input:
5 6 ...S.. ...#.. ...#.. ...#.# #G..#.
output:
4 RIGHT FORWARD IF-OPEN 1 RIGHT
result:
ok correct answer!
Test #49:
score: 0
Accepted
time: 3ms
memory: 3576kb
input:
4 8 .#...#.S ..###... ........ ....G..#
output:
4 LEFT LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #50:
score: 0
Accepted
time: 5ms
memory: 3624kb
input:
3 4 #S.. ##.. .#G#
output:
4 LEFT FORWARD RIGHT IF-OPEN 2
result:
ok correct answer!
Test #51:
score: 0
Accepted
time: 1ms
memory: 3624kb
input:
4 6 #.S.#. #..#.. ..#... ..G##.
output:
3 LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #52:
score: 0
Accepted
time: 5ms
memory: 3680kb
input:
4 5 #..S. #.#.. ..#.. G#..#
output:
4 RIGHT FORWARD LEFT FORWARD
result:
ok correct answer!
Test #53:
score: 0
Accepted
time: 12ms
memory: 3776kb
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: 4ms
memory: 3780kb
input:
5 6 ##S..# ...#.. ...#.# ...#.. ..#G.#
output:
3 RIGHT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #55:
score: 0
Accepted
time: 1ms
memory: 3700kb
input:
6 4 ...S ..#. .... .... .... G...
output:
3 LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #56:
score: 0
Accepted
time: 3ms
memory: 3688kb
input:
5 6 ....S. ...... ...... ....#. ....G.
output:
3 LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #57:
score: 0
Accepted
time: 3ms
memory: 3688kb
input:
4 5 S###. .#### .#### ..G##
output:
3 LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #58:
score: 0
Accepted
time: 3ms
memory: 3664kb
input:
6 5 ...S. ...#. .#... ..##. .##.. .G#..
output:
3 LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #59:
score: 0
Accepted
time: 1ms
memory: 3636kb
input:
5 4 ..S. #... .#.. ..#. G...
output:
3 RIGHT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #60:
score: 0
Accepted
time: 3ms
memory: 3632kb
input:
5 5 ..#S# .#..# ....# ..#.# ###G.
output:
1 FORWARD
result:
ok correct answer!
Test #61:
score: 0
Accepted
time: 2ms
memory: 3732kb
input:
10 10 #...S..#.. ..#...#... ..#.#.#... ...#....## ##.....#.# .....#.##. .....#...# ...###.... ..#.#..... .#.#....#G
output:
4 LEFT FORWARD RIGHT FORWARD
result:
ok correct answer!
Test #62:
score: 0
Accepted
time: 1ms
memory: 3688kb
input:
10 10 .#..S#...# .....#.... ...#...#.# .##..#.... #....###.# ........## ...#..#... ##.#....## ...#....#. #.#G...#.#
output:
3 RIGHT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #63:
score: 0
Accepted
time: 3ms
memory: 3776kb
input:
10 10 .......S.. .......... .......... .......... ........#. .......... .......... .......... .......... ........G.
output:
3 LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #64:
score: 0
Accepted
time: 10ms
memory: 3788kb
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: 14ms
memory: 3772kb
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: 3704kb
input:
10 10 .........S .......... ..#....... .......... .....#.... .#........ .......... .......... .......#.. .G.......#
output:
3 LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #67:
score: 0
Accepted
time: 10ms
memory: 3832kb
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: 5ms
memory: 3632kb
input:
10 10 ......S... .......... .......... .......... .......... ...#....#. .......... #.....#... .......... .....#G...
output:
3 RIGHT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #69:
score: 0
Accepted
time: 11ms
memory: 3628kb
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: 15ms
memory: 3732kb
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: 17ms
memory: 3680kb
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: 4ms
memory: 3640kb
input:
10 10 S#..#...#. .#.....#.. .#.....#.. .........# ##.#.#.#.. .#.#.#.#.. .#.#.#.#.# .#.#.#.#.. .#.#.#.#.. ...#...#.G
output:
3 LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #73:
score: 0
Accepted
time: 2ms
memory: 3632kb
input:
10 10 S.#.#.#.#. #..#.#.#.# .#..#.#.#. #.#..#.#.# .#.#..#.#. #.#.#..#.# .#.#.#..#. #.#.#.#..# .#.#.#.#.. #.#.#.#.#G
output:
4 LEFT FORWARD RIGHT FORWARD
result:
ok correct answer!
Test #74:
score: 0
Accepted
time: 3ms
memory: 3784kb
input:
10 10 S......... .########. .#......#. .#......#. .#......#. .#......#. .#......#. .#......#. .########. .........G
output:
3 LEFT FORWARD IF-OPEN 2
result:
ok correct answer!
Test #75:
score: 0
Accepted
time: 4ms
memory: 3776kb
input:
10 10 S.#....... #.###.#### #......#.# ##.###.... .#.#...#.# ...#####.# .#.#.#...# .#.#...### ##.#####.# .........G
output:
4 LEFT FORWARD RIGHT FORWARD
result:
ok correct answer!
Test #76:
score: 0
Accepted
time: 11ms
memory: 3728kb
input:
10 10 S.#....... #.###.#### #......#.# ##.###.... .#.#...#.# ...#####.# .#.#.#...# .#.#...### ##.#####.# G.........
output:
5 LEFT IF-OPEN 5 RIGHT GOTO 2 FORWARD
result:
ok correct answer!