結果

問題 No.421 しろくろチョコレート
ユーザー 37zigen37zigen
提出日時 2016-09-09 03:03:10
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 6,021 bytes
コンパイル時間 4,516 ms
コンパイル使用メモリ 83,856 KB
実行使用メモリ 52,472 KB
最終ジャッジ日時 2024-04-28 00:55:53
合計ジャッジ時間 8,483 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
50,188 KB
testcase_01 WA -
testcase_02 AC 50 ms
50,360 KB
testcase_03 WA -
testcase_04 AC 50 ms
50,388 KB
testcase_05 WA -
testcase_06 AC 47 ms
50,196 KB
testcase_07 WA -
testcase_08 AC 49 ms
50,164 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 47 ms
50,048 KB
testcase_13 AC 49 ms
49,928 KB
testcase_14 AC 48 ms
50,296 KB
testcase_15 AC 49 ms
49,960 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 47 ms
50,316 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 48 ms
50,240 KB
testcase_24 AC 47 ms
50,416 KB
testcase_25 AC 53 ms
50,228 KB
testcase_26 AC 49 ms
50,112 KB
testcase_27 AC 50 ms
49,956 KB
testcase_28 AC 52 ms
50,124 KB
testcase_29 AC 50 ms
50,388 KB
testcase_30 WA -
testcase_31 AC 53 ms
50,304 KB
testcase_32 AC 53 ms
50,388 KB
testcase_33 WA -
testcase_34 AC 50 ms
50,192 KB
testcase_35 AC 50 ms
50,224 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 AC 50 ms
50,332 KB
testcase_41 AC 48 ms
50,340 KB
testcase_42 WA -
testcase_43 WA -
testcase_44 AC 51 ms
50,072 KB
testcase_45 WA -
testcase_46 AC 48 ms
50,196 KB
testcase_47 WA -
testcase_48 AC 51 ms
50,076 KB
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 50 ms
50,156 KB
testcase_52 WA -
testcase_53 AC 47 ms
50,168 KB
testcase_54 AC 51 ms
50,080 KB
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 AC 48 ms
50,128 KB
testcase_63 AC 48 ms
50,176 KB
testcase_64 AC 52 ms
50,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package No400番台;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;

public class Q421_2 {

	int[] dx = { 1, -1, 0, 0 };
	int[] dy = { 0, 0, 1, -1 };
	int h, w;
	char[][] choco;
	int[][] comp;

	@SuppressWarnings("unchecked")
	void solver() {
		h = ni();
		w = ni();
		choco = new char[h][w];
		for (int i = 0; i < h; i++) {
			choco[i] = ns().toCharArray();
		}

		comp = new int[h][w];
		for (int i = 0; i < h; i++) {
			Arrays.fill(comp[i], -1);
		}

		int color = 0;
		int ans = 0;
		int odd = 0, even = 0;

		for (int y = 0; y < h; y++) {
			for (int x = 0; x < w; x++) {
				if (on_filed(x, y) && comp[y][x] == -1) {
					int tmp_odd = 0, tmp_even = 0;
					ArrayDeque<Pair> que = new ArrayDeque<>();
					que.add(new Pair(x, y));
					while (!que.isEmpty()) {
						Pair p = que.poll();
						if (comp[p.y][p.x] != -1)
							continue;
						comp[p.y][p.x] = color;

						if ((p.x + p.y) % 2 == 0)
							tmp_even++;
						else
							tmp_odd++;

						for (int i = 0; i < 4; i++) {
							int nx = p.x + dx[i];
							int ny = p.y + dy[i];
							if (on_filed(nx, ny) && comp[ny][nx] == -1) {
								que.add(new Pair(nx, ny));
							}
						}
					}
					int common = Math.min(tmp_odd, tmp_even);
					ans += common * 100;
					tmp_odd -= common;
					tmp_even -= common;
					odd += tmp_odd;
					even += tmp_even;
					color++;
				}
			}
		}
		// int the_number_of_matchings = bipartiteMatching(g, (h * w) / 2);
		// ans += the_number_of_matchings * 100;
		// even -= the_number_of_matchings;
		// odd -= the_number_of_matchings;

		// show_comp();

		int common = Math.min(even, odd);
		ans += common * 10;
		even -= common;
		odd -= common;
		ans += even + odd;
		System.out.println(ans);
	}

