슈콩

[BOJ] 17143 낚시왕 본문

Algorithms/Baekjoon

[BOJ] 17143 낚시왕

shukong 2025. 12. 6. 23:47

 

[문제]

https://www.acmicpc.net/problem/17143

 

 

[소스 코드]

import java.io.*;
import java.util.*;

public class Main {
	static class Shark{
		int r,c,s,d,z;
		Shark(int r,int c,int s,int d,int z){
			this.r = r;
			this.c = c;
			this.s = s;
			this.d = d;
			this.z = z;
		}
	}
	static int[] dr = {-1,0,1,0};
	static int[] dc = {0,-1,0,1};
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int R = Integer.parseInt(st.nextToken());
		int C = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());
		Shark[][] map = new Shark[R+1][C+1];
		for(int i=0;i<m;i++) {
			st = new StringTokenizer(br.readLine());
			int r = Integer.parseInt(st.nextToken());
			int c = Integer.parseInt(st.nextToken());
			int s = Integer.parseInt(st.nextToken());
			int d = Integer.parseInt(st.nextToken());
			int z = Integer.parseInt(st.nextToken());
			if(d==1) d=0; 
			if(d==4) d=1;
			if(d==0 || d==2) s = s %= (R-1)*2;
			if(d==1 || d==3) s = s %= (C-1)*2;
			map[r][c] = new Shark(r,c,s,d,z);
		}
		int result = 0;
		for(int col=1;col<=C;col++) {
			for(int row=1;row<=R;row++) {
				if(map[row][col]!=null) {
					result += map[row][col].z;
					map[row][col] = null;
					break;
				}
			}
			Queue<Shark> q = new LinkedList<>();
			for(int i=1;i<=R;i++) {
				for(int j=1;j<=C;j++) {
					if(map[i][j]!=null) q.offer(map[i][j]);
				}
			}
			map = new Shark[R+1][C+1];
			while(!q.isEmpty()) {
				Shark shark = q.poll();
				int s = shark.s;
				for(int t=0;t<s;t++) {
					int nr = shark.r + dr[shark.d];
					int nc = shark.c + dc[shark.d];
					if(nr<=0 || nr>R || nc<=0 || nc>C) {
						shark.r -= dr[shark.d];
						shark.c -= dc[shark.d];
						shark.d = (shark.d+2) % 4;
					}
					else {
						shark.r = nr;
						shark.c = nc;
					}
				}
				if(map[shark.r][shark.c]!=null) {
					if(map[shark.r][shark.c].z<shark.z) {
						map[shark.r][shark.c] = shark; 
					}
				}
				else map[shark.r][shark.c] = shark;
			}
		}
		System.out.println(result);
	}
}

 

배열에 1개 고정 : 클래스 객체 사용 !

'Algorithms > Baekjoon' 카테고리의 다른 글

[BOJ] 17142 연구소3  (3) 2025.12.07
[BOJ] 17140 이차원 배열과 연산  (0) 2025.12.07
[BOJ] 17144 미세먼지 안녕!  (0) 2025.12.06
[BOJ] 16236 아기 상어  (0) 2025.12.06
[BOJ] 16235 나무 재테크  (0) 2025.12.05