Submission #60848


Source Code Expand

import java.util.Arrays;

public class Main{

	int h, w, N, INF = 1<<25;
	char[][] m;
	int[][] d = {{-1,0},{0,1},{1,0},{0,-1}};
	char[] s;
	int[][][][] next, dp;
	
	int get(int i, int j, int k, int now){
		if(now==N)return 0;
		if(dp[i][j][k][now]!=-1)return dp[i][j][k][now];
		int res = INF;
		for(int dir=0;dir<4;dir++){
			if(dir==k)continue;
			int c = next[i][j][dir][s[now]-'a'];
			if(c!=INF)res = Math.min(res, c+get(i+c*d[dir][0], j+c*d[dir][1], dir, now+1));
		}
		return dp[i][j][k][now] = res;
	}
	
	void run() {
		Scanner sc = new Scanner();
		h = sc.nextInt(); w = sc.nextInt(); N = sc.nextInt();
		s = sc.next().toCharArray();
		m = new char[h][];
		for(int i=0;i<h;i++)m[i]=sc.next().toCharArray();
		next = new int[h][w][4][26];
		for(int i=0;i<h;i++)for(int j=0;j<w;j++)for(int k=0;k<4;k++)for(int c=0;c<26;c++){
			next[i][j][k][c] = INF;
			int pi = i+d[k][0], pj = j+d[k][1];
			for(;;){
				if(pi<0||h<=pi||pj<0||w<=pj)break;
				if(m[pi][pj]-'a'==c){
					next[i][j][k][c] = Math.abs(i-pi)+Math.abs(j-pj);break;
				}
				pi+=d[k][0]; pj+=d[k][1];
			}
		}
		dp = new int[h][w][4][N];
		for(int i=0;i<h;i++)for(int j=0;j<w;j++)for(int k=0;k<4;k++)for(int l=0;l<N;l++)dp[i][j][k][l]=-1;
		int res = INF;
		for(int i=0;i<h;i++)for(int j=0;j<w;j++)for(int k=0;k<4;k++)res=Math.min(res, get(i, j, k, 0));
		System.out.println(res==INF?-1:res);
	}

	void debug(Object... o) {
		System.out.println(Arrays.deepToString(o));
	}

	class Scanner {
		int nextInt() {
			try {
				int c = System.in.read();
				while (c != '-' && (c < '0' || '9' < c))
					c = System.in.read();
				if (c == '-') return -nextInt();
				int res = 0;
				do {
					res *= 10;
					res += c - '0';
					c = System.in.read();
				} while ('0' <= c && c <= '9');
				return res;
			} catch (Exception e) {
				return -1;
			}
		}
		long nextLong() {
			try {
				int c = System.in.read();
				while (c != '-' && (c < '0' || '9' < c))
					c = System.in.read();
				if (c == '-') return -nextLong();
				long res = 0;
				do {
					res *= 10;
					res += c - '0';
					c = System.in.read();
				} while ('0' <= c && c <= '9');
				return res;
			} catch (Exception e) {
				return -1;
			}
		}
		double nextDouble() {
			return Double.parseDouble(next());
		}
		String next() {
			try {
				StringBuilder res = new StringBuilder("");
				int c = System.in.read();
				while (Character.isWhitespace(c))
					c = System.in.read();
				do {
					res.append((char) c);
				} while (!Character.isWhitespace(c = System.in.read()));
				return res.toString();
			} catch (Exception e) {
				return null;
			}
		}
		String nextLine(){
			try{
				StringBuilder res =new StringBuilder("");
				int c = System.in.read();
				while (c=='\r' || c=='\n')
					c = System.in.read();
				do {
					res.append((char) c);
					c = System.in.read();
				} while (c!='\r' && c!='\n');
				return res.toString();
			}catch (Exception e) {
				return null;
			}
		}
	}
	
	public static void main(String... args) {
		new Main().run();
	}
}

Submission Info

