Submission #115743

# Submission time Handle Problem Language Result Execution time Memory
115743 2019-06-08T20:00:18 Z Leonardo_Paes Portal (COCI17_portal) C++11
96 / 120
111 ms 36220 KB
#include <bits/stdc++.h>
#define MAXN 610
using namespace std;

#define int long long

typedef pair < int, int > ii;
typedef pair < int, ii > iii;

int n, m, val[MAXN][MAXN], c, f, vist[MAXN*MAXN];
char mat[MAXN][MAXN];

int l[MAXN][MAXN], r[MAXN][MAXN], u[MAXN][MAXN], d[MAXN][MAXN];

vector < int > grafo[MAXN*MAXN];

int bfs()
{
	queue < ii > fila;
	fila.push({c, 1});

	while(fila.size())
	{
		ii u = fila.front();
		fila.pop();
		int v = u.first;
		int d = u.second;

		if(vist[v]) continue;
		vist[v] = d;

		//cout << v << ' ' << d << endl;
		if(v == f) return d;

		for(int i = 0 ; i < grafo[v].size() ; i++)
		{
			int at = grafo[v][i];
			if(!vist[at]) fila.push({at, d+1});
		}
	}

	return -1;
}

 main()
{
	cin >> n >> m;
	int cont = 1;
	for(int i = 1 ; i <= n ; i++)
	{
		for(int j = 1 ; j <= m ; j++)
		{
			cin >> mat[i][j];
			val[i][j] = cont++;
			if(mat[i][j] == 'C') c = cont-1;
			else if(mat[i][j] == 'F') f = cont-1;
		}
	}

	// vizinhos esquerda

	int last =0;

	for(int i=1; i<=n; i++){
		for(int j=1; j<=m; j++){
			if(mat[i][j]=='#'){
				last=j;
			}
			else{
				if(mat[i+1][j]=='#')l[i][j]=val[i][last+1];
				if(mat[i-1][j]=='#')l[i][j]=val[i][last+1];
				if(mat[i][j+1]=='#')l[i][j]=val[i][last+1];
				if(mat[i][j-1]=='#')l[i][j]=val[i][last+1];
			}
		}
	}

	// vizinhos direita

	for(int i=1; i<=n; i++){
		for(int j=m; j>=1; j--){
			if(mat[i][j]=='#'){
				last=j;
			}
			else{
				if(mat[i+1][j]=='#')r[i][j]=val[i][last-1];
				if(mat[i-1][j]=='#')r[i][j]=val[i][last-1];
				if(mat[i][j+1]=='#')r[i][j]=val[i][last-1];
				if(mat[i][j-1]=='#')r[i][j]=val[i][last-1];
			}
		}
	}

	// vizinhos cima

	for(int j=1; j<=m; j++){
		for(int i=1; i<=n; i++){
			if(mat[i][j]=='#'){
				last=i;
			}
			else{
				if(mat[i+1][j]=='#')u[i][j]=val[last+1][j];
				if(mat[i-1][j]=='#')u[i][j]=val[last+1][j];
				if(mat[i][j+1]=='#')u[i][j]=val[last+1][j];
				if(mat[i][j-1]=='#')u[i][j]=val[last+1][j];
			}
		}
	}


	// vizinhos baixo

	for(int j=1; j<=m; j++){
		for(int i=n; i>=1; i--){
			if(mat[i][j]=='#'){
				last=i;
			}
			else{
				if(mat[i+1][j]=='#')d[i][j]=val[last-1][j];
				if(mat[i-1][j]=='#')d[i][j]=val[last-1][j];
				if(mat[i][j+1]=='#')d[i][j]=val[last-1][j];
				if(mat[i][j-1]=='#')d[i][j]=val[last-1][j];
			}
		}
	}

	for(int i=1; i<=n; i++){
		for(int j=1; j<=m; j++){
			if(mat[i][j]=='#')continue;
			if(mat[i+1][j]!='#')grafo[val[i][j]].push_back(val[i+1][j]);
			if(mat[i-1][j]!='#')grafo[val[i][j]].push_back(val[i-1][j]);
			if(mat[i][j+1]!='#')grafo[val[i][j]].push_back(val[i][j+1]);
			if(mat[i][j-1]!='#')grafo[val[i][j]].push_back(val[i][j-1]);
			
			if(mat[i+1][j]=='#')
			{
				grafo[val[i][j]].push_back(r[i][j]);
				grafo[val[i][j]].push_back(d[i][j]);
				grafo[val[i][j]].push_back(l[i][j]);
				grafo[val[i][j]].push_back(u[i][j]);
			}
			else if(mat[i-1][j]=='#'){
				grafo[val[i][j]].push_back(r[i][j]);
				grafo[val[i][j]].push_back(d[i][j]);
				grafo[val[i][j]].push_back(l[i][j]);
				grafo[val[i][j]].push_back(u[i][j]);
			}
			else if(mat[i][j+1]=='#'){
				grafo[val[i][j]].push_back(r[i][j]);
				grafo[val[i][j]].push_back(d[i][j]);
				grafo[val[i][j]].push_back(l[i][j]);
				grafo[val[i][j]].push_back(u[i][j]);
			}
			else if(mat[i][j-1]=='#'){
				grafo[val[i][j]].push_back(r[i][j]);
				grafo[val[i][j]].push_back(d[i][j]);
				grafo[val[i][j]].push_back(l[i][j]);
				grafo[val[i][j]].push_back(u[i][j]);
			}
		}
	}

	/*for(int i = 1 ; i <= n ; i++)
	{
		for(int j = 1 ; j <= m ; j++)
		{
			cout << "VERT: " << i << ' ' << j << endl;
			for(int k = 0 ; k < grafo[val[i][j]].size() ; k++)
			{
				cout << grafo[val[i][j]][k] << ' ';
			}
			cout << endl;
		}
	}*/

	vist[0] = 1;
	int resp = bfs();

	if(resp == -1) cout << "nemoguce\n";
	else cout << resp-1 << endl;
}

Compilation message

portal.cpp: In function 'long long int bfs()':
portal.cpp:35:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0 ; i < grafo[v].size() ; i++)
                   ~~^~~~~~~~~~~~~~~~~
portal.cpp: At global scope:
portal.cpp:45:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  main()
       ^
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 9216 KB Output is correct
2 Correct 10 ms 9344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 9344 KB Output is correct
2 Correct 10 ms 9344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9472 KB Output is correct
2 Correct 10 ms 9344 KB Output is correct
3 Correct 9 ms 9344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 14968 KB Output is correct
2 Correct 19 ms 12416 KB Output is correct
3 Correct 18 ms 12800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 16128 KB Output is correct
2 Correct 20 ms 14976 KB Output is correct
3 Correct 24 ms 13952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 27328 KB Output is correct
2 Correct 50 ms 21112 KB Output is correct
3 Correct 47 ms 21504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 105 ms 32632 KB Output is correct
2 Correct 79 ms 30284 KB Output is correct
3 Correct 73 ms 24912 KB Output is correct
4 Incorrect 55 ms 25592 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 111 ms 36220 KB Output isn't correct
2 Halted 0 ms 0 KB -