Submission #396124

# Submission time Handle Problem Language Result Execution time Memory
396124 2021-04-29T13:30:51 Z Nicholas_Patrick Portals (BOI14_portals) C++17
100 / 100
829 ms 183556 KB
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;

char getTile(){
	for(char c;;){
		c=getchar();
		for(char s: ".#SC")
			if(s==c)
				return c;
	}
}
int main(){
	int r, c;
	scanf("%d%d", &r, &c);
	int v=r*c;
	vector<char> board(v);
	for(auto& i: board)
		i=getTile();
	vector<vector<int>> adjLis(v);
	vector<vector<int>> disLis(v);
	vector<int> walldist(v, r);
	int s, t;
	for(int i=0; i<r; i++)for(int j=0; j<c; j++){
		if(board[i*c+j]=='S'){
			s=i*c+j;
			board[i*c+j]='.';
		}else if(board[i*c+j]=='C'){
			t=i*c+j;
			board[i*c+j]='.';
		}
		walldist[i*c+j]=min({i+1, j+1, r-i, c-j});
		if(board[i*c+j]=='#'){
			walldist[i*c+j]=0;
			continue;
		}
		if(i and board[(i-1)*c+j]=='.'){
			adjLis[i*c+j].push_back((i-1)*c+j);
			disLis[i*c+j].push_back(1);
			adjLis[(i-1)*c+j].push_back(i*c+j);
			disLis[(i-1)*c+j].push_back(1);
		}
		if(j and board[i*c+j-1]=='.'){
			adjLis[i*c+j].push_back(i*c+j-1);
			disLis[i*c+j].push_back(1);
			adjLis[i*c+j-1].push_back(i*c+j);
			disLis[i*c+j-1].push_back(1);
		}
	}
	for(int i=0; i<r; i++){
		for(int j=1; j<c; j++)
			walldist[i*c+j]=min(walldist[i*c+j], walldist[i*c+j-1]+1);
		for(int j=c; --j;)
			walldist[i*c+j-1]=min(walldist[i*c+j-1], walldist[i*c+j]+1);
	}
	for(int j=0; j<c; j++){
		for(int i=1; i<r; i++)
			walldist[i*c+j]=min(walldist[i*c+j], walldist[(i-1)*c+j]+1);
		for(int i=r; --i;)
			walldist[(i-1)*c+j]=min(walldist[(i-1)*c+j], walldist[i*c+j]+1);
	}
	vector<int> destination(v);
	for(int i=0; i<r; i++)for(int j=c; j--;){
		if(i==0 or board[(i-1)*c+j]=='#'){
			destination[i*c+j]=i*c+j;
		}else{
			destination[i*c+j]=destination[(i-1)*c+j];
		}
		if(board[i*c+j]=='.'){
			adjLis[i*c+j].push_back(destination[i*c+j]);
			disLis[i*c+j].push_back(walldist[i*c+j]);
		}
	}
	for(int i=r; i--;)for(int j=c; j--;){
		if(i==r-1 or board[(i+1)*c+j]=='#'){
			destination[i*c+j]=i*c+j;
		}else{
			destination[i*c+j]=destination[(i+1)*c+j];
		}
		if(board[i*c+j]=='.'){
			adjLis[i*c+j].push_back(destination[i*c+j]);
			disLis[i*c+j].push_back(walldist[i*c+j]);
		}
	}
	for(int i=r; i--;)for(int j=0; j<c; j++){
		if(j==0 or board[i*c+j-1]=='#'){
			destination[i*c+j]=i*c+j;
		}else{
			destination[i*c+j]=destination[i*c+j-1];
		}
		if(board[i*c+j]=='.'){
			adjLis[i*c+j].push_back(destination[i*c+j]);
			disLis[i*c+j].push_back(walldist[i*c+j]);
		}
	}
	for(int i=r; i--;)for(int j=c; j--;){
		if(j==c-1 or board[i*c+j+1]=='#'){
			destination[i*c+j]=i*c+j;
		}else{
			destination[i*c+j]=destination[i*c+j+1];
		}
		if(board[i*c+j]=='.'){
			adjLis[i*c+j].push_back(destination[i*c+j]);
			disLis[i*c+j].push_back(walldist[i*c+j]);
		}
	}
	vector<vector<int>> dijkstra(v);
	vector<int> dist(v, v);
	dist[s]=0;
	dijkstra[0].push_back(s);
	for(int i=0; i<v; i++){
		for(int j: dijkstra[i]){
			if(dist[j]!=i)
				continue;
			if(j==t){
				printf("%d\n", i);
				return 0;
			}
			for(int k=adjLis[j].size(); k--;){
				if(dist[j]+disLis[j][k]<dist[adjLis[j][k]]){
					dist[adjLis[j][k]]=dist[j]+disLis[j][k];
					dijkstra[dist[adjLis[j][k]]].push_back(adjLis[j][k]);
				}
			}
		}
	}
}

