알고리즘 문제/BOJ
1780번 종이의 개수
parkit
2020. 1. 13. 00:18
728x90
반응형
https://www.acmicpc.net/problem/1780
분할 정복이다.
셈하는 것은 HashMap으로 하였다.
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | import java.io.*; import java.util.*; public class Main { static int n, minus = 0, zero = 0, one = 0; static int m[][]; static HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>(); static boolean chk(int r, int c, int len) { int num = m[r][c]; for(int i=r; i<r+len; i++) { for(int j=c; j<c+len; j++) { if(num != m[i][j]) { return false; } } } return true; } static void simulation(int r, int c, int len) { if(chk(r, c, len)) { hm.put(m[r][c], hm.get(m[r][c]) + 1); return; } for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { int y = r + (len/3)*i; int x = c + (len/3)*j; simulation(y, x, len/3); } } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer st; hm.put(-1, 0); hm.put(0, 0); hm.put(1, 0); n = Integer.parseInt(br.readLine()); m = new int[n+1][n+1]; for(int i=0; i<n; i++) { st = new StringTokenizer(br.readLine()); for(int j=0; j<n; j++) { m[i][j] = Integer.parseInt(st.nextToken()); } } simulation(0, 0, n); for(int i=-1; i<=1; i++) { bw.write(String.valueOf(hm.get(i)) + "\n"); } bw.flush(); bw.close(); } } | cs |
728x90
반응형