結果

問題 No.348 カゴメカゴメ
ユーザー threepipes_sthreepipes_s
提出日時 2016-02-27 07:30:27
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,077 ms / 2,000 ms
コード長 5,676 bytes
コンパイル時間 3,069 ms
コンパイル使用メモリ 84,300 KB
実行使用メモリ 100,504 KB
最終ジャッジ日時 2023-10-24 18:23:32
合計ジャッジ時間 24,225 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 939 ms
80,212 KB
testcase_01 AC 71 ms
54,384 KB
testcase_02 AC 1,062 ms
100,504 KB
testcase_03 AC 777 ms
67,440 KB
testcase_04 AC 86 ms
53,836 KB
testcase_05 AC 95 ms
55,148 KB
testcase_06 AC 56 ms
53,812 KB
testcase_07 AC 58 ms
51,744 KB
testcase_08 AC 58 ms
53,796 KB
testcase_09 AC 59 ms
53,808 KB
testcase_10 AC 59 ms
53,804 KB
testcase_11 AC 57 ms
53,800 KB
testcase_12 AC 190 ms
62,068 KB
testcase_13 AC 150 ms
57,136 KB
testcase_14 AC 84 ms
53,940 KB
testcase_15 AC 718 ms
94,028 KB
testcase_16 AC 56 ms
53,784 KB
testcase_17 AC 55 ms
53,780 KB
testcase_18 AC 55 ms
53,784 KB
testcase_19 AC 55 ms
53,804 KB
testcase_20 AC 59 ms
53,824 KB
testcase_21 AC 57 ms
53,788 KB
testcase_22 AC 56 ms
53,784 KB
testcase_23 AC 877 ms
74,188 KB
testcase_24 AC 1,021 ms
76,092 KB
testcase_25 AC 944 ms
74,756 KB
testcase_26 AC 804 ms
73,532 KB
testcase_27 AC 821 ms
75,292 KB
testcase_28 AC 1,077 ms
75,028 KB
testcase_29 AC 888 ms
74,328 KB
testcase_30 AC 854 ms
75,496 KB
testcase_31 AC 905 ms
74,608 KB
testcase_32 AC 1,010 ms
75,568 KB
testcase_33 AC 265 ms
60,580 KB
testcase_34 AC 258 ms
61,472 KB
testcase_35 AC 257 ms
60,676 KB
testcase_36 AC 244 ms
61,152 KB
testcase_37 AC 247 ms
60,720 KB
testcase_38 AC 261 ms
61,388 KB
testcase_39 AC 263 ms
61,028 KB
testcase_40 AC 252 ms
60,916 KB
testcase_41 AC 268 ms
61,196 KB
testcase_42 AC 251 ms
61,016 KB
testcase_43 AC 156 ms
59,424 KB
testcase_44 AC 157 ms
58,356 KB
testcase_45 AC 153 ms
59,724 KB
testcase_46 AC 144 ms
57,576 KB
testcase_47 AC 153 ms
56,904 KB
testcase_48 AC 148 ms
56,956 KB
testcase_49 AC 153 ms
59,784 KB
testcase_50 AC 145 ms
57,140 KB
testcase_51 AC 158 ms
60,108 KB
testcase_52 AC 159 ms
59,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.HashMap;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
 
