Submission #553445

# Submission time Handle Problem Language Result Execution time Memory
553445 2022-04-25T21:15:29 Z new_acc Portals (BOI14_portals) C++14
70 / 100
1000 ms 50636 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];
priority_queue<pair<int,pair<int,int> >, vector<pair<int,pair<int,int> > >,greater<pair<int,pair<int,int> > > >deq;
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){
	vis[a][b]=1;
	if(inn(a-1,b) and !vis[a-1][b]) deq.push({odl[a][b]+1,{a-1,b}});
	if(inn(a+1,b) and !vis[a+1][b]) deq.push({odl[a][b]+1,{a+1,b}});
	if(inn(a,b+1) and !vis[a][b+1]) deq.push({odl[a][b]+1,{a,b+1}});
	if(inn(a,b-1) and !vis[a][b-1]) deq.push({odl[a][b]+1,{a,b-1}});
	if(!vis[a][last[a][b][0]]) deq.push({odl[a][b]+naj[a][b]+1,{a,last[a][b][0]}});
	if(!vis[a][last[a][b][1]]) deq.push({odl[a][b]+naj[a][b]+1,{a,last[a][b][1]}});
	if(!vis[last[a][b][2]][b]) deq.push({odl[a][b]+naj[a][b]+1,{last[a][b][2],b}});
	if(!vis[last[a][b][3]][b]) deq.push({odl[a][b]+naj[a][b]+1,{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];
		}	
	}
	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];
		}
	}
	for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) naj[i][j]=min({j-last[i][j][0],last[i][j][1]-j,i-last[i][j][2],last[i][j][3]-i});
	while(deq.size()){
		pair<int,pair<int,int> >v=deq.top();
		deq.pop();
		if(vis[v.se.fi][v.se.se]) continue;
		naj[v.se.fi][v.se.se]=v.fi;
		vis[v.se.fi][v.se.se]=1;
		pair<int,int>u=v.se;
		if(inn(u.fi+1,u.se) and !vis[u.fi+1][u.se]) deq.push({naj[u.fi][u.se]+1,{u.fi+1,u.se}});
		if(inn(u.fi-1,u.se) and !vis[u.fi-1][u.se]) deq.push({naj[u.fi][u.se]+1,{u.fi-1,u.se}});
		if(inn(u.fi,u.se+1) and !vis[u.fi][u.se+1]) deq.push({naj[u.fi][u.se]+1,{u.fi,u.se+1}});
		if(inn(u.fi,u.se-1) and !vis[u.fi][u.se-1]) deq.push({naj[u.fi][u.se]+1,{u.fi,u.se-1}});
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++) vis[i][j]=0;
	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.push({0,s});
	while(deq.size()){
		pair<int,pair<int,int> >v=deq.top();
		deq.pop();
		if(vis[v.se.fi][v.se.se]) continue;
		odl[v.se.fi][v.se.se]=v.fi;
		add(v.se.fi,v.se.se);
	}
	cout<<odl[tt.fi][tt.se]<<"\n";
}
int main(){
	ios_base::sync_with_stdio(0),cin.tie(0);
	solve();
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 2 ms 980 KB Output is correct
10 Correct 2 ms 1108 KB Output is correct
11 Correct 1 ms 980 KB Output is correct
12 Correct 1 ms 980 KB Output is correct
13 Correct 1 ms 980 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 980 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 8 ms 3756 KB Output is correct
6 Correct 10 ms 3668 KB Output is correct
7 Correct 10 ms 3784 KB Output is correct
8 Correct 4 ms 3668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 0 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 2 ms 980 KB Output is correct
10 Correct 2 ms 1108 KB Output is correct
11 Correct 1 ms 980 KB Output is correct
12 Correct 1 ms 980 KB Output is correct
13 Correct 1 ms 980 KB Output is correct
14 Correct 7 ms 3668 KB Output is correct
15 Correct 9 ms 3668 KB Output is correct
16 Correct 10 ms 3712 KB Output is correct
17 Correct 13 ms 3668 KB Output is correct
18 Correct 16 ms 3796 KB Output is correct
19 Correct 19 ms 3872 KB Output is correct
20 Correct 34 ms 5324 KB Output is correct
21 Correct 8 ms 3756 KB Output is correct
22 Correct 9 ms 3668 KB Output is correct
23 Correct 12 ms 3668 KB Output is correct
24 Correct 12 ms 3784 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 980 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 4 ms 3668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 3 ms 1108 KB Output is correct
10 Correct 3 ms 1204 KB Output is correct
11 Correct 1 ms 980 KB Output is correct
12 Correct 1 ms 980 KB Output is correct
13 Correct 1 ms 980 KB Output is correct
14 Correct 8 ms 3668 KB Output is correct
15 Correct 8 ms 3680 KB Output is correct
16 Correct 10 ms 3696 KB Output is correct
17 Correct 11 ms 3780 KB Output is correct
18 Correct 15 ms 3796 KB Output is correct
19 Correct 20 ms 3884 KB Output is correct
20 Correct 31 ms 5324 KB Output is correct
21 Correct 7 ms 3668 KB Output is correct
22 Correct 7 ms 3768 KB Output is correct
23 Correct 10 ms 3720 KB Output is correct
24 Correct 341 ms 26316 KB Output is correct
25 Correct 434 ms 26876 KB Output is correct
26 Correct 736 ms 33052 KB Output is correct
27 Execution timed out 1052 ms 50636 KB Time limit exceeded
28 Halted 0 ms 0 KB -