Submission #138452

# Submission time Handle Problem Language Result Execution time Memory
138452 2019-07-30T01:55:18 Z baluteshih Mecho (IOI09_mecho) C++14
100 / 100
279 ms 7160 KB
#include <bits/stdc++.h>
#define pb push_back
#define jizz ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define ET cout << "\n"
#define F first
#define S second
#define MP make_pair
#define ALL(v) v.begin(),v.end()
#define MEM memset(i,j,sizeof i)
#define DB(a,s,e) {for(int i=s;i<e;++i) cout << a[i] << " ";ET;}
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const int INF=1e9;
int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0},n,s;
string mp[805];
int ti[805][805],vis[805][805];
queue<pii> q;
pii st;

bool check(int x,int y)
{
	return !(x<0||y<0||x>=n||y>=n||vis[x][y]||mp[x][y]=='T');
}

bool check2(int x,int y)
{
	return !(x<0||y<0||x>=n||y>=n||vis[x][y]!=INF||mp[x][y]=='T');
}

int tran(int nw,int beg)
{
	if(nw==beg) return beg;
	return (nw-beg-1)/s+beg+1;
}

bool running(int m)
{
	queue<pii> q;
	for(int i=0;i<n;++i)
		fill(vis[i],vis[i]+n,INF);
	for(q.push(MP(st.F,st.S)),vis[st.F][st.S]=m;!q.empty();)
	{
		auto t=q.front();
		q.pop();
		if(tran(vis[t.F][t.S],m)>ti[t.F][t.S]) continue;
		if(tran(vis[t.F][t.S],m)==ti[t.F][t.S]&&(vis[t.F][t.S]-m)%s==0) continue;
		if(mp[t.F][t.S]=='D') return 1;
		for(int i=0;i<4;++i)
			if(check2(t.F+dx[i],t.S+dy[i]))
				q.push(MP(t.F+dx[i],t.S+dy[i])),vis[t.F+dx[i]][t.S+dy[i]]=vis[t.F][t.S]+1;
	}
	return 0;
}

