문제 : https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14hwZqABsCFAYD
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Solution { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int[][] arr = new int[100][100]; for(int test =0; test<10; test++) { int n = Integer.parseInt(br.readLine()); // 한 변의 길이 // 테이블 저장 for(int i=0; i<100; i++) { StringTokenizer st = new StringTokenizer(br.readLine()); for(int j=0; j<100; j++) { // 1 = N극, 2 = S극 arr[i][j] = Integer.parseInt(st.nextToken()); } } int sum = 0; // 세로줄 하나씩 탐색 for(int i=0; i<100; i++) { int last = 0; // 최근 마지막 자성체 for(int j=0; j<100; j++) { // N극 이면 if(arr[j][i] == 1) { last = 1; } // S극 이면 else if(arr[j][i] == 2) { // 최근 마지막 자성체가 N극 이면 if(last == 1) { sum++; } last = 2; } } } System.out.println("#" + (test+1) + " " + sum); } br.close(); } }
N극은 무조건 밑(S)으로 붙고, S극은 무조건 위(N)로 붙으므로
교착상태에 빠지는 경우는
세로로 보았을 때 N극 다음에 나오는 자성체가 S극이 나오는 경우이다.
100 * 100 배열에 N극, S극 정보를 각각 저장해준다.
세로로 한줄씩 전체순회하여
N극이 나오고 그 다음에 나온 자성체가 S극인 경우일 때마다 결과값을 늘려준다.
* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.