Submission #26196

# Submission time Handle Problem Language Result Execution time Memory
26196 2017-06-28T10:10:21 Z samir_droubi Portals (BOI14_portals) C++14
70 / 100
1000 ms 145192 KB
#include <bits/stdc++.h>
using namespace std;
const int mxn=1005;
int n,m;
char g[mxn][mxn];

int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
bool check(int x,int y){
	return x>=1&&x<=n&&y>=1&&y<=m&&g[x][y]!='#';
}

int wall[mxn][mxn];
queue<pair<int,int> >q;
void bfs()
{
	while(!q.empty())
	{
		int x=q.front().first;
		int y=q.front().second;
		q.pop();
		for(int i=0;i<4;++i)
		{
			int xx=x+dx[i];
			int yy=y+dy[i];
			if(!check(xx,yy))continue;
			if(wall[xx][yy]>wall[x][y]+1)
			{
				wall[xx][yy]=wall[x][y]+1;
				q.push({xx,yy});
			}
		}
	}
}

vector<pair<pair<int,int>,int> >adj[mxn][mxn];;
int dist[mxn][mxn];
set<pair<int,pair<int,int> > >pq;
void dijkstra(int x,int y)
{
	dist[x][y]=0;
	pq.insert({0,{x,y}});
	while(!pq.empty())
	{
		pair<int,pair<int,int> > p=*pq.begin();
		pq.erase(pq.begin());
		int ds=p.first;
		int x=p.second.first;
		int y=p.second.second;
		for(int i=0;i<adj[x][y].size();++i)
		{
			int xx=adj[x][y][i].first.first;
			int yy=adj[x][y][i].first.second;
			int w=adj[x][y][i].second;
			if(dist[xx][yy]>ds+w)
			{
				dist[xx][yy]=ds+w;
				pq.insert({ds+w,{xx,yy}});
			}
		}		
	}
}

void clear()
{
	for(int i=0;i<mxn;++i)
		for(int j=0;j<mxn;++j)
		{
			wall[i][j]=(1e9);
			dist[i][j]=(1e9);
		}
}
void up(int i,int j)
{
	int x=i;
	while(x>=1&&g[x][j]!='#')
	{
		adj[x][j].push_back({{i,j},wall[x][j]});
		--x;
	}
}
void down(int i,int j)
{
	int x=i;
	while(x<=n&&g[x][j]!='#')
	{
		adj[x][j].push_back({{i,j},wall[x][j]});
		++x;
	}
}
void left(int i,int j)
{
	int y=j;
	while(y>=1&&g[i][y]!='#')
	{
		adj[i][y].push_back({{i,j},wall[i][y]});
		--y;
	}
}
void right(int i,int j)
{
	int y=j;
	while(y<=m&&g[i][y]!='#')
	{
		adj[i][y].push_back({{i,j},wall[i][y]});
		++y;
	}
}
int main()
{
	clear();
	int sx,sy,ex,ey;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
		{
			cin>>g[i][j];
			if(g[i][j]=='S')
			{
				sx=i;
				sy=j;
			}
			if(g[i][j]=='C')
			{
				ex=i;
				ey=j;
			}
		}

	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
		{
			bool ok=false;
			for(int k=0;k<4;++k)
			{
				int x=i+dx[k];
				int y=j+dy[k];
				if(!check(x,y))ok=true;
				else adj[i][j].push_back({{x,y},1});
			}
			if(ok)
			{
				wall[i][j]=1;
				q.push({i,j});
			}
		}	

	bfs();

	for(int i=1;i<=n;++i)
		for(int j=1;j<=n;++j)
		{
			if(g[i][j]!='#')continue;
			right(i,j+1);
			left(i,j-1);
			up(i-1,j);
			down(i+1,j);
		}
	for(int i=1;i<=n;++i)
	{
		right(i,1);
		left(i,n);
	}
	for(int j=1;j<=m;++j)
	{
		down(1,j);
		up(m,j);
	}

	dijkstra(sx,sy);
	printf("%d\n",dist[ex][ey]);
	return 0;
}

Compilation message

portals.cpp: In function 'void dijkstra(int, int)':
portals.cpp:50:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0;i<adj[x][y].size();++i)
                ^
portals.cpp: In function 'int main()':
portals.cpp:113:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d",&n,&m);
                     ^
portals.cpp:171:8: warning: 'ey' may be used uninitialized in this function [-Wmaybe-uninitialized]
  printf("%d\n",dist[ex][ey]);
        ^
portals.cpp:171:8: warning: 'ex' may be used uninitialized in this function [-Wmaybe-uninitialized]
portals.cpp:170:17: warning: 'sy' may be used uninitialized in this function [-Wmaybe-uninitialized]
  dijkstra(sx,sy);
                 ^
