結果

問題 No.421 しろくろチョコレート
ユーザー hermione17
提出日時 2016-09-09 23:10:30
言語 Java
(openjdk 23)
結果
AC  
実行時間 236 ms / 2,000 ms
コード長 1,911 bytes
コンパイル時間 3,401 ms
コンパイル使用メモリ 77,228 KB
実行使用メモリ 58,784 KB
最終ジャッジ日時 2024-09-23 07:06:27
合計ジャッジ時間 15,312 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 65
権限があれば一括ダウンロードができます

ソースコード

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();
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0