Submission #103298

# Submission time Handle Problem Language Result Execution time Memory
103298 2019-03-29T17:25:32 Z luciocf Portals (BOI14_portals) C++14
20 / 100
8 ms 768 KB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 210;

typedef pair<int, int> pii;

int dist[maxn][maxn], wall[maxn][maxn];
int ox, oy, dx, dy, n, m;

bool mark[maxn][maxn];

char tab[maxn][maxn];

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

int find_wall(int x, int y)
{
	memset(mark, 0, sizeof mark);

	dist[x][y] = 0, mark[x][y] = true;
	queue<pii> fila;

	fila.push({x, y});

	while (!fila.empty())
	{
		int x = fila.front().first, y = fila.front().second;
		fila.pop();

		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 || mark[a][b] || tab[a][b] == '#') continue;

			dist[a][b] = dist[x][y]+1, mark[a][b] = true;
			fila.push({a, b});
		}
	}

	int ans = 1e9+10;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			if (tab[i-1][j] == '#' || tab[i+1][j] == '#' ||
				tab[i][j-1] == '#' || tab[i][j+1] == '#')
				ans = min(ans, dist[i][j]+1);

	return ans;
}

void bfs(void)
{
	dist[ox][oy] = 0, mark[ox][oy] = true;
	queue<pii> fila;

	fila.push({ox, oy});

	while (!fila.empty())
	{
		int x = fila.front().first, y = fila.front().second;
		fila.pop();

		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 || mark[a][b] || tab[a][b] == '#') continue;

			dist[a][b] = dist[x][y]+1, mark[a][b] = true;
			fila.push({a, b});
		}

		for (int a = x; a >= 1; a--)
		{
			if (tab[a-1][y] != '#') continue;
			if (mark[a][y]) break;

			dist[a][y] = dist[x][y]+wall[a][y], mark[a][y] = true;
			fila.push({a, y});
			break;
		}

		for (int a = x; a <= n; a++)
		{
			if (tab[a+1][y] != '#') continue;
			if (mark[a][y]) break;

			dist[a][y] = dist[x][y]+wall[a][y], mark[a][y] = true;
			fila.push({a, y});
			break;
		}

		for (int b = y; b >= 1; b--)
		{
			if (tab[x][b-1] != '#') continue;
			if (mark[x][b]) break;

			dist[x][b] = dist[x][y]+wall[x][b], mark[x][b] = true;
			fila.push({x, b});
			break;
		}

		for (int b = y; b <= m; b++)
		{
			if (tab[x][b+1] != '#') continue;
			if (mark[x][b]) break;

			dist[x][b] = dist[x][y]+wall[x][b], mark[x][b] = true;
			fila.push({x, b});
			break;
		}
	}
}

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;

			wall[i][j] = 1;
		}
	}

	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] = '#';

	if (n > 50 || m > 50)
	{
		bfs();
		printf("%d\n", dist[dx][dy]);
		return 0;
	}

	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			wall[i][j] = find_wall(i, j);

	memset(mark, 0, sizeof mark);
	bfs();

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

Compilation message

portals.cpp: In function 'int main()':
portals.cpp:118: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:124: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 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Incorrect 3 ms 384 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Incorrect 3 ms 384 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 6 ms 768 KB Output is correct
6 Correct 7 ms 692 KB Output is correct
7 Correct 8 ms 768 KB Output is correct
8 Correct 5 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Incorrect 4 ms 384 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Incorrect 4 ms 384 KB Output isn't correct
7 Halted 0 ms 0 KB -