portals.cpp:170:17: warning: 'sx' may be used uninitialized in this function [-Wmaybe-uninitialized]
# Verdict Execution time Memory Grader output
1 Correct 9 ms 34576 KB Output is correct
2 Correct 3 ms 34576 KB Output is correct
3 Correct 9 ms 34576 KB Output is correct
4 Correct 3 ms 34576 KB Output is correct
5 Correct 3 ms 34576 KB Output is correct
6 Correct 13 ms 34576 KB Output is correct
7 Correct 6 ms 34576 KB Output is correct
8 Correct 3 ms 34576 KB Output is correct
9 Correct 9 ms 34576 KB Output is correct
10 Correct 3 ms 34576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 34576 KB Output is correct
2 Correct 3 ms 34576 KB Output is correct
3 Correct 6 ms 34576 KB Output is correct
4 Correct 3 ms 34576 KB Output is correct
5 Correct 9 ms 34576 KB Output is correct
6 Correct 9 ms 34576 KB Output is correct
7 Correct 9 ms 34576 KB Output is correct
8 Correct 9 ms 34576 KB Output is correct
9 Correct 13 ms 34972 KB Output is correct
10 Correct 6 ms 34840 KB Output is correct
11 Correct 6 ms 34840 KB Output is correct
12 Correct 13 ms 34840 KB Output is correct
13 Correct 13 ms 34840 KB Output is correct
14 Correct 9 ms 34576 KB Output is correct
15 Correct 9 ms 34840 KB Output is correct
16 Correct 9 ms 34576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 34576 KB Output is correct
2 Correct 6 ms 34576 KB Output is correct
3 Correct 9 ms 34576 KB Output is correct
4 Correct 16 ms 34576 KB Output is correct
5 Correct 39 ms 38536 KB Output is correct
6 Correct 43 ms 38668 KB Output is correct
7 Correct 43 ms 38800 KB Output is correct
8 Correct 23 ms 39196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 34576 KB Output is correct
2 Correct 16 ms 34576 KB Output is correct
3 Correct 6 ms 34576 KB Output is correct
4 Correct 3 ms 34576 KB Output is correct
5 Correct 9 ms 34576 KB Output is correct
6 Correct 16 ms 34576 KB Output is correct
7 Correct 13 ms 34576 KB Output is correct
8 Correct 6 ms 34576 KB Output is correct
9 Correct 13 ms 34972 KB Output is correct
10 Correct 6 ms 34840 KB Output is correct
11 Correct 6 ms 34840 KB Output is correct
12 Correct 9 ms 34840 KB Output is correct
13 Correct 9 ms 34840 KB Output is correct
14 Correct 39 ms 38536 KB Output is correct
15 Correct 33 ms 38668 KB Output is correct
16 Correct 43 ms 38800 KB Output is correct
17 Correct 53 ms 38272 KB Output is correct
18 Correct 46 ms 38932 KB Output is correct
19 Correct 43 ms 39064 KB Output is correct
20 Correct 39 ms 39064 KB Output is correct
21 Correct 43 ms 38668 KB Output is correct
22 Correct 26 ms 38668 KB Output is correct
23 Correct 43 ms 38668 KB Output is correct
24 Correct 39 ms 38932 KB Output is correct
25 Correct 6 ms 34576 KB Output is correct
26 Correct 13 ms 34840 KB Output is correct
27 Correct 9 ms 34576 KB Output is correct
28 Correct 36 ms 39196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 34576 KB Output is correct
2 Correct 6 ms 34576 KB Output is correct
3 Correct 9 ms 34576 KB Output is correct
4 Correct 9 ms 34576 KB Output is correct
5 Correct 6 ms 34576 KB Output is correct
6 Correct 6 ms 34576 KB Output is correct
7 Correct 19 ms 34576 KB Output is correct
8 Correct 6 ms 34576 KB Output is correct
9 Correct 3 ms 34972 KB Output is correct
10 Correct 9 ms 34840 KB Output is correct
11 Correct 6 ms 34840 KB Output is correct
12 Correct 6 ms 34840 KB Output is correct
13 Correct 9 ms 34840 KB Output is correct
14 Correct 39 ms 38536 KB Output is correct
15 Correct 29 ms 38668 KB Output is correct
16 Correct 33 ms 38800 KB Output is correct
17 Correct 29 ms 38272 KB Output is correct
18 Correct 39 ms 38932 KB Output is correct
19 Correct 39 ms 39064 KB Output is correct
20 Correct 33 ms 39064 KB Output is correct
21 Correct 36 ms 38668 KB Output is correct
22 Correct 29 ms 38668 KB Output is correct
23 Correct 46 ms 38668 KB Output is correct
24 Correct 879 ms 132156 KB Output is correct
25 Execution timed out 1000 ms 145192 KB Execution timed out
26 Halted 0 ms 0 KB -