Submission #120400

# Submission time Handle Problem Language Result Execution time Memory
120400 2019-06-24T11:17:31 Z zeyad49 Portal (COCI17_portal) Java 11
96 / 120
449 ms 30036 KB
import java.io.*;
import java.util.*;

public class portal {
	static int INF = (int) 1e9;
	static int[] di = { 1, -1, 0, 0 };
	static int[] dj = { 0, 0, 1, -1 };

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner();
		PrintWriter out = new PrintWriter(System.out);
		int n = sc.nextInt(), m = sc.nextInt();
		int[][] up = new int[n][m], down = new int[n][m], left = new int[n][m], right = new int[n][m];
		int[][] dist = new int[n][m];
		for (int[] x : dist)
			Arrays.fill(x, INF);
		char[][] grid = new char[n][m];
		PriorityQueue<edge> pq = new PriorityQueue<>((x, y) -> x.c - y.c);

		for (int i = 0; i < n; i++) {
			grid[i] = sc.next().toCharArray();
			for (int j = 0; j < m; j++)
				if (grid[i][j] == 'C')
					pq.add(new edge(i, j, dist[i][j] = 0));
		}
		for (int j = 0; j < m; j++) {
			up[0][j] = 0;
			down[n - 1][j] = n - 1;

		}
		for (int i = 0; i < n; i++) {
			left[i][0] = 0;
			right[i][m - 1] = m - 1;
		}
		for (int i = 1; i < n - 1; i++)
			for (int j = 1; j < m - 1; j++) {
				up[i][j] = grid[i][j] == '#' ? i : up[i - 1][j];
				left[i][j] = grid[i][j] == '#' ? j : left[i][j - 1];

			}
		for (int i = n - 2; i > 0; i--)
			for (int j = m - 2; j > 0; j--) {
				down[i][j] = grid[i][j] == '#' ? i : down[i + 1][j];
				right[i][j] = grid[i][j] == '#' ? j : right[i][j + 1];
			}

		int ans = -1;
		while (!pq.isEmpty()) {
			edge curr = pq.poll();
			int i = curr.i, j = curr.j, c = curr.c;
			if (grid[i][j] == 'F') {
				ans = dist[i][j];
				break;
			}
			if (c > dist[i][j])
				continue;
			boolean portal = false;
			for (int k = 0; k < 4; k++) {
				int i2 = i + di[k], j2 = j + dj[k];
				if (grid[i2][j2] == '#')
					portal = true;
				if (grid[i2][j2] != '#' && c + 1 < dist[i2][j2])
					pq.add(new edge(i2, j2, dist[i2][j2] = c + 1));
			}
			if (!portal)
				continue;
			// up
			int i2 = up[i][j] + 1, j2 = j;
			if (grid[i2][j2] != '#' && c + 1 < dist[i2][j2])
				pq.add(new edge(i2, j2, dist[i2][j2] = c + 1));
			// down
			i2 = down[i][j] - 1;
			if (grid[i2][j2] != '#' && c + 1 < dist[i2][j2])
				pq.add(new edge(i2, j2, dist[i2][j2] = c + 1));
			// left
			i2 = i;
			j2 = left[i][j] + 1;
			if (grid[i2][j2] != '#' && c + 1 < dist[i2][j2])
				pq.add(new edge(i2, j2, dist[i2][j2] = c + 1));
			// right
			j2 = right[i][j] - 1;
			if (grid[i2][j2] != '#' && c + 1 < dist[i2][j2])
				pq.add(new edge(i2, j2, dist[i2][j2] = c + 1));

		}
		out.println(ans==-1?"nemoguce":ans);
		out.close();

	}

	static class edge {
		int i, j, c;

		edge(int a, int b, int C) {
			i = a;
			j = b;
			c = C;
		}
	}

	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

		Scanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		Scanner(String fileName) throws FileNotFoundException {
			br = new BufferedReader(new FileReader(fileName));
		}

		String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		String nextLine() throws IOException {
			return br.readLine();
		}

		int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		long nextLong() throws NumberFormatException, IOException {
			return Long.parseLong(next());
		}

		double nextDouble() throws NumberFormatException, IOException {
			return Double.parseDouble(next());
		}

		boolean ready() throws IOException {
			return br.ready();
		}

	}

}
# Verdict Execution time Memory Grader output
1 Correct 212 ms 15968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 192 ms 15456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 190 ms 15304 KB Output is correct
2 Correct 186 ms 15452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 202 ms 15052 KB Output is correct
2 Correct 200 ms 15588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 186 ms 14864 KB Output is correct
2 Correct 188 ms 14792 KB Output is correct
3 Correct 185 ms 14812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 209 ms 16340 KB Output is correct
2 Correct 197 ms 15976 KB Output is correct
3 Correct 245 ms 16728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 268 ms 18128 KB Output is correct
2 Correct 223 ms 16732 KB Output is correct
3 Correct 250 ms 17404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 356 ms 23508 KB Output is correct
2 Correct 306 ms 20268 KB Output is correct
3 Correct 322 ms 21524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 449 ms 30036 KB Output is correct
2 Correct 285 ms 22436 KB Output is correct
3 Correct 365 ms 24072 KB Output is correct
4 Incorrect 334 ms 23824 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 391 ms 28924 KB Output isn't correct
2 Halted 0 ms 0 KB -