문제 링크 : https://www.acmicpc.net/problem/21610
문제에서 하라는 대로 구현하면 된다.
import java.io.*;
import java.util.*;
public class baekjoon_21610{
public static int N,M;
public static int board[][];
public static int dir[][] = {{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1}};
public static class Node{
int y;
int x;
public Node(){}
public Node(int a,int b){
y = a;
x = b;
}
}
public static boolean isBoundry(int y,int x){
if(y<0 || x<0 || y>=N || x>=N) return false;
return true;
}
public static void move(int direction, int cnt, ArrayList<Node> cloud){
ArrayList<Node> nextCloud = new ArrayList<Node>();
cnt %= N;
boolean visited[][] = new boolean[N][N]; //이전 구름있던 곳 표시
for(int i=0;i<cloud.size();i++){
Node c = cloud.get(i);
int ny = c.y + dir[direction-1][0]*cnt;
int nx = c.x + dir[direction-1][1]*cnt;
if(ny<0) ny += N;
if(nx<0) nx += N;
if(ny>=N) ny %= N;
if(nx>=N) nx %=N;
visited[ny][nx] = true;
board[ny][nx] +=1;
}
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(visited[i][j]){
int aroundCount = 0;
for(int k=1;k<8;k+=2){
int ny = i + dir[k][0];
int nx = j + dir[k][1];
if(!isBoundry(ny,nx))continue;
if(board[ny][nx] > 0){
aroundCount++;
}
}
board[i][j] += aroundCount;
}
}
}
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(visited[i][j] || board[i][j] <2) continue;
board[i][j] -= 2;
nextCloud.add(new Node(i,j));
}
}
cloud.clear();
for(int i=0;i<nextCloud.size();i++){
cloud.add(nextCloud.get(i));
}
}
public static void main(String[] args)throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
board = new int [N][N];
for(int i=0;i<N;i++){
st = new StringTokenizer(br.readLine());
for(int j=0;j<N;j++){
board[i][j] = Integer.parseInt(st.nextToken());
}
}
ArrayList<Node> cloud = new ArrayList<Node>();
cloud.add(new Node(N-2,0));
cloud.add(new Node(N-1,0));
cloud.add(new Node(N-2,1));
cloud.add(new Node(N-1,1));
for(int i=0;i<M;i++){
st = new StringTokenizer(br.readLine());
int d = Integer.parseInt(st.nextToken());
int cnt = Integer.parseInt(st.nextToken());
move(d,cnt,cloud);
}
int ans =0;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
ans += board[i][j];
}
}
System.out.println(ans);
}
}
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 13911 집 구하기 [JAVA] (0) | 2021.07.13 |
---|---|
[백준] 2479 경로 찾기 [JAVA] (0) | 2021.07.13 |
[백준] 18223 민준이와마산그리고건우 [JAVA] (0) | 2021.07.13 |
[백준] 1613 역사 [JAVA] (0) | 2021.06.23 |
[백준] 11562 백양로 브레이크 [JAVA] (0) | 2021.06.23 |