int main()
{jizz
	int l=-1,r;
	cin >> n >> s;
	for(int i=0;i<n;++i)
		cin >> mp[i];
	for(int i=0;i<n;++i)
		for(int j=0;j<n;++j)
			ti[i][j]=INF;
	for(int i=0;i<n;++i)
		for(int j=0;j<n;++j)
			if(mp[i][j]=='H')
				q.push(MP(i,j)),vis[i][j]=1,ti[i][j]=0;
			else if(mp[i][j]=='T'||mp[i][j]=='D')
				vis[i][j]=1;
			else if(mp[i][j]=='M')
				st=MP(i,j);
	while(!q.empty())
	{
		auto t=q.front();
		q.pop();
		for(int i=0;i<4;++i)
			if(check(t.F+dx[i],t.S+dy[i]))
				q.push(MP(t.F+dx[i],t.S+dy[i])),vis[t.F+dx[i]][t.S+dy[i]]=1,ti[t.F+dx[i]][t.S+dy[i]]=ti[t.F][t.S]+1;
	}
	for(r=ti[st.F][st.S];r-l>1;)
	{
		int m=(l+r)/2;
		if(running(m)) l=m;
		else r=m;
	}
	cout << l << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 137 ms 6980 KB Output is correct
8 Correct 2 ms 504 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 2 ms 408 KB Output is correct
12 Correct 3 ms 760 KB Output is correct
13 Correct 2 ms 632 KB Output is correct
14 Correct 3 ms 760 KB Output is correct
15 Correct 2 ms 504 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
17 Correct 2 ms 504 KB Output is correct
18 Correct 2 ms 504 KB Output is correct
19 Correct 2 ms 632 KB Output is correct
20 Correct 2 ms 504 KB Output is correct
21 Correct 3 ms 632 KB Output is correct
22 Correct 2 ms 632 KB Output is correct
23 Correct 2 ms 632 KB Output is correct
24 Correct 3 ms 632 KB Output is correct
25 Correct 3 ms 760 KB Output is correct
26 Correct 3 ms 760 KB Output is correct
27 Correct 3 ms 760 KB Output is correct
28 Correct 3 ms 760 KB Output is correct
29 Correct 3 ms 760 KB Output is correct
30 Correct 3 ms 888 KB Output is correct
31 Correct 3 ms 892 KB Output is correct
32 Correct 3 ms 888 KB Output is correct
33 Correct 8 ms 2808 KB Output is correct
34 Correct 7 ms 2808 KB Output is correct
35 Correct 32 ms 2936 KB Output is correct
36 Correct 9 ms 3240 KB Output is correct
37 Correct 9 ms 3192 KB Output is correct
38 Correct 41 ms 3192 KB Output is correct
39 Correct 10 ms 3576 KB Output is correct
40 Correct 11 ms 3576 KB Output is correct
41 Correct 56 ms 3704 KB Output is correct
42 Correct 12 ms 4088 KB Output is correct
43 Correct 13 ms 3960 KB Output is correct
44 Correct 83 ms 4084 KB Output is correct
45 Correct 15 ms 4472 KB Output is correct
46 Correct 16 ms 4472 KB Output is correct
47 Correct 94 ms 4528 KB Output is correct
48 Correct 16 ms 4856 KB Output is correct
49 Correct 16 ms 4856 KB Output is correct
50 Correct 104 ms 4984 KB Output is correct
51 Correct 17 ms 5368 KB Output is correct
52 Correct 22 ms 5344 KB Output is correct
53 Correct 146 ms 5496 KB Output is correct
54 Correct 21 ms 5752 KB Output is correct
55 Correct 20 ms 5752 KB Output is correct
56 Correct 175 ms 5752 KB Output is correct
57 Correct 23 ms 6264 KB Output is correct
58 Correct 24 ms 6264 KB Output is correct
59 Correct 194 ms 6392 KB Output is correct
60 Correct 27 ms 6776 KB Output is correct
61 Correct 25 ms 6648 KB Output is correct
62 Correct 207 ms 6748 KB Output is correct
63 Correct 163 ms 6776 KB Output is correct
64 Correct 265 ms 6876 KB Output is correct
65 Correct 279 ms 6780 KB Output is correct
66 Correct 217 ms 6776 KB Output is correct
67 Correct 189 ms 6748 KB Output is correct
68 Correct 70 ms 6648 KB Output is correct
69 Correct 59 ms 6652 KB Output is correct
70 Correct 56 ms 6776 KB Output is correct
71 Correct 54 ms 6652 KB Output is correct
72 Correct 44 ms 6648 KB Output is correct
73 Correct 56 ms 6908 KB Output is correct
74 Correct 108 ms 7088 KB Output is correct
75 Correct 115 ms 7032 KB Output is correct
76 Correct 99 ms 7160 KB Output is correct
77 Correct 103 ms 7160 KB Output is correct
78 Correct 112 ms 6904 KB Output is correct
79 Correct 91 ms 6996 KB Output is correct
80 Correct 100 ms 6904 KB Output is correct
81 Correct 113 ms 7048 KB Output is correct
82 Correct 107 ms 7032 KB Output is correct
83 Correct 151 ms 7028 KB Output is correct
84 Correct 105 ms 6904 KB Output is correct
85 Correct 111 ms 7036 KB Output is correct
86 Correct 129 ms 7032 KB Output is correct
87 Correct 115 ms 6948 KB Output is correct
88 Correct 161 ms 6900 KB Output is correct
89 Correct 154 ms 6904 KB Output is correct
90 Correct 151 ms 6940 KB Output is correct
91 Correct 143 ms 6820 KB Output is correct
92 Correct 142 ms 6904 KB Output is correct