結果

問題 No.421 しろくろチョコレート
ユーザー uwiuwi
提出日時 2016-09-09 22:44:57
言語 Java21
(openjdk 21)
結果
AC  
実行時間 525 ms / 2,000 ms
コード長 5,012 bytes
コンパイル時間 4,140 ms
コンパイル使用メモリ 84,492 KB
実行使用メモリ 62,780 KB
最終ジャッジ日時 2023-10-24 14:46:01
合計ジャッジ時間 18,692 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
53,336 KB
testcase_01 AC 285 ms
61,680 KB
testcase_02 AC 215 ms
59,680 KB
testcase_03 AC 57 ms
53,328 KB
testcase_04 AC 157 ms
58,832 KB
testcase_05 AC 127 ms
55,732 KB
testcase_06 AC 54 ms
53,328 KB
testcase_07 AC 240 ms
57,476 KB
testcase_08 AC 214 ms
61,640 KB
testcase_09 AC 57 ms
53,336 KB
testcase_10 AC 94 ms
55,004 KB
testcase_11 AC 278 ms
57,720 KB
testcase_12 AC 52 ms
53,328 KB
testcase_13 AC 52 ms
53,328 KB
testcase_14 AC 53 ms
53,336 KB
testcase_15 AC 52 ms
53,316 KB
testcase_16 AC 450 ms
61,676 KB
testcase_17 AC 481 ms
62,780 KB
testcase_18 AC 445 ms
61,824 KB
testcase_19 AC 52 ms
53,328 KB
testcase_20 AC 362 ms
60,196 KB
testcase_21 AC 403 ms
62,024 KB
testcase_22 AC 222 ms
56,976 KB
testcase_23 AC 52 ms
53,340 KB
testcase_24 AC 52 ms
53,340 KB
testcase_25 AC 52 ms
53,340 KB
testcase_26 AC 52 ms
53,336 KB
testcase_27 AC 52 ms
53,336 KB
testcase_28 AC 193 ms
61,424 KB
testcase_29 AC 190 ms
61,652 KB
testcase_30 AC 280 ms
61,640 KB
testcase_31 AC 175 ms
61,368 KB
testcase_32 AC 160 ms
61,296 KB
testcase_33 AC 363 ms
61,852 KB
testcase_34 AC 167 ms
61,336 KB
testcase_35 AC 171 ms
61,392 KB
testcase_36 AC 220 ms
59,240 KB
testcase_37 AC 446 ms
62,400 KB
testcase_38 AC 506 ms
62,664 KB
testcase_39 AC 250 ms
59,324 KB
testcase_40 AC 274 ms
58,012 KB
testcase_41 AC 63 ms
53,892 KB
testcase_42 AC 84 ms
54,472 KB
testcase_43 AC 236 ms
59,680 KB
testcase_44 AC 287 ms
59,540 KB
testcase_45 AC 195 ms
57,220 KB
testcase_46 AC 166 ms
57,064 KB
testcase_47 AC 334 ms
59,432 KB
testcase_48 AC 307 ms
62,436 KB
testcase_49 AC 213 ms
61,640 KB
testcase_50 AC 154 ms
58,524 KB
testcase_51 AC 302 ms
60,264 KB
testcase_52 AC 245 ms
57,148 KB
testcase_53 AC 171 ms
57,088 KB
testcase_54 AC 231 ms
61,724 KB
testcase_55 AC 80 ms
54,392 KB
testcase_56 AC 77 ms
53,932 KB
testcase_57 AC 172 ms
56,932 KB
testcase_58 AC 297 ms
57,312 KB
testcase_59 AC 148 ms
56,796 KB
testcase_60 AC 525 ms
61,676 KB
testcase_61 AC 461 ms
62,544 KB
testcase_62 AC 52 ms
53,328 KB
testcase_63 AC 52 ms
53,340 KB
testcase_64 AC 85 ms
59,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package contest;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class N421 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		int n = ni(), m = ni();
		char[][] map = nm(n,m);
		boolean[][] g = new boolean[n*m][n*m];
		int[] dr = { 1, 0, -1, 0 };
		int[] dc = { 0, 1, 0, -1 };
		for(int i = 0;i < n;i++){
			for(int j = 0;j < m;j++){
				if((i+j) % 2 == 0 && map[i][j] == 'w'){
					for(int k = 0;k < 4;k++){
						int ni = i + dr[k], nj = j + dc[k];
						if(ni >= 0 && ni < n && nj >= 0 && nj < m && map[ni][nj] == 'b'){
							g[i*m+j][ni*m+nj] = true;
						}
					}
				}
			}
		}
		
		int mat = doBipartiteMatchingHK(g);
		int w = 0, b = 0;
		for(int i = 0;i < n;i++){
			for(int j = 0;j < m;j++){
				if(map[i][j] == 'w'){
					w++;
				}
				if(map[i][j] == 'b'){
					b++;
				}
			}
		}
		w -= mat; b -= mat;
		out.println(mat*100+Math.min(w, b)*10 + Math.max(w, b) - Math.min(w, b));
	}
	
	public static int doBipartiteMatchingHK(boolean[][] g)
	{
		int n = g.length;
		if(n == 0)return 0;
		int m = g[0].length;
		int[] from = new int[m];
		int[] to = new int[n];
		Arrays.fill(to, -1);
		Arrays.fill(from, n);
		
		int[] d = new int[n+1];
		int mat = 0;
		while(true){
			Arrays.fill(d, -1);
			int[] q = new int[n];
			int r = 0;
			for(int i = 0;i < n;i++){
				if(to[i] == -1){
					d[i] = 0;
					q[r++] = i;
				}
			}
			
			for(int p = 0;p < r;p++) {
				int cur = q[p];
				for(int adj = 0;adj < m;adj++){
					if(g[cur][adj]){
						int nex = from[adj];
						if(d[nex] == -1) {
							if(nex != n)q[r++] = nex;
							d[nex] = d[cur] + 1;
						}
					}
				}
			}
			if(d[n] == -1)break;
			
			for(int i = 0;i < n;i++){
				if(to[i] == -1){
					if(dfsHK(d, g, n, m, to, from, i))mat++;
				}
			}
		}
		
		return mat;
	}
	
	static boolean dfsHK(int[] d, boolean[][] g, int n, int m, int[] to, int[] from, int cur)
	{
		if(cur == n)return true;
		for(int adj = 0;adj < m;adj++){
			if(g[cur][adj]){
				int nex = from[adj];
				if(d[nex] == d[cur] + 1 && dfsHK(d, g, n, m, to, from, nex)){
					to[cur] = adj;
					from[adj] = cur;
					return true;
				}
			}
		}
		d[cur] = -1;
		return false;
	}	
	void run() throws Exception
	{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//		Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){
//			@Override
//			public void run() {
//				long s = System.currentTimeMillis();
//				solve();
//				out.flush();
//				if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//			}
//		};
//		t.start();
//		t.join();
	}
	
	public static void main(String[] args) throws Exception { new N421().run(); }
	
	private byte[] inbuf = new byte[1024];
	private int lenbuf = 0, ptrbuf = 0;
	
	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
	
	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private long[] nal(int n)
	{
		long[] a = new long[n];
		for(int i = 0;i < n;i++)a[i] = nl();
		return a;
	}
	
	private char[][] nm(int n, int m) {
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private int[][] nmi(int n, int m) {
		int[][] map = new int[n][];
		for(int i = 0;i < n;i++)map[i] = na(m);
		return map;
	}
	
	private int ni() { return (int)nl(); }
	
	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}
0