結果
問題 | No.340 雪の足跡 |
ユーザー | atn112323 |
提出日時 | 2016-02-06 11:47:29 |
言語 | Java21 (openjdk 21) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 13,397 bytes |
コンパイル時間 | 3,084 ms |
コンパイル使用メモリ | 93,376 KB |
実行使用メモリ | 127,852 KB |
最終ジャッジ日時 | 2024-09-21 20:48:06 |
合計ジャッジ時間 | 20,382 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 58 ms
50,168 KB |
testcase_01 | AC | 59 ms
50,580 KB |
testcase_02 | AC | 59 ms
50,444 KB |
testcase_03 | AC | 59 ms
50,612 KB |
testcase_04 | AC | 59 ms
50,152 KB |
testcase_05 | AC | 59 ms
50,360 KB |
testcase_06 | AC | 60 ms
50,212 KB |
testcase_07 | AC | 60 ms
50,748 KB |
testcase_08 | AC | 59 ms
50,288 KB |
testcase_09 | AC | 59 ms
50,592 KB |
testcase_10 | AC | 62 ms
50,228 KB |
testcase_11 | AC | 104 ms
51,968 KB |
testcase_12 | AC | 81 ms
51,216 KB |
testcase_13 | AC | 92 ms
51,716 KB |
testcase_14 | AC | 224 ms
58,448 KB |
testcase_15 | AC | 297 ms
58,800 KB |
testcase_16 | AC | 200 ms
56,572 KB |
testcase_17 | AC | 299 ms
58,812 KB |
testcase_18 | AC | 294 ms
59,312 KB |
testcase_19 | AC | 280 ms
58,596 KB |
testcase_20 | AC | 911 ms
119,180 KB |
testcase_21 | AC | 491 ms
61,720 KB |
testcase_22 | AC | 215 ms
88,756 KB |
testcase_23 | AC | 991 ms
123,092 KB |
testcase_24 | AC | 852 ms
98,572 KB |
testcase_25 | AC | 859 ms
112,000 KB |
testcase_26 | AC | 268 ms
57,328 KB |
testcase_27 | AC | 145 ms
54,292 KB |
testcase_28 | AC | 314 ms
59,476 KB |
testcase_29 | AC | 948 ms
100,672 KB |
testcase_30 | AC | 718 ms
88,672 KB |
testcase_31 | TLE | - |
testcase_32 | TLE | - |
testcase_33 | TLE | - |
testcase_34 | TLE | - |
testcase_35 | TLE | - |
testcase_36 | AC | 993 ms
119,440 KB |
ソースコード
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.Queue; public class Main { /** * This class represents a problem setting. */ class Problem implements IProblem { int[] WHN; int[][] B; @Override public void set(ProblemReader reader) throws IOException { WHN = reader.nextIntArray(); B = new int[WHN[2]][]; for (int i = 0; i < WHN[2]; i++) { reader.nextInt(); B[i] = reader.nextIntArray(); } } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(StringUtils.toString(WHN) + "\n"); for (int i = 0; i < WHN[2]; i++) { sb.append(StringUtils.toString(B[i].length) + "\n"); sb.append(StringUtils.toString(B[i]) + "\n"); } sb.setLength(sb.length() - 1); return sb.toString(); } } /** * Solver for the problem. * It's the most important part of codes in this file. */ class Solver implements IProblemSolver<Problem> { int[] DX = new int[]{1, 0, -1, 0}; int[] DY = new int[]{0, 1, 0, -1}; @Override public String solve(Problem problem) { int W = problem.WHN[0], H = problem.WHN[1]; int dist = solve(W, H, problem.B); if (dist >= 0) { return StringUtils.toString(dist); } else { return "Odekakedekinai.."; } } private int solve(int W, int H, int[][] B) { if (W == 1 && H == 1) { return 0; } int[][][] connection = new int[W + 1][H + 1][4]; for (int i = 0; i < B.length; i++) { int x = B[i][0] % W, y = B[i][0] / W; for (int j = 1; j < B[i].length; j++) { int xx = B[i][j] % W, yy = B[i][j] / W; if (xx != x) { int ax = x, bx = xx; if (ax > bx) { ax = xx; bx = x; } connection[ax][y][0]++; connection[bx][y][0]--; connection[ax + 1][y][2]++; connection[bx + 1][y][2]--; } else if (yy != y) { int ay = y, by = yy; if (ay > by) { ay = yy; by = y; } connection[x][ay][1]++; connection[x][by][1]--; connection[x][ay + 1][3]++; connection[x][by + 1][3]--; } x = xx; y = yy; } } for (int x = 0; x < W; x++) { for (int y = 0; y < H; y++) { int px = x - 1, py = y - 1; if (px >= 0) { connection[x][y][0] += connection[px][y][0]; connection[x][y][2] += connection[px][y][2]; } if (py >= 0) { connection[x][y][1] += connection[x][py][1]; connection[x][y][3] += connection[x][py][3]; } } } int[][] visited = new int[W][H]; Queue<int[]> que = new LinkedList<>(); que.add(new int[]{0, 0}); while (que.size() > 0) { int[] xy = que.poll(); for (int d = 0; d < 4; d++) { if (connection[xy[0]][xy[1]][d] > 0) { int xx = xy[0] + DX[d]; int yy = xy[1] + DY[d]; if (0 <= xx && xx < W && 0 <= yy && yy < H && visited[xx][yy] <= 0) { visited[xx][yy] = visited[xy[0]][xy[1]] + 1; que.add(new int[]{xx, yy}); } } } } return visited[W - 1][H - 1] > 0 ? visited[W - 1][H - 1] : -1; } } /** * This class is a solver to confirm the main solver. * This solver often works only for a problem with a small number setting. */ class Solver2 implements IProblemSolver<Problem> { @Override public String solve(Problem problem) { // TODO return null; } } class Factory implements IProblemFactory<Problem> { @Override public Problem newProblem() { // TODO return null; } @Override public Problem newProblem(ProblemReader reader) throws IOException { Problem problem = new Problem(); problem.set(reader); return problem; } } ProblemProcessor<Problem> processor; Main() { Init(); } void Init() { ProblemProcessorConfig<Problem> config = new ProblemProcessorConfig<>(); config.factory = new Factory(); config.solver = new Solver(); config.solver2 = new Solver2(); processor = new ProblemProcessor<Problem>(config); } void process(String mode) throws IOException { processor.process(mode); } public static void main(String args[]) throws IOException { String mode = "solve"; new Main().process(mode); } /* Common library */ interface IProblem { abstract void set(ProblemReader reader) throws IOException; } interface IProblemFactory<P extends IProblem> { abstract P newProblem(); abstract P newProblem(ProblemReader reader) throws IOException; } interface IProblemSolver<P extends IProblem> { abstract String solve(P problem); } class ProblemReader { BufferedReader br = null; ProblemReader() { br = new BufferedReader(new InputStreamReader(System.in)); } int nextInt() throws IOException { return StringUtils.toInt(br.readLine()); } long nextLong() throws IOException { return StringUtils.toLong(br.readLine()); } double nextDouble() throws IOException { return StringUtils.toDouble(br.readLine()); } String nextString() throws IOException { return br.readLine(); } int[] nextIntArray() throws IOException { return StringUtils.toIntArray(br.readLine()); } long[] nextLongArray() throws IOException { return StringUtils.toLongArray(br.readLine()); } double[] nextDoubleArray() throws IOException { return StringUtils.toDoubleArray(br.readLine()); } String[] nextStringArray() throws IOException { return StringUtils.toStringArray(br.readLine()); } int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } long[] nextLongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } double[] nextDoubleArray(int n) throws IOException { double[] a = new double[n]; for (int i = 0; i < n; i++) { a[i] = nextDouble(); } return a; } String[] nextStringArray(int n) throws IOException { String[] strs = new String[n]; for (int i = 0; i < n; i++) { strs[i] = nextString(); } return strs; } int[][] nextIntArray2(int n) throws IOException { int[][] a = new int[n][]; for (int i = 0; i < n; i++) { a[i] = nextIntArray(); } return a; } long[][] nextLongArray2(int n) throws IOException { long[][] a = new long[n][]; for (int i = 0; i < n; i++) { a[i] = nextLongArray(); } return a; } double[][] nextDoubleArray2(int n) throws IOException { double[][] a = new double[n][]; for (int i = 0; i < n; i++) { a[i] = nextDoubleArray(); } return a; } String[][] nextStringArray2(int n) throws IOException { String[][] strs = new String[n][]; for (int i = 0; i < n; i++) { strs[i] = nextStringArray(); } return strs; } void close() throws IOException { if (br != null) { br.close(); } } } class ProblemCreator<P extends IProblem> { IProblemFactory<P> factory; ProblemCreator(IProblemFactory<P> factory) { this.factory = factory; } void create() throws FileNotFoundException { P problem = factory.newProblem(); System.out.println(problem.toString()); } } class ProblemSolverComparator<P extends IProblem> { IProblemFactory<P> factory; IProblemSolver<P> solver1, solver2; ProblemSolverComparator(IProblemFactory<P> factory, IProblemSolver<P> solver1, IProblemSolver<P> solver2) { this.factory = factory; this.solver1 = solver1; this.solver2 = solver2; } void compare() throws IOException { ProblemReader reader = null; try { reader = new ProblemReader(); P problem = factory.newProblem(reader); String res1 = solver1.solve(problem); String res2 = solver2.solve(problem); if (res1.equals(res2)) { System.out.println(res1); } else { System.err.println(res1 + " " + res2); } } finally { if (reader != null) { reader.close(); } } } } class ProblemAnswerer<P extends IProblem> { IProblemFactory<P> factory; IProblemSolver<P> solver; ProblemAnswerer(IProblemFactory<P> factory, IProblemSolver<P> solver) { this.factory = factory; this.solver = solver; } void answer() throws IOException { ProblemReader reader = null; try { reader = new ProblemReader(); P problem = factory.newProblem(reader); String res = solver.solve(problem); System.out.println(res); } finally { if (reader != null) { reader.close(); } } } } class ProblemProcessorConfig<P extends IProblem> { IProblemFactory<P> factory; IProblemSolver<P> solver, solver2; } class ProblemProcessor<P extends IProblem> { ProblemProcessorConfig<P> config; ProblemCreator<P> creator; ProblemSolverComparator<P> comparator; ProblemAnswerer<P> answerer; ProblemProcessor(ProblemProcessorConfig<P> config) { this.config = config; creator = new ProblemCreator<P>(config.factory); comparator = new ProblemSolverComparator<P>(config.factory, config.solver, config.solver2); answerer = new ProblemAnswerer<P>(config.factory, config.solver); } void process(String mode) throws IOException { switch (mode) { case "create": creator.create(); break; case "debug": comparator.compare(); break; case "solve": answerer.answer(); break; } } } static class StringUtils { static String toString(int a) { return String.valueOf(a); } static String toString(long a) { return String.valueOf(a); } static String toString(double a) { return String.valueOf(a); } static String toString(String str) { return str; } static String toString(int[] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { if (i < a.length - 1) { sb.append(a[i] + " "); } else { sb.append(a[i]); } } return sb.toString(); } static String toString(long[] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { if (i < a.length - 1) { sb.append(a[i] + " "); } else { sb.append(a[i]); } } return sb.toString(); } static String toString(double[] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { if (i < a.length - 1) { sb.append(a[i] + " "); } else { sb.append(a[i]); } } return sb.toString(); } static String toString(String[] strs) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < strs.length; i++) { if (i < strs.length - 1) { sb.append(strs[i] + " "); } else { sb.append(strs[i]); } } return sb.toString(); } static String toString(int[][] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { if (j < a[i].length - 1) { sb.append(a[i][j] + " "); } else { sb.append(a[i][j]); } } sb.append("\n"); } sb.setLength(sb.length() - 1); return sb.toString(); } static String toString(long[][] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { if (j < a[i].length - 1) { sb.append(a[i][j] + " "); } else { sb.append(a[i][j]); } } sb.append("\n"); } sb.setLength(sb.length() - 1); return sb.toString(); } static String toString(double[][] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { if (j < a[i].length - 1) { sb.append(a[i][j] + " "); } else { sb.append(a[i][j]); } } sb.append("\n"); } sb.setLength(sb.length() - 1); return sb.toString(); } static String toString(String[][] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { if (j < a[i].length - 1) { sb.append(a[i][j] + " "); } else { sb.append(a[i][j]); } } sb.append("\n"); } sb.setLength(sb.length() - 1); return sb.toString(); } static int toInt(String s) throws IOException { return Integer.parseInt(s); } static long toLong(String s) throws IOException { return Long.parseLong(s); } static double toDouble(String s) throws IOException { return Double.parseDouble(s); } static int[] toIntArray(String s) throws IOException { String[] tokens = s.split(" "); int[] a = new int[tokens.length]; for (int i = 0; i < tokens.length; i++) { a[i] = Integer.parseInt(tokens[i]); } return a; } static long[] toLongArray(String s) throws IOException { String[] tokens = s.split(" "); long[] a = new long[tokens.length]; for (int i = 0; i < tokens.length; i++) { a[i] = Long.parseLong(tokens[i]); } return a; } static double[] toDoubleArray(String s) throws IOException { String[] tokens = s.split(" "); double[] a = new double[tokens.length]; for (int i = 0; i < tokens.length; i++) { a[i] = Double.parseDouble(tokens[i]); } return a; } static String[] toStringArray(String s) throws IOException { return s.split(" "); } } }