Submission #553449

# Submission time Handle Problem Language Result Execution time Memory
553449 2022-04-25T21:25:51 Z new_acc Portals (BOI14_portals) C++14
100 / 100
192 ms 88000 KB
#include<bits/stdc++.h>
#define fi first
#define se second
#define pitem item*
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
const int N=1e3+10;
const int SS=1<<19;
const int INFi=2e9;
const ll INFl=1e13;
const ll mod2=998244353;
const ll mod=1e9+7;
const ll mod3=1000696969;
const ll p=70032301;
const ull p2=913;
const int L=20;
int last[N][N][4],odl[N][N],m,n,naj[N][N];
char t[N][N];
vector<pair<int,int> > deq[N*N];
bool vis[N][N];
bool inn(int a,int b){
	return (a>=1 and a<=n and b>=1 and b<=m and t[a][b]!='#');
}
void add(int a,int b,int o){
	if(vis[a][b]) return;
	vis[a][b]=1;
	odl[a][b]=o;
	if(inn(a-1,b) and !vis[a-1][b]) deq[odl[a][b]+1].push_back({a-1,b});
	if(inn(a+1,b) and !vis[a+1][b]) deq[odl[a][b]+1].push_back({a+1,b});
	if(inn(a,b+1) and !vis[a][b+1]) deq[odl[a][b]+1].push_back({a,b+1});
	if(inn(a,b-1) and !vis[a][b-1]) deq[odl[a][b]+1].push_back({a,b-1});
	if(!vis[a][last[a][b][0]]) deq[odl[a][b]+naj[a][b]+1].push_back({a,last[a][b][0]});
	if(!vis[a][last[a][b][1]]) deq[odl[a][b]+naj[a][b]+1].push_back({a,last[a][b][1]});
	if(!vis[last[a][b][2]][b]) deq[odl[a][b]+naj[a][b]+1].push_back({last[a][b][2],b});
	if(!vis[last[a][b][3]][b]) deq[odl[a][b]+naj[a][b]+1].push_back({last[a][b][3],b});
}
void solve(){
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++) cin>>t[i][j];
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(t[i][j-1]=='#' or j==1) last[i][j][0]=j;
			else last[i][j][0]=last[i][j-1][0];
			if(t[i-1][j]=='#' or i==1) last[i][j][2]=i;
			else last[i][j][2]=last[i-1][j][2];
		}	
	}
	deque<pair<int,int> >tf;
	for(int i=n;i>=1;i--){
		for(int j=m;j>=1;j--){
			if(t[i+1][j]=='#' or i==n) last[i][j][3]=i;
			else last[i][j][3]=last[i+1][j][3];
			if(t[i][j+1]=='#' or j==m) last[i][j][1]=j;
			else last[i][j][1]=last[i][j+1][1];
			if(t[i][j]=='#') tf.push_back({i,j});
			else naj[i][j]=INFi;
		}
		tf.push_back({i,0}),tf.push_back({i,m+1});
	}
	for(int i=1;i<=m;i++) tf.push_back({0,i}),tf.push_back({n+1,i});
	while(tf.size()){
		pair<int,int> v=tf.front();
		tf.pop_front();
		if(inn(v.fi+1,v.se) and naj[v.fi+1][v.se]==INFi) naj[v.fi+1][v.se]=naj[v.fi][v.se]+1,tf.push_back({v.fi+1,v.se});
		if(inn(v.fi-1,v.se) and naj[v.fi-1][v.se]==INFi) naj[v.fi-1][v.se]=naj[v.fi][v.se]+1,tf.push_back({v.fi-1,v.se});
		if(inn(v.fi,v.se+1) and naj[v.fi][v.se+1]==INFi) naj[v.fi][v.se+1]=naj[v.fi][v.se]+1,tf.push_back({v.fi,v.se+1});
		if(inn(v.fi,v.se-1) and naj[v.fi][v.se-1]==INFi) naj[v.fi][v.se-1]=naj[v.fi][v.se]+1,tf.push_back({v.fi,v.se-1});
	}
	for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) naj[i][j]--;
	pair<int,int>s,tt;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
		 	if(t[i][j]=='S') s={i,j};
		 	if(t[i][j]=='C') tt={i,j};
		}
	}
	deq[0].push_back(s);
	for(int i=0;i<=n*m;i++)
		for(int j=0;j<deq[i].size();j++) add(deq[i][j].fi,deq[i][j].se,i);
	cout<<odl[tt.fi][tt.se]<<"\n";
}
int main(){
	ios_base::sync_with_stdio(0),cin.tie(0);
	solve();
}

Compilation message

portals.cpp: In function 'void solve()':
portals.cpp:83:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |   for(int j=0;j<deq[i].size();j++) add(deq[i][j].fi,deq[i][j].se,i);
      |               ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 24292 KB Output is correct
