답안 #197355

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
197355 2020-01-20T13:53:20 Z dndhk 포탈들 (BOI14_portals) C++14
100 / 100
957 ms 176860 KB
#include <bits/stdc++.h>

#define all(v) (v).begin(), (v).end()
#define uniqv(v) (v).erase(unique(all(v)), (v).end())
#define pb push_back
#define FI first
#define SE second
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define test 1
#define TEST if(test)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

const int MOD = 1000000007; // 998244353
const int INF = 1000000;
const ll INFLL = 1e18;
const int MAX_N = 1000;

int N, M;
string str[MAX_N+1];
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
int d1[MAX_N+1][MAX_N+1];
pii nxt[4][MAX_N+1][MAX_N+1];
int dist[MAX_N+1][MAX_N+1];
deque<pii> dq;
pii s, e;

vector<pair<pii, int> > gp[MAX_N+1][MAX_N+1];
bool vst[MAX_N+1][MAX_N+1];
priority_queue<pair<int, pii>  , vector<pair<int, pii> > , greater<pair<int, pii> > > pq;

int main(){
	scanf("%d%d", &N, &M);
	for(int i=0; i<N; i++)	cin>>str[i];
	for(int i=0; i<N; i++){
		for(int j=0; j<M; j++){
			if(str[i][j]=='S'){
				s = {i, j};
				str[i][j] = '.';
			}
			if(str[i][j]=='C'){
				e = {i, j};
				str[i][j] = '.';
			}
			if(str[i][j]=='.'){
				d1[i][j] = INF;
				if(i==0 || i==N-1 || j==0 || j==M-1){
					d1[i][j] = 0;
					dq.pb({i, j});
				}else{
					for(int k=0; k<4; k++){
						if(str[i+dx[k]][j+dy[k]]=='#'){
							d1[i][j] = 0;
							dq.pb({i, j});
							break;
						}
					}
				}
			}
		}
	}
	while(!dq.empty()){
		pii now = dq.front(); dq.pop_front();
		for(int i=0; i<4; i++){
			if(now.first+dx[i]<0 || now.first+dx[i]>=N || now.second+dy[i]<0 || now.second+dy[i]>=M)	continue;
			if(str[now.first+dx[i]][now.second+dy[i]]=='#')	continue;
			if(d1[now.first+dx[i]][now.second+dy[i]]>d1[now.first][now.second]+1){
				d1[now.first+dx[i]][now.second+dy[i]] = d1[now.first][now.second] + 1;
				dq.pb({now.first+dx[i], now.second+dy[i]});
			}
		}
	}
	for(int i=0; i<N; i++){
		for(int j=0; j<M; j++){
			if(str[i][j]=='.'){
				if(i==0 || str[i-1][j]=='#'){
					int l = i, r = i;
					while(r<N-1 && str[r+1][j]=='.'){
						r++;
					}
					for(int k=l; k<=r; k++){
						nxt[0][k][j] = {l, j};
						nxt[1][k][j] = {r, j};
					}
				}
				if(j==0 || str[i][j-1]=='#'){
					int l = j, r = j;
					while(r<M-1 && str[i][r+1]=='.'){
						r++;
					}
					for(int k=l; k<=r; k++){
						nxt[2][i][k] = {i, l};
						nxt[3][i][k] = {i, r};
					}
				}
			}
		}
	}
	for(int i=0; i<N; i++){
		for(int j=0; j<M; j++){
			if(str[i][j]=='.'){
				for(int k=0; k<4; k++){
					gp[i][j].pb({nxt[k][i][j], d1[i][j]+1});
					if(i+dx[k]<0 || i+dx[k]>=N || j+dy[k]<0 || j+dy[k]>=M)	continue;
					if(str[i+dx[k]][j+dy[k]]=='#')	continue;
					gp[i][j].pb({{i+dx[k], j+dy[k]}, 1});
				}
				dist[i][j] = INF;
			}
		}
	}
	dist[s.first][s.second] = 0;
	pq.push({0, s});
	while(!pq.empty()){
		pii p = pq.top().second; pq.pop();
		if(vst[p.first][p.second])	continue;
		//cout<<p.first<<" "<<p.second<<" "<<dist[p.first][p.second]<<endl;
		vst[p.first][p.second] = true;
		for(pair<pii, int>  i : gp[p.first][p.second]){
			if(dist[i.first.first][i.first.second]>dist[p.first][p.second] + i.second){
				dist[i.first.first][i.first.second] = dist[p.first][p.second] + i.second;
				pq.push({dist[i.first.first][i.first.second], i.first});
			}
		}
	}
	cout<<dist[e.first][e.second];
	return 0;
}

Compilation message

portals.cpp: In function 'int main()':
portals.cpp:40:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &M);
  ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23928 KB Output is correct
