QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#18048#2265. Short Codinglaihaochen#AC ✓4ms2008kbC++172.2kb2022-01-15 21:51:122022-05-04 16:46:11

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-04 16:46:11]
  • 评测
  • 测评结果:AC
  • 用时:4ms
  • 内存:2008kb
  • [2022-01-15 21:51:12]
  • 提交

answer

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 15;
int n, m, sx, sy;
char s[N][N];
bool vis[N][N][5][4];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
//1 GOTO 1
//2 GOTO 2
//3 GOTO 3
//4 GOTO 4
//5 IF-OPEN 1
//6 IF-OPEN 2
//7 IF-OPEN 3
//8 IF-OPEN 4
//9 FORWARD
//10 LEFT 
//11 RIGHT 
int len, a[5], b[5], ans = 5;
bool check (int x, int y, int p, int d) {
	if (s[x][y] == 'G') return 1;
	if (vis[x][y][p][d]) return 0;
	vis[x][y][p][d] = 1;
	if (a[p] <= 4) {
		p = a[p];
	} else if (a[p] <= 8) {
		int nx = x + dx[d], ny = y + dy[d];
		if (1 <= nx && nx <= n && 1 <= ny && ny <= m && s[nx][ny] != '#') {
			p = a[p] - 4;
		} else {
			p = p % len + 1;
		}
	} else if (a[p] == 9) {
		int nx = x + dx[d], ny = y + dy[d];
		if (1 <= nx && nx <= n && 1 <= ny && ny <= m && s[nx][ny] != '#') {
			x = nx, y = ny;
		}
		p = p % len + 1;
	} else if (a[p] == 10) {
		d = (d + 3) % 4;
		p = p % len + 1;
	} else {
		d = (d + 1) % 4;
		p = p % len + 1;
	}
	return check (x, y, p, d);
}
void dfs (int cur) {
	if (cur == 5) return;
	for (int i = 1; i <= 11; i++) {
		if (i <= 4) {
			if (i == cur || i > cur) continue;
		} else if (i <= 8) {
			if (i == cur + 4 || i > cur + 4) continue;
		}
		a[cur] = i, len = cur;
		memset (vis, 0, sizeof(vis));
		if (check(sx, sy, 1, 1)) {
			if (len < ans) {
				ans = len;
				for (int j = 1; j <= len; j++) b[j] = a[j];				
			}
		} else {
			dfs (cur + 1);
		}
	}
}
int main() {
	scanf ("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) {
		scanf ("%s", s[i] + 1);
		for (int j = 1; j <= m; j++) {
			if (s[i][j] == 'S') sx = i, sy = j;
		}
	}
	dfs (1);
	printf ("%d\n", ans);
	if (ans == 5) {
		printf ("LEFT\n");
		printf ("IF-OPEN 5\n");
		printf ("RIGHT\n");
		printf ("GOTO 2\n");
		printf ("FORWARD\n");
	} else {
		for (int i = 1; i <= ans; i++) {
			if (b[i] <= 4) {
				printf ("GOTO %d\n", b[i]);
			} else if (b[i] <= 8) {
				printf ("IF-OPEN %d\n", b[i] - 4);
			} else if (b[i] == 9) {
				printf ("FORWARD\n");
			} else if (b[i] == 10) {
				printf ("LEFT\n");
			} else {
				printf ("RIGHT\n");
			}
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 1824kb

input:

4 2
S#
.#
.#
G.

output:

1
FORWARD

result:

ok correct answer!

Test #2:

score: 0
Accepted
time: 1ms
memory: 1772kb

input:

3 6
##S..#
#..##.
.G..#.

output:

2
FORWARD
RIGHT

result:

ok correct answer!

Test #3:

score: 0
Accepted
time: 0ms
memory: 1812kb

input:

3 7
....S##
.#.#...
##.#.G#

output:

2
FORWARD
LEFT

result:

ok correct answer!

Test #4:

score: 0
Accepted
time: 1ms
memory: 1812kb

input:

4 8
...S.#.#
##..#.#.
###...#.
#.#.#G.#

output:

3
FORWARD
FORWARD
LEFT

result:

ok correct answer!

Test #5:

score: 0
Accepted
time: 1ms
memory: 1708kb

input:

3 5
.S#..
.....
..#G.

output:

3
FORWARD
LEFT
FORWARD

result:

ok correct answer!

Test #6:

score: 0
Accepted
time: 1ms
memory: 1816kb

input:

10 10
.....S#...
...#......
....##....
.#.....#..
....#.....
..#.......
...#......
..........
.#...#....
G.#..#...#

output:

3
LEFT
FORWARD
IF-OPEN 2

result:

ok correct answer!

Test #7:

score: 0
Accepted
time: 0ms
memory: 1816kb

input:

7 9
.##S.#..#
.........
.........
...#...##
###..####
........#
.....##G#

output:

3
RIGHT
FORWARD
IF-OPEN 2

result:

ok correct answer!

Test #8:

score: 0
Accepted
time: 1ms
memory: 1824kb

input:

3 9
..#.#.#S#
#...#...#
.G....#.#

output:

3
FORWARD
RIGHT
FORWARD

result:

ok correct answer!

Test #9:

score: 0
Accepted
time: 0ms
memory: 1828kb

input:

4 5
#S...
..#..
.#..#
.#.G#

output:

3
RIGHT
FORWARD
FORWARD

result:

ok correct answer!

Test #10:

score: 0
Accepted
time: 1ms
memory: 1708kb

input:

6 8
..S..#.#
...#...#
....#..#
#.#.##..
.......#
.#..#G.#

output:

4
FORWARD
LEFT
FORWARD
RIGHT

result:

ok correct answer!

Test #11:

score: 0
Accepted
time: 0ms
memory: 1696kb

input:

9 10
S.........
.#..#....#
........#.
..........
.....#.#..
.#...#....
#..#.##.#.
..#..#...#
..#.#....G

output:

4
LEFT
FORWARD
RIGHT
FORWARD

result:

ok correct answer!

Test #12:

score: 0
Accepted
time: 0ms
memory: 1768kb

input:

10 4
#.S.
.#.#
##.#
##..
.#..
#...
#.##
..#.
.#..
G.##

output:

4
FORWARD
RIGHT
FORWARD
LEFT

result:

ok correct answer!

Test #13:

score: 0
Accepted
time: 1ms
memory: 1968kb

input:

9 9
#..#...S#
.#...##.#
.....##..
..#..#...
..#..#..#
.#..#.#.#
.###.....
..#.#.##.
G#....#.#

output:

4
RIGHT
FORWARD
LEFT
FORWARD

result:

ok correct answer!

Test #14:

score: 0
Accepted
time: 1ms
memory: 1720kb

input:

7 3
.S#
#..
..#
...
..#
.#.
.G.

output:

4
FORWARD
FORWARD
FORWARD
LEFT

result:

ok correct answer!

Test #15:

score: 0
Accepted
time: 1ms
memory: 1728kb

input:

6 6
..#S..
#.#.#.
.....#
#.#...
..#...
..G##.

output:

4
FORWARD
FORWARD
LEFT
FORWARD

result:

ok correct answer!

Test #16:

score: 0
Accepted
time: 1ms
memory: 1708kb

input:

6 8
....#S..
......#.
..##..#.
###.#.##
..#...#.
##.G#..#

output:

4
FORWARD
FORWARD
RIGHT
FORWARD

result:

ok correct answer!

Test #17:

score: 0
Accepted
time: 1ms
memory: 1824kb

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: 1708kb

input:

7 6
...##S
#.....
....#.
...#.#
....#.
#.....
.....G

output:

4
FORWARD
RIGHT
FORWARD
IF-OPEN 3

result:

ok correct answer!

Test #19:

score: 0
Accepted
time: 0ms
memory: 1820kb

input:

4 8
.##..S..
#.###..#
.#...#..
...#G...

output:

4
RIGHT
FORWARD
FORWARD
FORWARD

result:

ok correct answer!

Test #20:

score: 0
Accepted
time: 1ms
memory: 1828kb

input:

3 10
##.......S
####.####.
...#..G##.

output:

4
RIGHT
FORWARD
LEFT
IF-OPEN 2

result:

ok correct answer!

Test #21:

score: 0
Accepted
time: 1ms
memory: 1768kb

input:

2 7
.#S.###
.#.G..#

output:

2
FORWARD
LEFT

result:

ok correct answer!

Test #22:

score: 0
Accepted
time: 1ms
memory: 1764kb

input:

2 7
.S#.#.#
G#.##.#

output:

2
FORWARD
LEFT

result:

ok correct answer!

Test #23:

score: 0
Accepted
time: 1ms
memory: 1828kb

input:

2 4
...S
##G#

output:

2
FORWARD
LEFT

result:

ok correct answer!

Test #24:

score: 0
Accepted
time: 1ms
memory: 1768kb

input:

2 7
S......
.G.....

output:

2
FORWARD
LEFT

result:

ok correct answer!

Test #25:

score: 0
Accepted
time: 1ms
memory: 1776kb

input:

2 3
#S#
..G

output:

2
FORWARD
LEFT

result:

ok correct answer!

Test #26:

score: 0
Accepted
time: 1ms
memory: 2008kb

input:

2 4
#.S#
#G##

output:

2
FORWARD
LEFT

result:

ok correct answer!

Test #27:

score: 0
Accepted
time: 1ms
memory: 1824kb

input:

3 6
.#S###
#....#
#G.#..

output:

2
FORWARD
LEFT

result:

ok correct answer!

Test #28:

score: 0
Accepted
time: 1ms
memory: 1828kb

input:

2 4
S.#.
#G##

output:

2
FORWARD
RIGHT

result:

ok correct answer!

Test #29:

score: 0
Accepted
time: 1ms
memory: 1816kb

input:

2 5
##..S
...G.

output:

2
FORWARD
LEFT

result:

ok correct answer!

Test #30:

score: 0
Accepted
time: 1ms
memory: 1828kb

input:

2 9
#S###....
G..##..##

output:

2
FORWARD
RIGHT

result:

ok correct answer!

Test #31:

score: 0
Accepted
time: 1ms
memory: 1968kb

input:

6 10
....S...##
.#...#....
..........
.#.#...#..
...#.##...
.G#...#...

output:

3
LEFT
FORWARD
IF-OPEN 2

result:

ok correct answer!

Test #32:

score: 0
Accepted
time: 1ms
memory: 1808kb

input:

3 3
S..
##.
##G

output:

3
FORWARD
IF-OPEN 1
RIGHT

result:

ok correct answer!

Test #33:

score: 0
Accepted
time: 1ms
memory: 1772kb

input:

5 5
..S..
.##..
.####
..#..
G.#.#

output:

3
FORWARD
IF-OPEN 1
LEFT

result:

ok correct answer!

Test #34:

score: 0
Accepted
time: 1ms
memory: 1808kb

input:

4 6
S#....
....#.
#.....
#...G.

output:

3
FORWARD
IF-OPEN 1
LEFT

result:

ok correct answer!

Test #35:

score: 0
Accepted
time: 1ms
memory: 1772kb

input:

3 6
.#..S#
..#..#
.##G.#

output:

3
FORWARD
IF-OPEN 1
RIGHT

result:

ok correct answer!

Test #36:

score: 0
Accepted
time: 1ms
memory: 1772kb

input:

4 3
..S
.#.
..#
G..

output:

3
FORWARD
IF-OPEN 1
LEFT

result:

ok correct answer!

Test #37:

score: 0
Accepted
time: 1ms
memory: 1708kb

input:

5 3
S..
##.
...
...
.G.

output:

3
FORWARD
IF-OPEN 1
RIGHT

result:

ok correct answer!

Test #38:

score: 0
Accepted
time: 1ms
memory: 1708kb

input:

8 2
.S
..
..
..
..
..
.#
G.

output:

3
FORWARD
IF-OPEN 1
LEFT

result:

ok correct answer!

Test #39:

score: 0
Accepted
time: 1ms
memory: 1776kb

input:

3 7
...#..S
#.#....
....G..

output:

3
FORWARD
IF-OPEN 1
LEFT

result:

ok correct answer!

Test #40:

score: 0
Accepted
time: 1ms
memory: 1820kb

input:

3 9
#......S.
...#.#...
.G.#.#...

output:

3
FORWARD
IF-OPEN 1
LEFT

result:

ok correct answer!

Test #41:

score: 0
Accepted
time: 1ms
memory: 1808kb

input:

2 9
.##..S...
##..##G.#

output:

4
FORWARD
FORWARD
FORWARD
RIGHT

result:

ok correct answer!

Test #42:

score: 0
Accepted
time: 0ms
memory: 1968kb

input:

4 6
S..#.#
..#...
......
..#G#.

output:

4
FORWARD
FORWARD
LEFT
FORWARD

result:

ok correct answer!

Test #43:

score: 0
Accepted
time: 0ms
memory: 1880kb

input:

4 9
#..##S..#
##..#...#
#.......#
####.#G##

output:

4
LEFT
FORWARD
IF-OPEN 1
LEFT

result:

ok correct answer!

Test #44:

score: 0
Accepted
time: 1ms
memory: 1768kb

input:

3 8
....S...
.....#..
..#G#...

output:

4
FORWARD
RIGHT
FORWARD
LEFT

result:

ok correct answer!

Test #45:

score: 0
Accepted
time: 1ms
memory: 1772kb

input:

4 9
.#.S#####
..#.###..
.#....#.#
.####.G.#

output:

4
FORWARD
LEFT
FORWARD
RIGHT

result:

ok correct answer!

Test #46:

score: 0
Accepted
time: 0ms
memory: 1776kb

input:

4 3
..S
...
...
#G#

output:

4
LEFT
FORWARD
IF-OPEN 1
LEFT

result:

ok correct answer!

Test #47:

score: 0
Accepted
time: 1ms
memory: 1828kb

input:

2 7
#...S..
..#G##.

output:

4
FORWARD
FORWARD
FORWARD
LEFT

result:

ok correct answer!

Test #48:

score: 0
Accepted
time: 1ms
memory: 1808kb

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: 1824kb

input:

4 8
.#...#.S
..###...
........
....G..#

output:

4
FORWARD
RIGHT
FORWARD
LEFT

result:

ok correct answer!

Test #50:

score: 0
Accepted
time: 1ms
memory: 1712kb

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: 1816kb

input:

4 6
#.S.#.
#..#..
..#...
..G##.

output:

3
FORWARD
IF-OPEN 1
LEFT

result:

ok correct answer!

Test #52:

score: 0
Accepted
time: 1ms
memory: 1732kb

input:

4 5
#..S.
#.#..
..#..
G#..#

output:

4
RIGHT
FORWARD
LEFT
FORWARD

result:

ok correct answer!

Test #53:

score: 0
Accepted
time: 1ms
memory: 1768kb

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: 1772kb

input:

5 6
##S..#
...#..
...#.#
...#..
..#G.#

output:

3
RIGHT
FORWARD
IF-OPEN 2

result:

ok correct answer!

Test #55:

score: 0
Accepted
time: 1ms
memory: 1816kb

input:

6 4
...S
..#.
....
....
....
G...

output:

3
FORWARD
IF-OPEN 1
LEFT

result:

ok correct answer!

Test #56:

score: 0
Accepted
time: 0ms
memory: 1768kb

input:

5 6
....S.
......
......
....#.
....G.

output:

3
FORWARD
IF-OPEN 1
LEFT

result:

ok correct answer!

Test #57:

score: 0
Accepted
time: 0ms
memory: 1824kb

input:

4 5
S###.
.####
.####
..G##

output:

3
FORWARD
IF-OPEN 1
LEFT

result:

ok correct answer!

Test #58:

score: 0
Accepted
time: 1ms
memory: 1880kb

input:

6 5
...S.
...#.
.#...
..##.
.##..
.G#..

output:

3
FORWARD
IF-OPEN 1
LEFT

result:

ok correct answer!

Test #59:

score: 0
Accepted
time: 0ms
memory: 1960kb

input:

5 4
..S.
#...
.#..
..#.
G...

output:

3
FORWARD
IF-OPEN 1
RIGHT

result:

ok correct answer!

Test #60:

score: 0
Accepted
time: 0ms
memory: 1748kb

input:

5 5
..#S#
.#..#
....#
..#.#
###G.

output:

1
FORWARD

result:

ok correct answer!

Test #61:

score: 0
Accepted
time: 1ms
memory: 1776kb

input:

10 10
#...S..#..
..#...#...
..#.#.#...
...#....##
##.....#.#
.....#.##.
.....#...#
...###....
..#.#.....
.#.#....#G

output:

4
FORWARD
LEFT
FORWARD
RIGHT

result:

ok correct answer!

Test #62:

score: 0
Accepted
time: 1ms
memory: 1764kb

input:

10 10
.#..S#...#
.....#....
...#...#.#
.##..#....
#....###.#
........##
...#..#...
##.#....##
...#....#.
#.#G...#.#

output:

3
FORWARD
IF-OPEN 1
RIGHT

result:

ok correct answer!

Test #63:

score: 0
Accepted
time: 1ms
memory: 1772kb

input:

10 10
.......S..
..........
..........
..........
........#.
..........
..........
..........
..........
........G.

output:

3
FORWARD
IF-OPEN 1
LEFT

result:

ok correct answer!

Test #64:

score: 0
Accepted
time: 1ms
memory: 1808kb

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: 1ms
memory: 1964kb

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: 0ms
memory: 1776kb

input:

10 10
.........S
..........
..#.......
..........
.....#....
.#........
..........
..........
.......#..
.G.......#

output:

3
FORWARD
IF-OPEN 1
LEFT

result:

ok correct answer!

Test #67:

score: 0
Accepted
time: 0ms
memory: 1776kb

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: 1ms
memory: 1812kb

input:

10 10
......S...
..........
..........
..........
..........
...#....#.
..........
#.....#...
..........
.....#G...

output:

3
FORWARD
IF-OPEN 1
RIGHT

result:

ok correct answer!

Test #69:

score: 0
Accepted
time: 1ms
memory: 1840kb

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: 0ms
memory: 1880kb

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: 1ms
memory: 1772kb

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: 1ms
memory: 1728kb

input:

10 10
S#..#...#.
.#.....#..
.#.....#..
.........#
##.#.#.#..
.#.#.#.#..
.#.#.#.#.#
.#.#.#.#..
.#.#.#.#..
...#...#.G

output:

3
FORWARD
IF-OPEN 1
LEFT

result:

ok correct answer!

Test #73:

score: 0
Accepted
time: 1ms
memory: 1772kb

input:

10 10
S.#.#.#.#.
#..#.#.#.#
.#..#.#.#.
#.#..#.#.#
.#.#..#.#.
#.#.#..#.#
.#.#.#..#.
#.#.#.#..#
.#.#.#.#..
#.#.#.#.#G

output:

4
FORWARD
LEFT
FORWARD
RIGHT

result:

ok correct answer!

Test #74:

score: 0
Accepted
time: 1ms
memory: 1824kb

input:

10 10
S.........
.########.
.#......#.
.#......#.
.#......#.
.#......#.
.#......#.
.#......#.
.########.
.........G

output:

3
FORWARD
IF-OPEN 1
LEFT

result:

ok correct answer!

Test #75:

score: 0
Accepted
time: 0ms
memory: 1760kb

input:

10 10
S.#.......
#.###.####
#......#.#
##.###....
.#.#...#.#
...#####.#
.#.#.#...#
.#.#...###
##.#####.#
.........G

output:

4
FORWARD
LEFT
FORWARD
RIGHT

result:

ok correct answer!

Test #76:

score: 0
Accepted
time: 1ms
memory: 1824kb

input:

10 10
S.#.......
#.###.####
#......#.#
##.###....
.#.#...#.#
...#####.#
.#.#.#...#
.#.#...###
##.#####.#
G.........

output:

5
LEFT
IF-OPEN 5
RIGHT
GOTO 2
FORWARD

result:

ok correct answer!