Submission #408689

# Submission time Handle Problem Language Result Execution time Memory
408689 2021-05-19T13:37:13 Z Jasiekstrz Mecho (IOI09_mecho) C++17
100 / 100
438 ms 8864 KB
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int INF=1e9+7;
const int N=800;
struct Pos
{
	int x,y;
	Pos(int _x=0,int _y=0) : x(_x), y(_y) {};
	bool operator==(const Pos &oth)
	{
		return (x==oth.x && y==oth.y);
	}
	vector<Pos> edges()
	{
		vector<Pos> tmp;
		tmp.reserve(4);
		for(auto dd:{make_pair(0,-1),make_pair(0,1),make_pair(-1,0),make_pair(1,0)})
			tmp.push_back({x+dd.fi,y+dd.se});
		return tmp;
	}
};
bool fr[N+10][N+10];
queue<Pos> qq;
int bt[N+10][N+10];
pair<int,int> d[N+10][N+10];
bool ok(Pos start,Pos finish,int s,int time,int n)
{
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
			d[i][j]={INF,INF};
	}
	d[start.x][start.y]={time,0};
	qq.push(start);
	while(!qq.empty())
	{
		auto x=qq.front();
		qq.pop();
		vector<Pos> e=x.edges();
		pair<int,int> tn=(d[x.x][x.y].se+1>=s ? make_pair(d[x.x][x.y].fi+1,0):
				make_pair(d[x.x][x.y].fi,d[x.x][x.y].se+1));
		for(auto v:e)
		{
			if((fr[v.x][v.y] || v==finish) && d[v.x][v.y].se==INF && bt[v.x][v.y]>tn.fi)
			{
				d[v.x][v.y]=tn;
				qq.push(v);
			}
		}
	}
	return (d[finish.x][finish.y].se!=INF);
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n,s;
	Pos start,finish;
	cin>>n>>s;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			bt[i][j]=INF;
			char c;
			cin>>c;
			if(c=='T')
			{
				fr[i][j]=false;
			}
			else if(c=='G')
			{
				fr[i][j]=true;
			}
			else if(c=='M')
			{
				fr[i][j]=true;
				start=Pos(i,j);
			}
			else if(c=='D')
			{
				fr[i][j]=false;
				finish=Pos(i,j);
			}
			else if(c=='H')
			{
				fr[i][j]=false;
				qq.push(Pos(i,j));
				bt[i][j]=0;
			}
		}
	}
	while(!qq.empty())
	{
		Pos a=qq.front();
		qq.pop();
		vector<Pos> e=a.edges();
		for(auto v:e)
		{
			if(fr[v.x][v.y] && bt[v.x][v.y]==INF)
			{
				bt[v.x][v.y]=bt[a.x][a.y]+1;
				qq.push(v);
			}
		}
	}
	//for(int i=1;i<=n;i++)
	//{
	//	for(int j=1;j<=n;j++)
	//		cerr<<bt[i][j]<<" ";
	//	cerr<<"\n";
	//}
	if(!ok(start,finish,s,0,n))
	{
		cout<<"-1\n";
		return 0;
	}
	int bg=0,en=bt[start.x][start.y]-1;
	while(bg<en)
	{
		int mid=(bg+en+1)/2;
		if(ok(start,finish,s,mid,n))
			bg=mid;
		else
			en=mid-1;
	}
	cout<<bg<<"\n";
	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 275 ms 8588 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 844 KB Output is correct
13 Correct 1 ms 716 KB Output is correct
14 Correct 1 ms 844 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 460 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 588 KB Output is correct
22 Correct 1 ms 588 KB Output is correct
23 Correct 1 ms 716 KB Output is correct
24 Correct 1 ms 716 KB Output is correct
25 Correct 1 ms 844 KB Output is correct
26 Correct 2 ms 844 KB Output is correct
27 Correct 1 ms 844 KB Output is correct
28 Correct 2 ms 844 KB Output is correct
29 Correct 2 ms 844 KB Output is correct
30 Correct 2 ms 844 KB Output is correct
31 Correct 2 ms 972 KB Output is correct
32 Correct 2 ms 972 KB Output is correct
33 Correct 15 ms 3916 KB Output is correct
34 Correct 19 ms 3916 KB Output is correct
35 Correct 73 ms 4036 KB Output is correct
36 Correct 19 ms 4456 KB Output is correct
37 Correct 24 ms 4428 KB Output is correct
38 Correct 91 ms 4348 KB Output is correct
39 Correct 23 ms 4928 KB Output is correct
40 Correct 31 ms 4972 KB Output is correct
41 Correct 116 ms 4960 KB Output is correct
42 Correct 29 ms 5452 KB Output is correct
43 Correct 36 ms 5380 KB Output is correct
44 Correct 144 ms 5388 KB Output is correct
45 Correct 35 ms 5984 KB Output is correct
46 Correct 45 ms 5996 KB Output is correct
47 Correct 185 ms 5972 KB Output is correct
48 Correct 41 ms 6516 KB Output is correct
49 Correct 53 ms 6420 KB Output is correct
50 Correct 214 ms 6548 KB Output is correct
51 Correct 48 ms 6996 KB Output is correct
52 Correct 61 ms 6988 KB Output is correct
53 Correct 289 ms 7016 KB Output is correct
54 Correct 53 ms 7520 KB Output is correct
55 Correct 70 ms 7448 KB Output is correct
56 Correct 332 ms 7436 KB Output is correct
57 Correct 60 ms 8004 KB Output is correct
58 Correct 82 ms 8040 KB Output is correct
59 Correct 380 ms 8164 KB Output is correct
60 Correct 68 ms 8524 KB Output is correct
61 Correct 96 ms 8612 KB Output is correct
62 Correct 438 ms 8488 KB Output is correct
63 Correct 198 ms 8516 KB Output is correct
64 Correct 336 ms 8512 KB Output is correct
65 Correct 327 ms 8460 KB Output is correct
66 Correct 294 ms 8472 KB Output is correct
67 Correct 68 ms 8592 KB Output is correct
68 Correct 98 ms 8568 KB Output is correct
69 Correct 85 ms 8484 KB Output is correct
70 Correct 84 ms 8516 KB Output is correct
71 Correct 83 ms 8592 KB Output is correct
72 Correct 80 ms 8504 KB Output is correct
73 Correct 108 ms 8820 KB Output is correct
74 Correct 234 ms 8800 KB Output is correct
75 Correct 218 ms 8748 KB Output is correct
76 Correct 203 ms 8840 KB Output is correct
77 Correct 211 ms 8772 KB Output is correct
78 Correct 82 ms 8864 KB Output is correct
79 Correct 211 ms 8772 KB Output is correct
80 Correct 180 ms 8700 KB Output is correct
81 Correct 209 ms 8812 KB Output is correct
82 Correct 188 ms 8772 KB Output is correct
83 Correct 246 ms 8772 KB Output is correct
84 Correct 258 ms 8768 KB Output is correct
85 Correct 227 ms 8772 KB Output is correct
86 Correct 269 ms 8824 KB Output is correct
87 Correct 237 ms 8696 KB Output is correct
88 Correct 251 ms 8628 KB Output is correct
89 Correct 303 ms 8640 KB Output is correct
90 Correct 295 ms 8768 KB Output is correct
91 Correct 291 ms 8720 KB Output is correct
92 Correct 287 ms 8636 KB Output is correct