public class Main {
	public static void main(String[] args) throws NumberFormatException,
	IOException {Solve solve = new Solve();solve.solve();}
}
class Solve{
	void dump(int[]a){for(int i=0;i<a.length;i++)System.out.print(a[i]+" ");System.out.println();}
	void dump(int[]a,int n){for(int i=0;i<a.length;i++)System.out.printf("%"+n+"d",a[i]);System.out.println();}
	void dump(long[]a){for(int i=0;i<a.length;i++)System.out.print(a[i]+" ");System.out.println();}
	void dump(char[]a){for(int i=0;i<a.length;i++)System.out.print(a[i]);System.out.println();}
	String itob(int a,int l){return String.format("%"+l+"s",Integer.toBinaryString(a)).replace(' ','0');}
	int n, m;
	void solve() throws NumberFormatException, IOException{
		final ContestScanner in = new ContestScanner();
		Writer out = new Writer();
		n = in.nextInt();
		m = in.nextInt();
		BitSet map = new BitSet((n+2)*(m+2));
		int allx = 0;
		for(int i=0; i<n; i++){
			char[] line = in.nextToken().toCharArray();
			for(int j=0; j<m; j++){
				if(line[j]=='x'){
					map.set((i+1)*(m+2)+(j+1));
					allx++;
				}
			}
		}
		n+=2;
		m+=2;
		List<Node> node = new ArrayList<>();
		final int[] dx = {1, 1, 1, 0, 0, -1, -1, -1};
		final int[] dy = {1, 0, -1, 1, -1, 1, -1, 0};
		final int[] dx4 = {1,0,-1,0};
		final int[] dy4 = {0,1,0,-1};
		BitSet used = new BitSet();
		Stack<Long> oq = new Stack<>();
		Stack<Integer> qu = new Stack<>();
		for(int i=0; i<n; i++) oq.add((long)i*m);
		int mask = (1<<30)-1;
		int id = 0;
		node.add(new Node(0, 0));
		while(!oq.isEmpty()){
			long ot = oq.pop();
			final int p = (int)ot&mask;
			ot >>= 30;
			if(used.get(p)) continue;
			used.set(p);
			final int y = p/m;
			final int x = p%m;
			if(!map.get(p)){
				for(int i=0; i<4; i++){
					final int ny = y+dy4[i];
					final int nx = x+dx4[i];
					final int np = ny*m+nx;
					if(out(ny, nx) || used.get(np)) continue;
					oq.add(np|ot<<30);
				}
			}else{
				id++;
				int left = m;
				int ly = 0;
				int size = 0;
				qu.clear();
				qu.add(p);
				while(!qu.isEmpty()){
					final int ip = qu.pop();
					size++;
					final int iy = ip/m;
					final int ix = ip%m;
					if(ix<left && !map.get(iy*m+ix+1)){
						left = ix;
						ly = iy;
					}
					for(int i=0; i<8; i++){
						final int ny = iy+dy[i];
						final int nx = ix+dx[i];
						final int np = ny*m+nx;
						if(out(ny, nx) || !map.get(np) || used.get(np))
							continue;
						used.set(np);
						qu.add(np);
					}
				}
				// p <- node
				if(size>3) oq.add(ly*m+left+1 | (long)id<<30);
				Node newnode = new Node(id, size);
				Node parent = node.get((int)ot);
				node.add(newnode);
				newnode.append(parent);
				parent.append(newnode);
			}
		}
		dp = new int[id+1][2];
		int x1 = dfs(node.get(0), -1, true);
		System.out.println(Math.max(x1, allx-x1));
	}
	int[][] dp;
	int dfs(Node v, int par, boolean f){
		if(dp[v.id][f?1:0]>0) return dp[v.id][f?1:0];
		int sumT = 0;
		int sumF = 0;
		for(Node nv: v.edge){
			if(nv.id == par) continue;
			if(f){
				// not use
				sumF += dfs(nv, v.id, false);
			}else{
				// use
				sumT += dfs(nv, v.id, true);
				// not use
				sumF += dfs(nv, v.id, false);
			}
		}
		int ans = f ? sumF : Math.max(sumF, sumT + v.size);
		return dp[v.id][f?1:0] = ans;
	}
	
	boolean out(int y, int x){
		return y<0||y>=n||x<0||x>=m;
	}
}

class Node{
	int id;
	int size;
	List<Node> edge = new ArrayList<>();
	Node(int id, int s){
		this.id = id;
		size = s;
	}
	void append(Node v){
		edge.add(v);
	}
}

class MultiSet<T> extends HashMap<T, Integer>{
	@Override
	public Integer get(Object key){return containsKey(key)?super.get(key):0;}
	public void add(T key,int v){put(key,get(key)+v);}
	public void add(T key){put(key,get(key)+1);}
	public void sub(T key)
	{final int v=get(key);if(v==1)remove(key);else put(key,v-1);}
}
class Timer{
	long time;
	public void set(){time=System.currentTimeMillis();}
	public long stop(){return time=System.currentTimeMillis()-time;}
	public void print()
	{System.out.println("Time: "+(System.currentTimeMillis()-time)+"ms");}
	@Override public String toString(){return"Time: "+time+"ms";}
}
class Writer extends PrintWriter{
	public Writer(String filename)throws IOException
	{super(new BufferedWriter(new FileWriter(filename)));}
	public Writer()throws IOException{super(System.out);}
}
class ContestScanner {
	private InputStreamReader in;private int c=-2;
	public ContestScanner()throws IOException 
	{in=new InputStreamReader(System.in);}
	public ContestScanner(String filename)throws IOException
	{in=new InputStreamReader(new FileInputStream(filename));}
	public String nextToken()throws IOException {
		StringBuilder sb=new StringBuilder();
		while((c=in.read())!=-1&&Character.isWhitespace(c));
		while(c!=-1&&!Character.isWhitespace(c)){sb.append((char)c);c=in.read();}
		return sb.toString();
	}
	public String readLine()throws IOException{
		StringBuilder sb=new StringBuilder();if(c==-2)c=in.read();
		while(c!=-1&&c!='\n'&&c!='\r'){sb.append((char)c);c=in.read();}
		return sb.toString();
	}
	public long nextLong()throws IOException,NumberFormatException
	{return Long.parseLong(nextToken());}
	public int nextInt()throws NumberFormatException,IOException
	{return(int)nextLong();}
	public double nextDouble()throws NumberFormatException,IOException 
	{return Double.parseDouble(nextToken());}
}
0