낚시왕


백준 [17143] 낚시왕

문제보기
Alt text

풀이

  • fishing() : 오른쪽으로 가면서 땅과 가까운 상어잡고 ans에 잡은 상어 추가 후 상어이동
  • moving() : 상어이동 후 이동칸에 이미 상어가 있다면 크기 비교 후 자리차지.

소스코

import java.util.Scanner;

public class Main {
	static class Shark {
		int s;
		int d;
		int z;

		public Shark(int s, int d, int z) {
			this.s = s;
			this.d = d;
			this.z = z;
		}
	}

	static int R, C, M, ans;
	static Shark[][] map;
	static int[] dx = { 0, -1, 1, 0, 0 };
	static int[] dy = { 0, 0, 0, 1, -1 };

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		R = sc.nextInt();
		C = sc.nextInt();
		M = sc.nextInt();
		ans = 0;
		map = new Shark[R][C];

		for (int i = 0; i < M; i++) {
			int r = sc.nextInt() - 1;
			int c = sc.nextInt() - 1;
			int s = sc.nextInt();
			int d = sc.nextInt();
			int z = sc.nextInt();
			map[r][c] = new Shark(s, d, z);
		}

		fishing(0);
		System.out.println(ans);
	}

	public static void fishing(int now) {
		if (now >= C) {
			return;
		}

		for (int i = 0; i < R; i++) {
			if (map[i][now] != null) {
				ans += map[i][now].z;
				map[i][now] = null;
				break;
			}
		}
		moving();
		fishing(now + 1);
	}

	public static void moving() {
		Shark[][] newMap = new Shark[R][C];
		for (int i = 0; i < R; i++) {
			for (int j = 0; j < C; j++) {
				if (map[i][j] != null) {
					int dir = map[i][j].d;
					int x = i;
					int y = j;
					for (int k = 0; k < map[i][j].s; k++) {
						int r = x + dx[dir];
						int c = y + dy[dir];

						if (isRange(r, c)) {
							x = r;
							y = c;
						} else {
							if (dir == 1) {
								dir = 2;
							} else if (dir == 2) {
								dir = 1;
							} else if (dir == 3) {
								dir = 4;
							} else if (dir == 4) {
								dir = 3;
							}
							k--;
						}
					}
					map[i][j].d = dir;
					if (newMap[x][y] == null) {
						newMap[x][y] = map[i][j];
					} else {
						if (newMap[x][y].z < map[i][j].z)
							newMap[x][y] = map[i][j];
					}

				}
			}
		}
		map = newMap;
	}

	public static boolean isRange(int x, int y) {
		return x >= 0 && y >= 0 && x < R && y < C;
	}
}