結果

問題 No.421 しろくろチョコレート
ユーザー hermione17hermione17
提出日時 2016-09-09 23:10:30
言語 Java21
(openjdk 21)
結果
AC  
実行時間 224 ms / 2,000 ms
コード長 1,911 bytes
コンパイル時間 3,504 ms
コンパイル使用メモリ 77,932 KB
実行使用メモリ 61,876 KB
最終ジャッジ日時 2023-10-24 14:47:01
合計ジャッジ時間 14,141 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
57,544 KB
testcase_01 AC 152 ms
59,852 KB
testcase_02 AC 128 ms
59,376 KB
testcase_03 AC 124 ms
57,528 KB
testcase_04 AC 124 ms
57,504 KB
testcase_05 AC 142 ms
57,636 KB
testcase_06 AC 145 ms
57,692 KB
testcase_07 AC 153 ms
60,320 KB
testcase_08 AC 130 ms
59,456 KB
testcase_09 AC 146 ms
57,628 KB
testcase_10 AC 124 ms
57,476 KB
testcase_11 AC 147 ms
60,048 KB
testcase_12 AC 122 ms
57,392 KB
testcase_13 AC 121 ms
57,340 KB
testcase_14 AC 110 ms
56,156 KB
testcase_15 AC 124 ms
57,364 KB
testcase_16 AC 174 ms
59,500 KB
testcase_17 AC 166 ms
60,868 KB
testcase_18 AC 176 ms
61,476 KB
testcase_19 AC 118 ms
57,508 KB
testcase_20 AC 155 ms
60,272 KB
testcase_21 AC 165 ms
60,728 KB
testcase_22 AC 153 ms
60,360 KB
testcase_23 AC 119 ms
57,404 KB
testcase_24 AC 119 ms
57,392 KB
testcase_25 AC 117 ms
55,180 KB
testcase_26 AC 119 ms
57,232 KB
testcase_27 AC 109 ms
56,272 KB
testcase_28 AC 154 ms
60,584 KB
testcase_29 AC 162 ms
60,636 KB
testcase_30 AC 157 ms
60,364 KB
testcase_31 AC 219 ms
61,808 KB
testcase_32 AC 159 ms
60,732 KB
testcase_33 AC 151 ms
60,732 KB
testcase_34 AC 129 ms
57,608 KB
testcase_35 AC 116 ms
56,252 KB
testcase_36 AC 148 ms
59,744 KB
testcase_37 AC 176 ms
61,464 KB
testcase_38 AC 201 ms
61,416 KB
testcase_39 AC 130 ms
59,488 KB
testcase_40 AC 181 ms
61,440 KB
testcase_41 AC 152 ms
58,260 KB
testcase_42 AC 149 ms
58,332 KB
testcase_43 AC 147 ms
59,692 KB
testcase_44 AC 195 ms
59,592 KB
testcase_45 AC 130 ms
57,612 KB
testcase_46 AC 125 ms
57,520 KB
testcase_47 AC 147 ms
59,632 KB
testcase_48 AC 220 ms
61,876 KB
testcase_49 AC 128 ms
59,500 KB
testcase_50 AC 125 ms
57,480 KB
testcase_51 AC 190 ms
61,736 KB
testcase_52 AC 147 ms
57,700 KB
testcase_53 AC 116 ms
56,508 KB
testcase_54 AC 128 ms
59,560 KB
testcase_55 AC 124 ms
57,204 KB
testcase_56 AC 142 ms
57,576 KB
testcase_57 AC 127 ms
57,608 KB
testcase_58 AC 148 ms
59,988 KB
testcase_59 AC 144 ms
58,312 KB
testcase_60 AC 200 ms
61,720 KB
testcase_61 AC 224 ms
61,752 KB
testcase_62 AC 123 ms
57,384 KB
testcase_63 AC 118 ms
57,272 KB
testcase_64 AC 118 ms
56,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;


public class Y421 {
	int N, M;
	char[][] S;
	int[][] matchRow, matchCol;
	boolean[][] checked;
	
	
	Y421() {
	    Scanner sc = new Scanner(System.in);	
	    N = sc.nextInt();
	    M = sc.nextInt();
	    
	    S = new char[N][];
	    
	    for (int i = 0; i < S.length; i++) {
			S[i] = sc.next().toCharArray();
		}
	    
	    matchRow = new int[N][M];
	    matchCol = new int[N][M];
	   
	    for (int i = 0; i < N; i++) {
	    	for (int j = 0; j < M; j++) {
	    		matchRow[i][j] = -1;
	    		matchCol[i][j] = -1;
	    	}
	    }

	    
	    int cnt = 0;
	    for (int i = 0; i < N; i++) {
	    	for (int j = 0; j < M; j++) {
	    		if (matchCol[i][j] == -1 && S[i][j] != '.') {
	    			checked = new boolean[N][M];
	    			if (search(i, j)) {
	    				cnt++;
	    			}
	    		}
	    	}
	    }
	    
	    int black = 0, white = 0;
	    for (int i = 0; i < N; i++) {
	    	for (int j = 0; j < M; j++) {
	    		if (matchCol[i][j] < 0) {
	    			if (S[i][j] == 'b') black++;
	    			if (S[i][j] == 'w') white++;
	    		}
	    	}
	    }
	    
	    int happiness = cnt * 100 + 10 * Math.min(black,  white) + Math.abs(black - white);
	    
	    System.out.println(happiness);
	}
	
	boolean search(int i, int j) {
	    if (checked[i][j]) {
	    	return false;
	    }
	    
		checked[i][j] = true;
	    
		
		int[] i_array = { i, i-1, i, i+1 };
	    int[] j_array = { j-1, j, j+1, j };
	    
	    for (int i2 : i_array) {
	    	for (int j2 : j_array) {
				if (0 <= i2 && i2 < N && 0 <= j2 && j2 < M) {
					if (S[i2][j2] != '.' && S[i][j] != S[i2][j2]) {
						if (matchCol[i2][j2] < 0 || search(matchRow[i2][j2], matchCol[i2][j2])) {
							matchCol[i][j] = j2;
							matchRow[i][j] = i2;
							matchCol[i2][j2] = j;
							matchRow[i2][j2] = i;
							return true;
						}
					}
				}
	    	}
	    }
	    return false;
	}
	
	public static void main(String argv[]) {
		new Y421();
	}
}
0