Submission #103344

# Submission time Handle Problem Language Result Execution time Memory
103344 2019-03-29T19:58:55 Z luciocf Portals (BOI14_portals) C++14
100 / 100
484 ms 27560 KB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e3+10;

typedef pair<int, int> pii;
typedef pair<int, pii> piii;

int ox, oy, dx, dy, n, m;

int up[maxn][maxn], down[maxn][maxn];
int lft[maxn][maxn], rght[maxn][maxn];

int dist[maxn][maxn];
int dp[maxn][maxn];

bool mark[maxn][maxn];

char tab[maxn][maxn];

int linha[] = {-1, 1, 0, 0};
int coluna[] = {0, 0, -1, 1};

void init(void)
{
	for (int i = 0; i <= n+1; i++)
		for (int j = 0; j <= m+1; j++)
			if (tab[i][j] != '#')
				dp[i][j] = 1e9+10;
}

void bfs(void)
{
	for (int i = 0; i <= n+1; i++)
		for (int j = 0; j <= m+1; j++)
			dist[i][j] = 1e9+10;
 
	dist[ox][oy] = 0;
	priority_queue<piii, vector<piii>, greater<piii>> fila;
 
	fila.push({0, {ox, oy}});
 
	while (!fila.empty())
	{
		int x = fila.top().second.first, y = fila.top().second.second;
		fila.pop();
 
		if (mark[x][y]) continue;
 
		mark[x][y] = true;
 
		for (int i = 0; i < 4; i++)
		{
			int a = x+linha[i], b = y+coluna[i];
			if (a < 0 || a > n+1 || b < 0 || b > m+1 || tab[a][b] == '#') continue;
 
			if (dist[x][y]+1 < dist[a][b])
			{
				dist[a][b] = dist[x][y]+1;
				fila.push({dist[a][b], {a, b}});
			}
		}

		int d = dp[x][y];
 
		int a = up[x][y]+1;

		if (dist[x][y]+d < dist[a][y])
		{
			dist[a][y] = dist[x][y]+d;
			fila.push({dist[a][y], {a, y}});
		}
 
		a = down[x][y]-1;

		if (dist[x][y]+d < dist[a][y])
		{
			dist[a][y] = dist[x][y]+d;
			fila.push({dist[a][y], {a, y}});
		}

		int b = lft[x][y]+1;
 
		if (dist[x][y]+d < dist[x][b])
		{
			dist[x][b] = dist[x][y]+d;
			fila.push({dist[x][b], {x, b}});
		}

		b = rght[x][y]-1;

		if (dist[x][y]+d < dist[x][b])
		{
			dist[x][b] = dist[x][y]+d;
			fila.push({dist[x][b], {x, b}});
		}
	}
}

int main(void)
{
	scanf("%d %d", &n, &m);

	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			scanf(" %c", &tab[i][j]);

			if (tab[i][j] == 'S') ox = i, oy = j;
			if (tab[i][j] == 'C') dx = i, dy = j;
		}
	}

	for (int i = 0; i <= n; i++) tab[i][0] = tab[i][m+1] = '#';
	for (int j = 0; j <= m; j++) tab[0][j] = tab[n+1][j] = '#';

	for (int i = 0; i <= n+1; i++)
	{
		for (int j = 0; j <= m+1; j++)
		{
			if (tab[i][j] == '#') up[i][j] = i;
			else up[i][j] = up[i-1][j];
		}

		for (int j = 0; j <= m+1; j++)
		{
			if (tab[i][j] == '#') lft[i][j] = j;
			else lft[i][j] = lft[i][j-1];
		}
	}

	for (int i = n+1; i >= 0; i--)
	{
		for (int j = 0; j <= m+1; j++)
		{
			if (tab[i][j] == '#') down[i][j] = i;
			else down[i][j] = down[i+1][j];
 		}

 		for (int j = m+1; j >= 1; j--)
 		{
 			if (tab[i][j] == '#') rght[i][j] = j;
			else rght[i][j] = rght[i][j+1];
 		}
	}

	init();

	for (int i = 0; i <= n+1; i++)
	{
		for (int j = 0; j <= m+1; j++)
			if (tab[i][j] != '#')
				dp[i][j] = min({dp[i][j], dp[i][j-1]+1, i-up[i][j]});

		for (int j = m+1; j >= 1; j--)
			if (tab[i][j] != '#')
				dp[i][j] = min({dp[i][j], dp[i][j+1]+1, i-up[i][j]});
	}

	for (int i = n+1; i >= 1; i--)
	{
		for (int j = 0; j <= m+1; j++)
			if (tab[i][j] != '#')
				dp[i][j] = min({dp[i][j], dp[i][j-1]+1, down[i][j]-i});

		for (int j = m+1; j >= 1; j--)
			if (tab[i][j] != '#')
				dp[i][j] = min({dp[i][j], dp[i][j+1]+1, down[i][j]-i});
	}

	bfs();

	printf("%d\n", dist[dx][dy]);
}