	int id(int x, int y) {
		if ((y + x) % 2 == 0)
			return (y * w + x) / 2;
		else
			return (y * w + x) / 2 + (h * w - 1) / 2 + 1;
	}

	boolean on_filed(int x, int y) {
		if (0 <= x && x < w && 0 <= y && y < h && choco[y][x] != '.') {
			return true;
		} else
			return false;
	}

	class Pair {
		int x;
		int y;

		Pair(int x, int y) {
			this.x = x;
			this.y = y;
		}
	}

	boolean augment(ArrayList<Integer>[] g, int u, Integer[] matchTo, Boolean[] visited) {
		if (u < 0)
			return true;
		for (int v : g[u]) {
			if (!visited[v]) {
				visited[v] = true;
				if (augment(g, matchTo[v], matchTo, visited)) {
					matchTo[u] = v;
					matchTo[v] = u;
					return true;
				}
			}
		}
		return false;
	}

	int bipartiteMatching(ArrayList<Integer>[] g, int L) {
		final int n = g.length;
		Integer[] matchTo = new Integer[n];
		Arrays.fill(matchTo, -1);
		matching = new ArrayList<>();
		int match = 0;

		for (int i = 0; i < L; i++) {
			Boolean[] visited = new Boolean[n];
			Arrays.fill(visited, false);
			if (augment(g, i, matchTo, visited))
				match++;
		}
		for (int i = 0; i < L; i++) {
			if (matchTo[i] > 0)
				matching.add(new Edge(i, matchTo[i]));
		}

		return match;
	}

	ArrayList<Edge> matching;

	class Edge {
		int a;
		int b;

		Edge(int a, int b) {
			this.a = a;
			this.b = b;
		}
	}

	void show_comp() {
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				System.out.print((comp[i][j] == -1 ? "#" : comp[i][j]) + " ");
			}
			System.out.println();
		}
	}

	static InputStream is;
	static PrintWriter out;
	static String INPUT = "";

	public static void main(String[] args) throws Exception {
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);

		new Q421_2().solver();
		out.flush();
	}

	static long nl() {
		try {
			long num = 0;
			boolean minus = false;
			while ((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'))
				;
			if (num == '-') {
				num = 0;
				minus = true;
			} else {
				num -= '0';
			}

			while (true) {
				int b = is.read();
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
			}
		} catch (IOException e) {
		}
		return -1;
	}

	static char nc() {
		try {
			int b = skip();
			if (b == -1)
				return 0;
			return (char) b;
		} catch (IOException e) {
		}
		return 0;
	}

	static double nd() {
		try {
			return Double.parseDouble(ns());
		} catch (Exception e) {
		}
		return 0;
	}

	static String ns() {
		try {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			if (b == -1)
				return "";
			sb.append((char) b);
			while (true) {
				b = is.read();
				if (b == -1)
					return sb.toString();
				if (b <= ' ')
					return sb.toString();
				sb.append((char) b);
			}
		} catch (IOException e) {
		}
		return "";
	}

	public static char[] ns(int n) {
		char[] buf = new char[n];
		try {
			int b = skip(), p = 0;
			if (b == -1)
				return null;
			buf[p++] = (char) b;
			while (p < n) {
				b = is.read();
				if (b == -1 || b <= ' ')
					break;
				buf[p++] = (char) b;
			}
			return Arrays.copyOf(buf, p);
		} catch (IOException e) {
		}
		return null;
	}

	public static byte[] nse(int n) {
		byte[] buf = new byte[n];
		try {
			int b = skip();
			if (b == -1)
				return null;
			is.read(buf);
			return buf;
		} catch (IOException e) {
		}
		return null;
	}

	static int skip() throws IOException {
		int b;
		while ((b = is.read()) != -1 && !(b >= 33 && b <= 126))
			;
		return b;
	}

	static boolean eof() {
		try {
			is.mark(1000);
			int b = skip();
			is.reset();
			return b == -1;
		} catch (IOException e) {
			return true;
		}
	}

	static int ni() {
		try {
			int num = 0;
			boolean minus = false;
			while ((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'))
				;
			if (num == '-') {
				num = 0;
				minus = true;
			} else {
				num -= '0';
			}

			while (true) {
				int b = is.read();
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
			}
		} catch (IOException e) {
		}
		return -1;
	}

}
0