알고리즘/문제 풀이

[Java/자바 백준 20057] 마법사 상어와 토네이도

광규니 2022. 1. 10. 15:53
반응형

풀이

2가지 구현이 핵심이었던 문제

1. 토네이도 루트 구현 -> 규칙이 있어서 나름 쉬움

2. 흩날리는 모래 구현  -> 꽤 까다로움

그림과 같이 소수점으로 떨어지게 되면 버림으로 해서 각각 계산해주어, 1 이상이면 map += 더해주고,

흩날리지 않은 나머지들을 모두 알파에 넣어줘야한다. (처음 퍼센트를 다 계산하니까 나머지가 55여서 55로 계산했다가 시간을 날렸다.)

 

그 과정에서 서 남 동 북 순서대로 3차원 배열을 사용해서 위치를 하나하나씩 구해주었다.

함수가 진행하면서 map 밖으로 나가면 answer에 더해주고 출력 !

package b0108;
import java.io.*;
import java.util.*;

public class Main_bj_g4_20057_마법사상어와토네이도 {
	static int N, curX, curY, curDir, answer;
	static int[][] map;
	//서 남 동 북 
	static int[] dx = {0,1,0,-1};
	static int[] dy = {-1,0,1,0};
	static int[][][] direct = { 
			// 모래 위치 기준으로 방향에 따라서 나눠주기 
			//서 
			{ { 1, -1, 2, -2, 0, 1, -1, 1, -1, 0 }, { 1, 1, 0, 0, -2, 0, 0, -1, -1, -1 } },
			// 남 
			{ { -1, -1, 0, 0, 2, 0, 0, 1, 1, 1 }, { -1, 1, -2, 2, 0, -1, 1, -1, 1, 0 } },
			// 동 
			{ { 1, -1, 2, -2, 0, 1, -1, 1, -1, 0 }, { -1, -1, 0, 0, 2, 0, 0, 1, 1, 1 } },
			// 북 
			{ { 1, 1, 0, 0, -2, 0, 0, -1, -1, -1 }, { -1, 1, -2, 2, 0, -1, 1, -1, 1, 0 } } 
	};
	static int[] percent = {1, 1, 2, 2, 5, 7, 7, 10, 10};
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		N = Integer.parseInt(br.readLine());
		map = new int[N][N];

		answer = 0;
		for(int i = 0 ; i < N ; i++) {
			st = new StringTokenizer(br.readLine(), " ");
			for(int j = 0 ; j < N ; j++) {
				map[i][j] = Integer.parseInt(st.nextToken());
			}
		}
		curX = N/2;
		curY = N/2;

		int idx = -1;
		int len = 0;
		curDir = 0;
		while(true) {
			if(++idx%2 == 0) {
				len ++;
			}
			for(int i = 0 ; i < len ; i++) {
				sandMove();
				windMove();
			}

			curDir = (curDir+1) % 4;

			if(len == N) {
				break;
			}
		}
		System.out.println(answer);
	}

	private static void windMove() {
		curX = curX + dx[curDir];
		curY = curY + dy[curDir];
	}

	private static void sandMove() {
		int nx = curX + dx[curDir];
		int ny = curY + dy[curDir];
		int sandX = nx;
		int sandY = ny;
		if(0> nx || nx>= N || 0>ny || ny>=N) return;

		int sand = map[nx][ny];

		if(sand == 0) return;

		for(int i = 0 ; i < 9 ; i++) {
			nx = sandX + direct[curDir][0][i];
			ny = sandY + direct[curDir][1][i];

			int amount = map[sandX][sandY] * percent[i] / 100;
			sand -= amount;

			if(0> nx || nx>= N || 0>ny || ny>=N) {
				answer += amount;
			}
			else map[nx][ny] += amount;
		}

		nx = sandX + direct[curDir][0][9];
		ny = sandY + direct[curDir][1][9];

		if(0> nx || nx>= N || 0>ny || ny>=N) {
			answer += sand;
			map[curX][curY] = 0;
		}
		else map[nx][ny] += sand;
	}

}

 

반응형