Compilation message

portals.cpp: In function 'int main()':
portals.cpp:103:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~~
portals.cpp:109:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf(" %c", &tab[i][j]);
    ~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 512 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 3 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 3 ms 640 KB Output is correct
9 Correct 3 ms 1664 KB Output is correct
10 Correct 4 ms 1664 KB Output is correct
11 Correct 4 ms 1664 KB Output is correct
12 Correct 4 ms 1664 KB Output is correct
13 Correct 3 ms 1664 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 4 ms 1664 KB Output is correct
16 Correct 2 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 ms 640 KB Output is correct
3 Correct 3 ms 640 KB Output is correct
4 Correct 3 ms 640 KB Output is correct
5 Correct 14 ms 5632 KB Output is correct
6 Correct 14 ms 5504 KB Output is correct
7 Correct 14 ms 5504 KB Output is correct
8 Correct 10 ms 5504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 3 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 4 ms 1664 KB Output is correct
10 Correct 3 ms 1664 KB Output is correct
11 Correct 3 ms 1664 KB Output is correct
12 Correct 4 ms 1664 KB Output is correct
13 Correct 4 ms 1664 KB Output is correct
14 Correct 13 ms 5504 KB Output is correct
15 Correct 13 ms 5632 KB Output is correct
16 Correct 18 ms 5504 KB Output is correct
17 Correct 16 ms 5504 KB Output is correct
18 Correct 15 ms 5632 KB Output is correct
19 Correct 24 ms 5632 KB Output is correct
20 Correct 20 ms 5632 KB Output is correct
21 Correct 12 ms 5504 KB Output is correct
22 Correct 12 ms 5632 KB Output is correct
23 Correct 13 ms 5632 KB Output is correct
24 Correct 13 ms 5604 KB Output is correct
25 Correct 2 ms 512 KB Output is correct
26 Correct 3 ms 1664 KB Output is correct
27 Correct 2 ms 512 KB Output is correct
28 Correct 9 ms 5632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 3 ms 640 KB Output is correct
4 Correct 5 ms 512 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 3 ms 640 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 4 ms 1664 KB Output is correct
10 Correct 3 ms 1664 KB Output is correct
11 Correct 3 ms 1664 KB Output is correct
12 Correct 6 ms 1664 KB Output is correct
13 Correct 4 ms 1664 KB Output is correct
14 Correct 13 ms 5632 KB Output is correct
15 Correct 17 ms 5632 KB Output is correct
16 Correct 13 ms 5632 KB Output is correct
17 Correct 17 ms 5624 KB Output is correct
18 Correct 16 ms 5632 KB Output is correct
19 Correct 17 ms 5632 KB Output is correct
20 Correct 15 ms 5632 KB Output is correct
21 Correct 17 ms 5624 KB Output is correct
22 Correct 16 ms 5524 KB Output is correct
23 Correct 13 ms 5632 KB Output is correct
24 Correct 239 ms 26332 KB Output is correct
25 Correct 484 ms 27560 KB Output is correct
26 Correct 294 ms 27384 KB Output is correct
27 Correct 379 ms 27384 KB Output is correct
28 Correct 189 ms 27256 KB Output is correct
29 Correct 218 ms 27116 KB Output is correct
30 Correct 288 ms 27256 KB Output is correct
31 Correct 14 ms 5604 KB Output is correct
32 Correct 293 ms 27128 KB Output is correct
33 Correct 2 ms 512 KB Output is correct
34 Correct 11 ms 1664 KB Output is correct
35 Correct 242 ms 27260 KB Output is correct
36 Correct 2 ms 512 KB Output is correct
37 Correct 9 ms 5632 KB Output is correct
38 Correct 126 ms 27112 KB Output is correct
39 Correct 177 ms 27128 KB Output is correct