Submission #408687

# Submission time Handle Problem Language Result Execution time Memory
408687 2021-05-19T13:35:09 Z Jasiekstrz Mecho (IOI09_mecho) C++17
84 / 100
435 ms 8932 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=INF;
	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 314 ms 8752 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 Incorrect 1 ms 716 KB Output isn't correct
13 Incorrect 1 ms 716 KB Output isn't correct
14 Correct 2 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 16 ms 3948 KB Output is correct
34 Correct 21 ms 3900 KB Output is correct
35 Correct 74 ms 3860 KB Output is correct
36 Correct 19 ms 4428 KB Output is correct
37 Correct 23 ms 4344 KB Output is correct
38 Correct 103 ms 4440 KB Output is correct
39 Correct 28 ms 4972 KB Output is correct
40 Correct 29 ms 4856 KB Output is correct
41 Correct 124 ms 4964 KB Output is correct
42 Correct 27 ms 5452 KB Output is correct
43 Correct 32 ms 5380 KB Output is correct
44 Correct 162 ms 5476 KB Output is correct
45 Correct 34 ms 6000 KB Output is correct
46 Correct 42 ms 5968 KB Output is correct
47 Correct 200 ms 5992 KB Output is correct
48 Correct 42 ms 6512 KB Output is correct
49 Correct 51 ms 6476 KB Output is correct
50 Correct 233 ms 6428 KB Output is correct
51 Correct 49 ms 6992 KB Output is correct
52 Correct 61 ms 6992 KB Output is correct
53 Correct 292 ms 7120 KB Output is correct
54 Correct 60 ms 7452 KB Output is correct
55 Correct 75 ms 7460 KB Output is correct
56 Correct 333 ms 7536 KB Output is correct
57 Correct 65 ms 8044 KB Output is correct
58 Correct 70 ms 8032 KB Output is correct
59 Correct 395 ms 8052 KB Output is correct
60 Correct 67 ms 8516 KB Output is correct
61 Correct 82 ms 8456 KB Output is correct
62 Correct 435 ms 8560 KB Output is correct
63 Correct 217 ms 8560 KB Output is correct
64 Correct 368 ms 8564 KB Output is correct
65 Correct 380 ms 8468 KB Output is correct
66 Correct 261 ms 8608 KB Output is correct
67 Correct 67 ms 8560 KB Output is correct
68 Correct 113 ms 8588 KB Output is correct
69 Correct 117 ms 8516 KB Output is correct
70 Correct 117 ms 8588 KB Output is correct
71 Correct 83 ms 8488 KB Output is correct
72 Incorrect 87 ms 8524 KB Output isn't correct
73 Incorrect 144 ms 8836 KB Output isn't correct
74 Correct 246 ms 8840 KB Output is correct
75 Correct 253 ms 8836 KB Output is correct
76 Correct 206 ms 8836 KB Output is correct
77 Correct 253 ms 8836 KB Output is correct
78 Correct 82 ms 8780 KB Output is correct
79 Correct 213 ms 8812 KB Output is correct
80 Correct 211 ms 8728 KB Output is correct
81 Correct 214 ms 8932 KB Output is correct
82 Correct 189 ms 8732 KB Output is correct
83 Correct 251 ms 8716 KB Output is correct
84 Correct 285 ms 8676 KB Output is correct
85 Correct 245 ms 8776 KB Output is correct
86 Correct 270 ms 8772 KB Output is correct
87 Correct 266 ms 8772 KB Output is correct
88 Correct 267 ms 8636 KB Output is correct
89 Correct 333 ms 8724 KB Output is correct
90 Correct 302 ms 8720 KB Output is correct
91 Correct 302 ms 8640 KB Output is correct
92 Correct 286 ms 8716 KB Output is correct