[SWEA] Magnetic

문제 보러가기

제한사항

이외의 제한사항은 없다.

첫번째 생각

간단한 시뮬레이션 문제로 S극에 끌리는 것, N극에 끌리는 것, 이미 교착상태 인것을 고려하여

구현해준다.

JAVA Code

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
29
30
31
32
33
34
35
36
37
38
39
import java.util.Scanner;

public class Solution {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int [] dy = {0,1,-1}; //  
		Scanner s = new Scanner(System.in);
		for(int tc=1; tc<=10; tc++) {
			int N = s.nextInt();
			int[][] table = new int[N][N];
			for(int i=0; i<N; i++) {
				for(int j=0; j<N; j++) {
					table[i][j] = s.nextInt();
				}
			}
			int count = 0;
			for(int i=0; i<N; i++) {
				for(int j=0; j<N; j++) {
					if( i+1 < 100 && table[i][j] == 1 ) {
						if( table[i+1][j] == 0) {
						table[i][j] = 0;
						table[i+1][j] = 1; 
						}
						else if( table[i+1][j] == 2) {
							table[i][j] = 3; // 이미  교착상태
							count++;
						}
					}
				}
			}
		
			System.out.println("#"+tc+" "+count);
			
		}
	}

}