2 Correct 18 ms 24404 KB Output is correct
3 Correct 17 ms 24404 KB Output is correct
4 Correct 12 ms 24276 KB Output is correct
5 Correct 16 ms 24404 KB Output is correct
6 Correct 13 ms 24404 KB Output is correct
7 Correct 13 ms 24424 KB Output is correct
8 Correct 13 ms 24404 KB Output is correct
9 Correct 14 ms 24276 KB Output is correct
10 Correct 19 ms 24276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 24276 KB Output is correct
2 Correct 17 ms 24404 KB Output is correct
3 Correct 15 ms 24404 KB Output is correct
4 Correct 13 ms 24300 KB Output is correct
5 Correct 15 ms 24408 KB Output is correct
6 Correct 17 ms 24356 KB Output is correct
7 Correct 15 ms 24404 KB Output is correct
8 Correct 13 ms 24444 KB Output is correct
9 Correct 14 ms 25068 KB Output is correct
10 Correct 14 ms 25132 KB Output is correct
11 Correct 15 ms 25064 KB Output is correct
12 Correct 16 ms 25044 KB Output is correct
13 Correct 13 ms 25044 KB Output is correct
14 Correct 16 ms 24324 KB Output is correct
15 Correct 16 ms 25060 KB Output is correct
16 Correct 12 ms 24300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 24276 KB Output is correct
2 Correct 26 ms 24336 KB Output is correct
3 Correct 16 ms 24404 KB Output is correct
4 Correct 16 ms 24424 KB Output is correct
5 Correct 25 ms 28200 KB Output is correct
6 Correct 27 ms 28304 KB Output is correct
7 Correct 22 ms 28368 KB Output is correct
8 Correct 26 ms 28548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 24276 KB Output is correct
2 Correct 15 ms 24408 KB Output is correct
3 Correct 16 ms 24336 KB Output is correct
4 Correct 14 ms 24288 KB Output is correct
5 Correct 15 ms 24400 KB Output is correct
6 Correct 14 ms 24424 KB Output is correct
7 Correct 15 ms 24388 KB Output is correct
8 Correct 16 ms 24352 KB Output is correct
9 Correct 17 ms 25044 KB Output is correct
10 Correct 16 ms 25064 KB Output is correct
11 Correct 14 ms 25064 KB Output is correct
12 Correct 17 ms 25044 KB Output is correct
13 Correct 17 ms 25064 KB Output is correct
14 Correct 21 ms 28276 KB Output is correct
15 Correct 22 ms 28264 KB Output is correct
16 Correct 22 ms 28356 KB Output is correct
17 Correct 24 ms 28372 KB Output is correct
18 Correct 21 ms 28484 KB Output is correct
19 Correct 20 ms 28628 KB Output is correct
20 Correct 23 ms 29524 KB Output is correct
21 Correct 25 ms 28244 KB Output is correct
22 Correct 23 ms 28196 KB Output is correct
23 Correct 23 ms 28284 KB Output is correct
24 Correct 23 ms 28644 KB Output is correct
25 Correct 13 ms 24276 KB Output is correct
26 Correct 17 ms 25044 KB Output is correct
27 Correct 17 ms 24344 KB Output is correct
28 Correct 20 ms 28520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 24276 KB Output is correct
2 Correct 16 ms 24424 KB Output is correct
3 Correct 15 ms 24384 KB Output is correct
4 Correct 14 ms 24276 KB Output is correct
5 Correct 15 ms 24420 KB Output is correct
6 Correct 18 ms 24336 KB Output is correct
7 Correct 18 ms 24448 KB Output is correct
8 Correct 15 ms 24336 KB Output is correct
9 Correct 15 ms 25104 KB Output is correct
10 Correct 15 ms 25172 KB Output is correct
11 Correct 17 ms 25040 KB Output is correct
12 Correct 19 ms 25060 KB Output is correct
13 Correct 16 ms 25064 KB Output is correct
14 Correct 22 ms 28220 KB Output is correct
15 Correct 26 ms 28296 KB Output is correct
16 Correct 20 ms 28464 KB Output is correct
17 Correct 21 ms 28396 KB Output is correct
18 Correct 23 ms 28476 KB Output is correct
19 Correct 21 ms 28660 KB Output is correct
20 Correct 21 ms 29488 KB Output is correct
21 Correct 27 ms 28244 KB Output is correct
22 Correct 21 ms 28280 KB Output is correct
23 Correct 24 ms 28324 KB Output is correct
24 Correct 160 ms 67068 KB Output is correct
25 Correct 192 ms 66792 KB Output is correct
26 Correct 151 ms 79844 KB Output is correct
27 Correct 180 ms 88000 KB Output is correct
28 Correct 173 ms 64296 KB Output is correct
29 Correct 157 ms 64144 KB Output is correct
30 Correct 163 ms 63964 KB Output is correct
31 Correct 21 ms 28652 KB Output is correct
32 Correct 135 ms 69172 KB Output is correct
33 Correct 17 ms 24292 KB Output is correct
34 Correct 16 ms 25000 KB Output is correct
35 Correct 113 ms 62564 KB Output is correct
36 Correct 18 ms 24320 KB Output is correct
37 Correct 23 ms 28532 KB Output is correct
38 Correct 143 ms 71964 KB Output is correct
39 Correct 78 ms 55740 KB Output is correct