Problem ๐ป
์ด ๋ฌธ์ ๋ L ≤ 10^8 ์ด๋ฏ๋ก 2์ฐจ์ ๋ฐฐ์ด์ ๋ง๋ค์ด ํ๋ํ๋ ๋ฑ์ ์ด๋์์ผ๊ฐ๋ฉฐ ํ ์ ์๋ ๋ฌธ์ ๋ ์๋๋ค. ๊ทธ๋ฌ๋ฏ๋ก ๋จธ๋ฆฌ์ ํ์ ํ์ N์ ์ด์ฉํด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํด์ผ ํ๋ค.
Approach 1 โญ
- ๊ตฌํ
ํด๋น ๋ฌธ์ ๋ ๋ฑ์ด ๋จธ๋ฆฌ๋ฅผ 1์ด๋ง๋ค ํ๋์ฉ ๋๋ ค๊ฐ๋ฉฐ ์ด๋ค ์๊ฐ์๋ ํ์ ๋ ํ๊ณ ํ๋ฉด์ ์ต์ข ์ฃฝ๋ ์๊ฐ์ ๊ตฌํ๋ ๋ฌธ์ ์ด๋ค. ๋ฌธ์ ์ ์ฃผ์ฒด๋ฅผ ๋ฑ์ด ์๋๋ผ ์ฌ๋์ด๋ผ๊ณ ์๊ฐํ๋ฉด ์์ ์ด ๊ฐ๋ ๊ฒฝ๋ก์ ๊ฒน์น๋ ์๊ฐ ํน์ ๋ฒ์์ ๋ฐ์ผ๋ก ๊ฐ๋ ์๊ฐ ์ฃฝ๋ ๊ฒ์ผ๋ก ํด์ํ ์ ์๋ค.
๋ฑ์ ์ค๋ก์ง ์์ง, ์ํ์ผ๋ก๋ง ๋จธ๋ฆฌ๊ฐ ๋์ด๋๋ค. ๊ทธ๋์ ๋ฐฉํฅ์ ๋ฐ๊พธ๋ ์๊ฐ ์ฐจ์ด์ ๋ฐ๊พธ๋ ๋ฐฉํฅ์ด ์ฃผ์ด์ง๋ฉด ๋ฐ๊พธ๋ ์๊ฐ ์ ๊น์ง๋ ๊ฐ์ ๋ฐฉํฅ์ผ๋ก๋ง ์ด๋ํ๋ค. ๊ทธ๋์ ํด๋น ๋ฐฉํฅ ์ ๊น์ง ํ๋ํ๋ ์ด๋ํ์ง ์๊ณ ๋จ๋ฒ์ ‘x + time’ ๋๋ ‘y + time’์ผ๋ก ์ขํ๋ฅผ ๊ฒฐ์ ํ ์ ์๋ค.
์ด๋ฌํ ์ขํ๋ฅผ ํ์ฉํด ์์ง ๊ฒฝ๋ก์ ๋ํด์ ์ํ์ข์ฐ๋ก ๋ถ๋ชํ์ ๊ฒฝ์ฐ, ์ํ ๊ฒฝ๋ก์ ๋ํด์ ์ํ์ข์ฐ๋ก ๋ถ๋ชํ์ ๊ฒฝ์ฐ๋ฅผ ๋ชจ๋ ์๊ฐํ์ฌ ๊ตฌํํ๋ฉด ๋๋ค. ๊ทธ๋ฆฌ๊ณ , ๋ง์ง๋ง์ผ๋ก ๋ฐฉํฅ์ ๋ฐ๊พธ์์ ๋ ๋ฑ์ด ์ฃฝ์ง ์๊ณ ๊ณ์ ๋์๊ฐ ์ ์์ผ๋ฏ๋ก ๋ชจ๋ ๋ฐฉํฅ ์ ํ์ ๋ง์น๊ณ ๋ฑ์ด ๋ ์ด๋ํ ์ ์๋์ง๋ฅผ ๋ณด๋ฉด ๋๋ค.
ํ์ง๋ง, ์ด๋ ค์ด ๊ฒ.. ๋ชป ํ์๋ค ใ
์ฌ๋ฌ ํ์ด ๋ฐฉ๋ฒ์ด ์๊ฒ ์ง๋ง, ๋ด๊ฐ ๋ณธ ํ์ด ๋ฐฉ๋ฒ์ ์์ง, ์ํ์ผ ๋ ์ํ์ข์ฐ๋ก ๋ค๊ฐ๊ฐ๋ ๊ฒฝ๋ก์ ์์ ์ ๊ณผ ๋ณธ๋ ๊ฒฝ๋ก์ ์์ ์ ์ฌ์ด์ ์ฐจ์ด ์ค ์์ ๊ฐ์ ๊ณ ๋ฅด๊ณ ํด๋น ๊ฐ์ด ๋ฐฉํฅ์ ํ์ ํ๋ ์๊ฐ์ ์ฐจ์ด๋ณด๋ค ํฌ๋ฉด ๋ถ๋ชํ ๊ฒ์ด๊ณ ์์ผ๋ฉด ๋ถ๋ชํ์ง ์์ ๊ฒ!
์ด์ ์ ์ํ ๊ฒฝ๋ก๋ฅผ ์๋์์ ์๋ก ๋ถ๋ชํ๋ ๊ฒฝ์ฐ(์ํ์ข์ฐ ์ค ์)๋ฅผ ๊ทธ๋ฆผ์ผ๋ก ๋ณด๋ฉด ์์ ๊ฐ๋ค. time ์ฆ, ๋ฐฉํฅ์ ๋ฐ๊พธ๊ธฐ ์ ํ์ฌ ์์ง์ด๋ ๊ฒฝ๋ก๋ฅผ ๋ชจ๋ ์์ง์ธ ๊ฒฝ์ฐ์ ๊ธธ์ด์ ํ์ฌ ๊ฒฝ๋ก์ ์์์ ๊ณผ ์ํ ๊ฒฝ๋ก์ ์ฐจ์ด ์ค ํด๋น ๊ฒฝ๋ก๋ฅผ ๋ชจ๋ ์์ง์ธ ๊ฒฝ์ฐ๊ฐ ๋ ํฌ๋ค๋ฉด, ๋ฌด์กฐ๊ฑด ๋ถ๋ชํ๊ฒ ๋์ด ์๊ณ ์์ผ๋ฉด ๋ถ๋ชํ์ง ์๋๋ค!
์ด๊ฑธ 8๊ฐ์ง ๊ฒฝ์ฐ์ ๋ชจ๋ ์๊ฐํด์ ์ ์ฉํด์ฃผ๋ฉด ์ ๋ต์ด ๋๋ค.
Solution ๐ก
public class BOJ_10875 {
private static int[] dy = {0, 1, 0, -1};
private static int[] dx = {1, 0, -1, 0};
private static class Command {
long time;
char cmd;
public Command(long time, char cmd) {
this.time = time;
this.cmd = cmd;
}
}
private static class Snake {
long sy, sx;
long ey, ex;
public Snake(long sy, long sx, long ey, long ex) {
this.sy = sy;
this.sx = sx;
this.ey = ey;
this.ex = ex;
if (this.sy > this.ey) {
long tmp = this.sy;
this.sy = this.ey;
this.ey = tmp;
}
if (this.sx > this.ex) {
long tmp = this.sx;
this.sx = this.ex;
this.ex = tmp;
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int l = Integer.parseInt(br.readLine());
int n = Integer.parseInt(br.readLine());
ArrayList<Command> cmd = new ArrayList<>();
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
long time = Long.parseLong(st.nextToken());
char c = st.nextToken().charAt(0);
cmd.add(new Command(time, c));
}
ArrayList<Snake> snakes = new ArrayList<>();
snakes.add(new Snake(-l-1, -l-1, l+1, -l-1));
snakes.add(new Snake(l+1, -l-1, l+1, l+1));
snakes.add(new Snake(l+1, l+1, -l-1, l+1));
snakes.add(new Snake(-l-1, l+1, -l-1, -l-1));
long y = 0;
long x = 0;
int dir = 0;
long ret = 0;
boolean done = false;
Command command;
for (int i = 0; i < cmd.size()+1; i++) {
if (i == cmd.size()) {
command = new Command(Long.MAX_VALUE, 'L');
} else {
command = cmd.get(i);
}
long t = Long.MAX_VALUE;
for (int idx = 0; idx < snakes.size(); idx++) {
if (snakes.get(idx).ey == snakes.get(idx).sy) {
if (dir == 0) { // ์ค๋ฅธ
if (y == snakes.get(idx).ey && x < snakes.get(idx).sx)
t = Math.min(t, snakes.get(idx).sx - x);
} else if (dir == 1) { // ์
if (snakes.get(idx).sx <= x && x <= snakes.get(idx).ex && y < snakes.get(idx).sy)
t = Math.min(t, snakes.get(idx).sy - y);
} else if (dir == 2) { // ์ผ
if (snakes.get(idx).ey == y && x > snakes.get(idx).ex)
t = Math.min(t, x - snakes.get(idx).ex);
} else { // ์๋
if (snakes.get(idx).sx <= x && x <= snakes.get(idx).ex && y > snakes.get(idx).sy)
t = Math.min(t, y - snakes.get(idx).sy);
}
} else if (snakes.get(idx).ex == snakes.get(idx).sx) {
if (dir == 0) {
if (snakes.get(idx).sy <= y && y <= snakes.get(idx).ey && x < snakes.get(idx).sx)
t = Math.min(t, snakes.get(idx).sx - x);
} else if (dir == 1) {
if (snakes.get(idx).sx == x && y < snakes.get(idx).sy)
t = Math.min(t, snakes.get(idx).sy - y);
} else if (dir == 2) {
if (snakes.get(idx).ey >= y && y >= snakes.get(idx).sy && x > snakes.get(idx).sx)
t = Math.min(t, x - snakes.get(idx).sx);
} else {
if (snakes.get(idx).sx == x && y > snakes.get(idx).ey)
t = Math.min(t, y - snakes.get(idx).ey);
}
}
}
if (command.time < t) {
snakes.add(new Snake(y, x, y + dy[dir] * command.time, x + dx[dir] * command.time));
ret += command.time;
y = y + dy[dir] * command.time;
x = x + dx[dir] * command.time;
if (command.cmd == 'L') dir = (dir + 1) % 4;
else dir = (dir - 1 < 0) ? 3 : dir - 1;
} else {
ret += t;
break;
}
}
System.out.println(ret);
}
}
'์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
1800. ์ธํฐ๋ท ์ค์น (0) | 2025.02.05 |
---|---|
14658. ํ๋์์ ๋ณ๋ฅ๋ณ์ด ๋น๋ฐ์น๋ค (0) | 2025.01.29 |
1863. ์ค์นด์ด๋ผ์ธ ์ฌ์ด๊ฑฐ (0) | 2025.01.22 |
4485. ๋ น์ ์ท ์ ์ ์ ๊ฐ ์ ค๋ค์ง? (0) | 2025.01.19 |
1238. ํํฐ (0) | 2025.01.17 |