Submission Time
Task F - 僕は宇宙人
User nanikaka
Language Java (OpenJDK 1.7.0)
Score 100
Code Size 3130 Byte
Status AC
Exec Time 1263 ms
Memory 53568 KB

Judge Result

Set Name All
Score / Max Score 100 / 100
Status
AC × 55
Set Name Test Cases
All case_000.txt, case_001.txt, case_002.txt, case_003.txt, case_004.txt, case_005.txt, case_006.txt, case_007.txt, case_008.txt, case_009.txt, case_010.txt, case_011.txt, case_012.txt, case_013.txt, case_014.txt, case_015.txt, case_016.txt, case_017.txt, case_018.txt, case_019.txt, case_020.txt, case_021.txt, case_022.txt, case_023.txt, case_024.txt, case_025.txt, case_026.txt, case_027.txt, case_028.txt, case_029.txt, case_030.txt, case_031.txt, case_032.txt, case_033.txt, case_034.txt, case_035.txt, case_036.txt, case_037.txt, case_038.txt, case_039.txt, case_040.txt, case_041.txt, case_042.txt, case_043.txt, case_044.txt, case_045.txt, case_046.txt, case_047.txt, case_048.txt, case_049.txt, case_050.txt, case_051.txt, case_052.txt, case_053.txt, case_054.txt
Case Name Status Exec Time Memory
case_000.txt AC 412 ms 18460 KB
case_001.txt AC 398 ms 18480 KB
case_002.txt AC 424 ms 18492 KB
case_003.txt AC 396 ms 18504 KB
case_004.txt AC 390 ms 18484 KB
case_005.txt AC 396 ms 18532 KB
case_006.txt AC 404 ms 18480 KB
case_007.txt AC 404 ms 18484 KB
case_008.txt AC 409 ms 19244 KB
case_009.txt AC 427 ms 21896 KB
case_010.txt AC 423 ms 20268 KB
case_011.txt AC 437 ms 21896 KB
case_012.txt AC 408 ms 18476 KB
case_013.txt AC 748 ms 53568 KB
case_014.txt AC 823 ms 52560 KB
case_015.txt AC 607 ms 36980 KB
case_016.txt AC 526 ms 27096 KB
case_017.txt AC 540 ms 28684 KB
case_018.txt AC 502 ms 25936 KB
case_019.txt AC 549 ms 37008 KB
case_020.txt AC 470 ms 24532 KB
case_021.txt AC 476 ms 24372 KB
case_022.txt AC 519 ms 25160 KB
case_023.txt AC 1263 ms 25820 KB
case_024.txt AC 554 ms 28980 KB
case_025.txt AC 430 ms 22044 KB
case_026.txt AC 504 ms 25360 KB
case_027.txt AC 592 ms 24392 KB
case_028.txt AC 470 ms 24344 KB
case_029.txt AC 516 ms 25688 KB
case_030.txt AC 621 ms 36880 KB
case_031.txt AC 552 ms 37080 KB
case_032.txt AC 503 ms 26252 KB
case_033.txt AC 510 ms 25232 KB
case_034.txt AC 441 ms 22596 KB
case_035.txt AC 582 ms 36956 KB
case_036.txt AC 455 ms 22416 KB
case_037.txt AC 522 ms 26836 KB
case_038.txt AC 506 ms 24680 KB
case_039.txt AC 589 ms 28864 KB
case_040.txt AC 630 ms 44952 KB
case_041.txt AC 530 ms 28992 KB
case_042.txt AC 464 ms 23016 KB
case_043.txt AC 490 ms 25012 KB
case_044.txt AC 419 ms 20384 KB
case_045.txt AC 479 ms 24092 KB
case_046.txt AC 460 ms 23280 KB
case_047.txt AC 566 ms 36976 KB
case_048.txt AC 524 ms 27548 KB
case_049.txt AC 447 ms 22360 KB
case_050.txt AC 529 ms 28916 KB
case_051.txt AC 478 ms 24360 KB
case_052.txt AC 561 ms 37012 KB
case_053.txt AC 583 ms 26512 KB
case_054.txt AC 473 ms 23640 KB