Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Tags
more
Archives
Today
Total
관리 메뉴

코드와이

[BAEKJOON] 20057 마법사 상어와 토네이도 본문

acmicpc

[BAEKJOON] 20057 마법사 상어와 토네이도

코드와이 2021. 9. 26. 00:04

 

문제링크

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

 

20057번: 마법사 상어와 토네이도

마법사 상어가 토네이도를 배웠고, 오늘은 토네이도를 크기가 N×N인 격자로 나누어진 모래밭에서 연습하려고 한다. 위치 (r, c)는 격자의 r행 c열을 의미하고, A[r][c]는 (r, c)에 있는 모래의 양을

www.acmicpc.net

 

달팽이 숫자 관련 문제에 많은 도움이 된다.

package acmicpc.Gold3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class 마법사_상어와_토네이도 {

	static int[][] dsx = {{-1,1,-2,-1,1,2,-1,1,0}, {-1,-1,0,0,0,0,1,1,2},    // 모래가 퍼지는 x방향
            {1,-1,2,1,-1,-2,1,-1,0}, {1,1,0,0,0,0,-1,-1,-2}};       
	static int[][] dsy = {{1,1,0,0,0,0,-1,-1,-2},{-1,1,-2,-1,1,2,-1,1,0},    // 모래가 퍼지는 y방향
               {-1,-1,0,0,0,0,1,1,2},{1,-1,2,1,-1,-2,1,-1,0}};
	static int[] sand = {1,1,2,7,7,2,10,10,5};								 // 모래 양
	static int[] dr = {0, 1, 0, -1};
	static int[] dc = {-1, 0, 1, 0};
	static int[][] map;
	static int n;
	public static void main(String[] args) throws NumberFormatException, IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		
		n = Integer.parseInt(br.readLine());
		map = new int[n][n];
		
		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());
			}
		}
		
		System.out.println(solve(n/2,n/2));
	}
	
	public static int solve(int x, int y) {
		
		int r = x;
		int c = y;
		int outSand = 0;		// map 밖으로 나가는 모래 양
		int cnt = 1;			// 지금 보고있는 방향으로 가는 횟수
		
		while(true) {
			
			for(int d = 0 ; d < 4 ; d++) {
				
				// 1,1,2,2 => 3,3,4,4 ... 식으로 현재 보고 있는 방향으로 움직이기 때문에 2번째 방향이 지나면 cnt에 1을 더한다.
				if(d == 2) cnt++;
				
				// cnt만큼 이동
				for(int i = 0 ; i < cnt ; i++) {
					
					int nr = r + dr[d];
					int nc = c + dc[d];
					
					if(nr < 0 || nc < 0 || nr >= n || nc >= n) return outSand;

					int spreadSand = 0;				// 날아간 모래의 양
					for(int j = 0 ; j < 9 ; j++) {
						int newSandR = nr + dsx[d][j];
						int newSandC = nc + dsy[d][j];
						
						spreadSand += (map[nr][nc] * sand[j]) / 100;
						// 날아간 모래가 
						// map 밖이면 outSand에 더해주고
						// map 안이면 해당 구역에 더해준다.
						if(newSandR < 0 || newSandC < 0 || newSandR >= n || newSandC >= n) {
							outSand += (map[nr][nc] * sand[j]) / 100;
						} else {
							map[newSandR][newSandC] += (map[nr][nc] * sand[j]) / 100;
						}
						
					}
					
					// 알파 계산
					int nrAlpha = nr + dr[d];
					int ncAlpha = nc + dc[d];
					
					if(nrAlpha < 0 || ncAlpha < 0 || nrAlpha >= n || ncAlpha >= n) {
						outSand += (map[nr][nc] - spreadSand);
					} else {
						map[nrAlpha][ncAlpha] += map[nr][nc] - spreadSand;
					}
					
					map[nr][nc] = 0;
					r = nr;
					c = nc;
					
				}
			}
			
			cnt++;
			
		}
		
	}
}

'acmicpc' 카테고리의 다른 글

[BAEKJOON] 17822. 원판 돌리기  (0) 2021.09.26
[BAEKJOON] 19238. 스타트 택시  (0) 2021.09.26
[BAEKJOON] 2661. 좋은 수열  (0) 2021.09.11
[BAEKJOON] 17140. 이차원 배열과 연산  (0) 2021.09.07
[BAEKJOON] 2458. 키 순서  (0) 2021.09.05