Compilation message

portals.cpp: In function 'int main()':
portals.cpp:16:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   16 |  scanf("%d%d", &r, &c);
      |  ~~~~~^~~~~~~~~~~~~~~~
portals.cpp:116:4: warning: 't' may be used uninitialized in this function [-Wmaybe-uninitialized]
  116 |    if(j==t){
      |    ^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 208 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 2 ms 716 KB Output is correct
10 Correct 2 ms 716 KB Output is correct
11 Correct 2 ms 588 KB Output is correct
12 Correct 2 ms 588 KB Output is correct
13 Correct 3 ms 588 KB Output is correct
14 Correct 1 ms 208 KB Output is correct
15 Correct 2 ms 588 KB Output is correct
16 Correct 1 ms 280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 20 ms 6092 KB Output is correct
6 Correct 20 ms 6312 KB Output is correct
7 Correct 21 ms 6476 KB Output is correct
8 Correct 24 ms 6924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 2 ms 716 KB Output is correct
10 Correct 2 ms 796 KB Output is correct
11 Correct 2 ms 668 KB Output is correct
12 Correct 2 ms 588 KB Output is correct
13 Correct 2 ms 588 KB Output is correct
14 Correct 20 ms 6092 KB Output is correct
15 Correct 20 ms 6256 KB Output is correct
16 Correct 21 ms 6364 KB Output is correct
17 Correct 20 ms 6432 KB Output is correct
18 Correct 26 ms 7024 KB Output is correct
19 Correct 23 ms 7508 KB Output is correct
20 Correct 26 ms 7488 KB Output is correct
21 Correct 20 ms 6092 KB Output is correct
22 Correct 22 ms 6124 KB Output is correct
23 Correct 25 ms 6312 KB Output is correct
24 Correct 24 ms 7672 KB Output is correct
25 Correct 1 ms 208 KB Output is correct
26 Correct 2 ms 588 KB Output is correct
27 Correct 1 ms 280 KB Output is correct
28 Correct 19 ms 6988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 280 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 276 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 2 ms 796 KB Output is correct
10 Correct 2 ms 716 KB Output is correct
11 Correct 2 ms 588 KB Output is correct
12 Correct 2 ms 588 KB Output is correct
13 Correct 2 ms 588 KB Output is correct
14 Correct 20 ms 6120 KB Output is correct
15 Correct 21 ms 6244 KB Output is correct
16 Correct 21 ms 6492 KB Output is correct
17 Correct 20 ms 6364 KB Output is correct
18 Correct 24 ms 7108 KB Output is correct
19 Correct 23 ms 7476 KB Output is correct
20 Correct 24 ms 7540 KB Output is correct
21 Correct 20 ms 6108 KB Output is correct
22 Correct 20 ms 6228 KB Output is correct
23 Correct 20 ms 6328 KB Output is correct
24 Correct 639 ms 157968 KB Output is correct
25 Correct 829 ms 182620 KB Output is correct
26 Correct 592 ms 178936 KB Output is correct
27 Correct 663 ms 181096 KB Output is correct
28 Correct 573 ms 146780 KB Output is correct
29 Correct 572 ms 148804 KB Output is correct
30 Correct 606 ms 149360 KB Output is correct
31 Correct 24 ms 7588 KB Output is correct
32 Correct 763 ms 183556 KB Output is correct
33 Correct 1 ms 208 KB Output is correct
34 Correct 2 ms 588 KB Output is correct
35 Correct 543 ms 157412 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 24 ms 6984 KB Output is correct
38 Correct 527 ms 167932 KB Output is correct
39 Correct 367 ms 133100 KB Output is correct