2 Correct 24 ms 24056 KB Output is correct
3 Correct 24 ms 24184 KB Output is correct
4 Correct 23 ms 24056 KB Output is correct
5 Correct 24 ms 24184 KB Output is correct
6 Correct 24 ms 24184 KB Output is correct
7 Correct 25 ms 24184 KB Output is correct
8 Correct 24 ms 24192 KB Output is correct
9 Correct 24 ms 24056 KB Output is correct
10 Correct 23 ms 23932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23928 KB Output is correct
2 Correct 25 ms 24180 KB Output is correct
3 Correct 24 ms 24184 KB Output is correct
4 Correct 24 ms 24184 KB Output is correct
5 Correct 24 ms 24184 KB Output is correct
6 Correct 24 ms 24184 KB Output is correct
7 Correct 24 ms 24184 KB Output is correct
8 Correct 24 ms 24184 KB Output is correct
9 Correct 26 ms 25592 KB Output is correct
10 Correct 27 ms 25464 KB Output is correct
11 Correct 27 ms 25464 KB Output is correct
12 Correct 26 ms 25336 KB Output is correct
13 Correct 26 ms 25412 KB Output is correct
14 Correct 24 ms 24184 KB Output is correct
15 Correct 26 ms 25464 KB Output is correct
16 Correct 24 ms 24056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23972 KB Output is correct
2 Correct 24 ms 24056 KB Output is correct
3 Correct 24 ms 24184 KB Output is correct
4 Correct 24 ms 24184 KB Output is correct
5 Correct 45 ms 32760 KB Output is correct
6 Correct 53 ms 32844 KB Output is correct
7 Correct 52 ms 33144 KB Output is correct
8 Correct 52 ms 33148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23928 KB Output is correct
2 Correct 65 ms 24184 KB Output is correct
3 Correct 24 ms 24188 KB Output is correct
4 Correct 25 ms 24056 KB Output is correct
5 Correct 24 ms 24184 KB Output is correct
6 Correct 24 ms 24184 KB Output is correct
7 Correct 24 ms 24312 KB Output is correct
8 Correct 24 ms 24184 KB Output is correct
9 Correct 26 ms 25464 KB Output is correct
10 Correct 27 ms 25592 KB Output is correct
11 Correct 31 ms 25464 KB Output is correct
12 Correct 30 ms 25408 KB Output is correct
13 Correct 27 ms 25464 KB Output is correct
14 Correct 46 ms 32760 KB Output is correct
15 Correct 48 ms 33016 KB Output is correct
16 Correct 49 ms 33272 KB Output is correct
17 Correct 47 ms 33084 KB Output is correct
18 Correct 52 ms 33656 KB Output is correct
19 Correct 57 ms 34680 KB Output is correct
20 Correct 56 ms 34612 KB Output is correct
21 Correct 45 ms 32760 KB Output is correct
22 Correct 47 ms 32764 KB Output is correct
23 Correct 47 ms 32988 KB Output is correct
24 Correct 55 ms 34564 KB Output is correct
25 Correct 24 ms 24056 KB Output is correct
26 Correct 26 ms 25464 KB Output is correct
27 Correct 24 ms 24056 KB Output is correct
28 Correct 43 ms 33144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23928 KB Output is correct
2 Correct 24 ms 24184 KB Output is correct
3 Correct 24 ms 24212 KB Output is correct
4 Correct 24 ms 24056 KB Output is correct
5 Correct 24 ms 24188 KB Output is correct
6 Correct 24 ms 24184 KB Output is correct
7 Correct 24 ms 24184 KB Output is correct
8 Correct 24 ms 24176 KB Output is correct
9 Correct 27 ms 25592 KB Output is correct
10 Correct 27 ms 25464 KB Output is correct
11 Correct 26 ms 25336 KB Output is correct
12 Correct 27 ms 25452 KB Output is correct
13 Correct 26 ms 25468 KB Output is correct
14 Correct 46 ms 32760 KB Output is correct
15 Correct 47 ms 32808 KB Output is correct
16 Correct 51 ms 33124 KB Output is correct
17 Correct 47 ms 33016 KB Output is correct
18 Correct 52 ms 33656 KB Output is correct
19 Correct 57 ms 34552 KB Output is correct
20 Correct 56 ms 34552 KB Output is correct
21 Correct 45 ms 32760 KB Output is correct
22 Correct 48 ms 32860 KB Output is correct
23 Correct 49 ms 33016 KB Output is correct
24 Correct 604 ms 142368 KB Output is correct
25 Correct 957 ms 176816 KB Output is correct
26 Correct 868 ms 176860 KB Output is correct
27 Correct 864 ms 176672 KB Output is correct
28 Correct 498 ms 129928 KB Output is correct
29 Correct 529 ms 131420 KB Output is correct
30 Correct 583 ms 132724 KB Output is correct
31 Correct 55 ms 34552 KB Output is correct
32 Correct 853 ms 176508 KB Output is correct
33 Correct 24 ms 24056 KB Output is correct
34 Correct 27 ms 25464 KB Output is correct
35 Correct 659 ms 149308 KB Output is correct
36 Correct 24 ms 24056 KB Output is correct
37 Correct 43 ms 33144 KB Output is correct
38 Correct 473 ms 140540 KB Output is correct
39 Correct 437 ms 114264 KB Output is correct