반응형
https://www.acmicpc.net/problem/20057
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Main {
static int N, x, y, answer;
static int[][] map;
static int[][] dir = {{0,-1}, {1,0}, {0,1}, {-1,0}};
static int[][] dia = {{1, -1}, {1, 1}, {-1, 1}, {-1, -1}};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
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());
}
}
x = y = N/2;
int d = 0;
int temp = 1;
Out : for(;;) {
// 이동 1
for(int j=0; j<temp; j++) {
x += dir[d][0];
y += dir[d][1];
tornado(x, y, d);
// print();
if(x == 0 && y == 0) break Out;
}
d++;
if(d == 4) d = 0;
// 이동 2
for(int j=0; j<temp; j++) {
x += dir[d][0];
y += dir[d][1];
tornado(x, y, d);
// print();
if(x == 0 && y == 0) break Out;
}
d ++;
if(d == 4) d = 0;
temp++;
}
System.out.println(answer);
}
public static void tornado(int r, int c, int direct) {
int value = map[r][c], nx, ny;
int per1 = (int)(value * 0.01);
int per2 = (int)(value * 0.02);
int per5 = (int)(value * 0.05);
int per7 = (int)(value * 0.07);
int per10 = (int)(value * 0.1);
// System.out.println("######### value : "+value + " / direct : " + direct + " / "+ per1 + " " + per2 + " " + per5 + " " + per7 + " " + per10);
for(int i=0; i<8; i++) {
if(i == 4) continue;
if(i%2 == 0) {
if(i==0) {
// a
nx = r + dir[direct][0];
ny = c + dir[direct][1];
int a = value - (2*(per1+per2+per7+per10) + per5);
if(isInside(nx, ny)) {
map[nx][ny] += a;
} else answer += a;
// 5%
nx = r + 2 * dir[direct][0];
ny = c + 2 * dir[direct][1];
if(isInside(nx, ny)) map[nx][ny] += per5;
else answer += per5;
} else {
//7%
nx = r + dir[direct][0];
ny = c + dir[direct][1];
if(isInside(nx, ny)) map[nx][ny] += per7;
else answer += per7;
//2%
nx = r + 2*dir[direct][0];
ny = c + 2*dir[direct][1];
if(isInside(nx, ny)) map[nx][ny] += per2;
else answer += per2;
}
} else {
if(i == 1 || i == 7) { // 10%
nx = r + dia[direct][0];
ny = c + dia[direct][1];
if(isInside(nx, ny)) map[nx][ny] += per10;
else answer += per10;
// System.out.println("here!!!!!!" + direct + " " + nx + " " + ny);
} else { // 1%
nx = r + dia[direct][0];
ny = c + dia[direct][1];
if(isInside(nx, ny)) map[nx][ny] += per1;
else answer += per1;
}
direct++;
if(direct==4) direct=0;
}
}
map[r][c] = 0;
}
public static void print() {
for(int i=0; i<N; i++) {
for(int j=0; j<N; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
public static boolean isInside(int x, int y) {
return x>=0 && y>=0 && x<N && y<N;
}
}
반응형
'알고리즘 > SW 역량 테스트' 카테고리의 다른 글
[백준] 21608. 상어 초등학교 (0) | 2021.10.01 |
---|---|
[백준] 20056. 마법사 상어와 파이어볼 (0) | 2021.09.20 |
[백준] 20058. 마법사 상어와 파이어스톰 (0) | 2021.08.11 |
[백준] 21610. 마법사 상어와 비바라기 (0) | 2021.08.08 |
[백준] 19238. 스타트 택시 (0) | 2020.07.16 |