結果

問題 No.348 カゴメカゴメ
ユーザー threepipes_sthreepipes_s
提出日時 2016-02-27 01:49:11
言語 Java21
(openjdk 21)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 5,640 bytes
コンパイル時間 3,797 ms
コンパイル使用メモリ 95,872 KB
実行使用メモリ 96,068 KB
最終ジャッジ日時 2024-09-24 10:59:28
合計ジャッジ時間 24,995 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 807 ms
66,740 KB
testcase_01 AC 74 ms
50,416 KB
testcase_02 AC 1,164 ms
96,068 KB
testcase_03 AC 830 ms
65,120 KB
testcase_04 AC 65 ms
50,924 KB
testcase_05 AC 90 ms
51,408 KB
testcase_06 AC 56 ms
50,276 KB
testcase_07 AC 56 ms
50,620 KB
testcase_08 AC 56 ms
50,476 KB
testcase_09 AC 58 ms
50,684 KB
testcase_10 AC 56 ms
50,492 KB
testcase_11 AC 59 ms
50,692 KB
testcase_12 AC 193 ms
58,472 KB
testcase_13 AC 154 ms
53,748 KB
testcase_14 AC 81 ms
51,496 KB
testcase_15 AC 762 ms
92,248 KB
testcase_16 AC 57 ms
50,736 KB
testcase_17 AC 56 ms
50,660 KB
testcase_18 AC 57 ms
50,332 KB
testcase_19 AC 56 ms
50,328 KB
testcase_20 AC 64 ms
50,860 KB
testcase_21 AC 59 ms
50,724 KB
testcase_22 AC 57 ms
50,400 KB
testcase_23 WA -
testcase_24 AC 921 ms
71,888 KB
testcase_25 AC 842 ms
71,232 KB
testcase_26 AC 895 ms
72,968 KB
testcase_27 AC 896 ms
70,828 KB
testcase_28 AC 870 ms
69,176 KB
testcase_29 AC 888 ms
70,644 KB
testcase_30 AC 875 ms
73,160 KB
testcase_31 AC 969 ms
71,568 KB
testcase_32 AC 908 ms
71,948 KB
testcase_33 AC 260 ms
57,756 KB
testcase_34 AC 266 ms
57,932 KB
testcase_35 AC 265 ms
57,836 KB
testcase_36 AC 261 ms
57,992 KB
testcase_37 AC 263 ms
57,860 KB
testcase_38 AC 260 ms
58,036 KB
testcase_39 AC 241 ms
57,684 KB
testcase_40 AC 267 ms
57,732 KB
testcase_41 WA -
testcase_42 AC 267 ms
57,904 KB
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 AC 157 ms
55,628 KB
testcase_47 AC 159 ms
54,664 KB
testcase_48 WA -
testcase_49 WA -
testcase_50 AC 160 ms
55,736 KB
testcase_51 WA -
testcase_52 WA -
権限があれば一括ダウンロードができます

ソースコード

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*m);
		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*m+j);
					allx++;
				}
			}
		}
		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