結果

問題 No.611 Day of the Mountain
ユーザー uwiuwi
提出日時 2017-12-08 17:28:30
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,193 ms / 2,017 ms
コード長 5,987 bytes
コンパイル時間 3,942 ms
コンパイル使用メモリ 83,584 KB
実行使用メモリ 54,376 KB
最終ジャッジ日時 2024-11-29 19:36:45
合計ジャッジ時間 10,777 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,182 ms
54,076 KB
testcase_01 AC 1,134 ms
54,112 KB
testcase_02 AC 1,159 ms
54,376 KB
testcase_03 AC 1,193 ms
54,220 KB
testcase_04 AC 53 ms
36,716 KB
testcase_05 AC 53 ms
36,764 KB
testcase_06 AC 53 ms
36,464 KB
testcase_07 AC 51 ms
36,816 KB
testcase_08 AC 57 ms
37,024 KB
testcase_09 AC 50 ms
36,764 KB
testcase_10 AC 50 ms
36,460 KB
testcase_11 AC 50 ms
36,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package adv2017;
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 N611 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		int h = ni(), w = ni();
		if(!(4L*h*w <= 1211))throw new AssertionError("4HW<=1211");
		
		char[][] map = nm(h, w);
		for(int i = 0;i < h;i++){
			for(int j = 0;j < w;j++){
				if(!(map[i][j] >= '1' && map[i][j] <= '9' || map[i][j] == '?'))throw new AssertionError("1~9 or ?");
			}
		}
		
		if(w > h){
			map = trans(map);
			int d = h; h = w; w = d;
		}
		
		int[][] ds = makeds(map);
		out.println(ds[h-1][w-1]);
		
		int[][] rds = makeds(rot(rot(map)));
		
		boolean[][] shortest = new boolean[h][w];
		for(int i = 0;i < h;i++){
			for(int j = 0;j < w;j++){
				shortest[i][j] = ds[i][j] + rds[h-1-i][w-1-j] == ds[h-1][w-1] + d1(map[i][j]);
			}
		}

		int mod = 201712111;
		long[] dp = new long[1<<w]; // i bit目 : 現在のセルからさかのぼってi個目のセルは最短経路を実現している
		for(int i = 0;i < h;i++){
			for(int j = 0;j < w;j++){
				if(i == 0 && j == 0){
					if(map[i][j] == '?'){
						dp[1] = 1;
						dp[0] = 8;
					}else{
						dp[1] = 1;
					}
					continue;
				}
				
				long[] ndp = new long[1<<w];
				for(int k = 0;k < 1<<w;k++){
					if((j > 0 && k<<~0<0 && ds[i][j-1] + d1(map[i][j]) == ds[i][j] || 
							i > 0 && k<<~(w-1)<0 && ds[i-1][j] + d1(map[i][j]) == ds[i][j])
							&& shortest[i][j]){ // left or up is shortest and self is shortest
						{
							int nk = k<<1&(1<<w)-1|1;
							ndp[nk] += dp[k];
							if(ndp[nk] >= mod)ndp[nk] -= mod;
						}
						
						if(map[i][j] == '?'){
							int nk = k<<1&(1<<w)-1|0;
							ndp[nk] += dp[k] * 8;
							ndp[nk] %= mod;
						}
					}else{
						int nk = k<<1&(1<<w)-1|0;
						ndp[nk] += dp[k] * (map[i][j] == '?' ? 9 : 1);
						ndp[nk] %= mod;
					}
				}
				dp = ndp;
			}
		}
		long ans = 0;
		for(int i = 1;i < 1<<w;i+=2){
			ans += dp[i];
		}
		out.println(ans % mod);
	}
	
	public static void tf(boolean[]... b)
	{
		for(boolean[] r : b) {
			for(boolean x : r)System.out.print(x?'#':'.');
			System.out.println();
		}
		System.out.println();
	}

	
	int[][] makeds(char[][] map)
	{
		int h = map.length, w = map[0].length;
		int[][] ds = new int[h][w];
		for(int i = 0;i < h;i++){
			for(int j = 0;j < w;j++){
				if(i == 0 && j == 0){
					ds[i][j] = d1(map[i][j]);
				}else{
					int d = 999999999;
					if(i-1 >= 0){
						d = Math.min(d, ds[i-1][j]);
					}
					if(j-1 >= 0){
						d = Math.min(d, ds[i][j-1]);
					}
					ds[i][j] = d + d1(map[i][j]);
				}
			}
		}
		return ds;
	}
	
	int d1(char c)
	{
		if(c == '?')return 1;
		return c-'0';
	}
	
	
	public static char[][] rot(char[][] a)
	{
		int n = a.length, m = a[0].length;
		char[][] b = new char[m][n];
		for(int i = 0;i < n;i++){
			for(int j = 0;j < m;j++){
				b[j][n-1-i] = a[i][j];
			}
		}
		return b;
	}

	
	public static char[][] trans(char[][] a)
	{
		if(a.length == 0)return new char[0][0];
		int n = a.length, m = a[0].length;
		char[][] ret = new char[m][n];
		for(int i = 0;i < m;i++){
			for(int j = 0;j < n;j++){
				ret[i][j] = a[j][i];
			}
		}
		return ret;
	}
	
	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 N611().run(); }
	
	private byte[] inbuf = new byte[1024];
	public 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