Submission #233148

# Submission time Handle Problem Language Result Execution time Memory
233148 2020-05-19T14:42:43 Z anubhavdhar Mecho (IOI09_mecho) C++14
25 / 100
6 ms 896 KB
#include<bits/stdc++.h>

#define ll long long int
#define pb push_back
#define mp make_pair
#define FOR(i,n) for(i=0;i<(n);++i)
#define FORe(i,n) for(i=1;i<=(n);++i)
#define FORr(i,a,b) for(i=(a);i<(b);++i)
#define FORrev(i,n) for(i=(n);i>=0;--i)
#define F0R(i,n) for(int i=0;i<(n);++i)
#define F0Re(i,n) for(int i=1;i<=(n);++i)
#define F0Rr(i,a,b) for(ll i=(a);i<(b);++i)
#define F0Rrev(i,n) for(int i=(n);i>=0;--i)
#define ii pair<ll,ll>
#define vi vector<ll>
#define vii vector<ii>
#define ff first 
#define ss second
#define cd complex<double>
#define vcd vector<cd>
#define ldd long double
#define dbgLine cout<<"Line : "<<__LINE__<<'\n'
#define all(x) (x).begin(),(x).end()

using namespace std;

const short int __PRECISION = 10;

const ll MOD = 1e9+7;
const ll INF = 1e17 + 1101;
const ll LOGN = 17;
const ll MAXN = 2e5+5;
const ll ROOTN = 320;

const int inf = 1e9+2;

const ldd PI = acos(-1);
const ldd EPS = 1e-7;

int N, S, disB[900][900], disM[900][900], X, Y, H, K;
char grid[900][900];
bool vis[900][900];

inline bool inside(int i, int j)
{
	return ((i < N) && (i >= 0) && (j < N) && (j >= 0));
}

int main()
{
	
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	

	cin>>N>>S;
  	assert(N < 61);
	queue<pair<int, int>> Q;

	F0R(i, N)
		F0R(j ,N)
		{
			cin>>grid[i][j];
			vis[i][j] = false;
			disB[i][j] = inf;
			if(grid[i][j] == 'H')
				Q.push(mp(i, j)), disB[i][j] = 0, vis[i][j] = true;
			else if(grid[i][j] == 'M')
				X = i, Y = j;
			else if(grid[i][j] == 'D')
				H = i, K = j;
		}

	while(!Q.empty())
	{
		int x = Q.front().ff, y = Q.front().ss;
		Q.pop();
		for(pair<int, int> p : {mp(0, -1), mp(0, 1), mp(1, 0), mp(-1, 0)})
			if(inside(x+p.ff, y+p.ss) && (!vis[x+p.ff][y+p.ss]) && (grid[x+p.ff][y+p.ss] == 'G' or grid[x+p.ff][y+p.ss] == 'D'))
			{
				disB[x+p.ff][y+p.ss] = disB[x][y] + 1;
				vis[x+p.ff][y+p.ss] = true;
				Q.push(mp(x+p.ff, y+p.ss));
			}
	}

	int lo = 0, hi = 4000, mid;

	// F0R(i, N)
	// {
	// 	F0R(j, N)
	// 		if(disB[i][j] == inf) cout<<'-';
	// 		else	cout<<disB[i][j];
	// 	cout<<'\n';
	// }


	while(lo < hi - 1)
	{
		mid = (lo + hi)/2;
		// cout<<"mid = "<<mid<<'\n';
		F0R(i, N)
			F0R(j, N)
				vis[i][j] = false, disM[i][j] = inf;
		vis[X][Y] = true;
		disM[X][Y] = 0;
		Q.push(mp(X, Y));
		while(!Q.empty())
		{
			int x = Q.front().ff, y = Q.front().ss;
			Q.pop();
			// cout<<"disM["<<x<<"]["<<y<<"] = "<<disM[x][y]<<'\n';
			if (disB[x][y] <= mid + (disM[x][y])/S)
			{
				disM[x][y] = inf;
				// cout<<x<<','<<y<<" is unreachable\n";
				continue;
			}
			for(pair<int, int> p : {mp(0, -1), mp(0, 1), mp(1, 0), mp(-1, 0)})
				if(inside(x+p.ff, y+p.ss)&&(!vis[x+p.ff][y+p.ss])&&(grid[x+p.ff][y+p.ss] == 'G' or grid[x+p.ff][y+p.ss] == 'D'))
				{
					disM[x+p.ff][y+p.ss] = disM[x][y] + 1;
					vis[x+p.ff][y+p.ss] = true;
					Q.push(mp(x+p.ff, y+p.ss));
				}
		}
		if(disM[H][K] == inf)	hi = mid;
		else	lo = mid;
		// F0R(i, N)	
		// {
		// 	F0R(j, N)
		// 		if(disM[i][j] == inf) cout<<'-' ;
		// 		else cout<<disM[i][j];
		// 	cout<<'\n';
		// }
	}
	mid = 0;//(lo + hi)/2;
	// cout<<"mid = "<<mid<<'\n';
	F0R(i, N)
		F0R(j, N)
			vis[i][j] = false, disM[i][j] = inf;
	vis[X][Y] = true;
	disM[X][Y] = 0;
	Q.push(mp(X, Y));
	while(!Q.empty())
	{
		int x = Q.front().ff, y = Q.front().ss;
		Q.pop();
		// cout<<"disM["<<x<<"]["<<y<<"] = "<<disM[x][y]<<'\n';
		if (disB[x][y] <= mid + (disM[x][y])/S)
		{
			disM[x][y] = inf;
			// cout<<x<<','<<y<<" is unreachable\n";
			continue;
		}
		// else
		// {
		// 	cout<<"doable as "<<disB[x][y]<<" >= "<<mid<<"+"<<disM[x][y]<<'/'<<S<<" = "<<mid + disM[x][y]/S<<'\n';
		// }
		for(pair<int, int> p : {mp(0, -1), mp(0, 1), mp(1, 0), mp(-1, 0)})
			if(inside(x+p.ff, y+p.ss)&&(!vis[x+p.ff][y+p.ss])&&(grid[x+p.ff][y+p.ss] == 'G' or grid[x+p.ff][y+p.ss] == 'D'))
			{
				disM[x+p.ff][y+p.ss] = disM[x][y] + 1;
				vis[x+p.ff][y+p.ss] = true;
				Q.push(mp(x+p.ff, y+p.ss));
			}
	}
	// F0R(i, N)	
	// {
	// 	F0R(j, N)
	// 		if(disM[i][j] == inf) cout<<'-' ;
	// 		else cout<<disM[i][j];
	// 	cout<<' '<<'\n';
	// }



	cout<<((disM[H][K] == inf) ? -1 : lo );

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Incorrect 4 ms 384 KB Output isn't correct
3 Correct 5 ms 384 KB Output is correct
4 Incorrect 4 ms 384 KB Output isn't correct
5 Correct 4 ms 384 KB Output is correct
6 Incorrect 4 ms 384 KB Output isn't correct
7 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
8 Correct 5 ms 512 KB Output is correct
9 Correct 5 ms 512 KB Output is correct
10 Correct 5 ms 512 KB Output is correct
11 Correct 5 ms 512 KB Output is correct
12 Incorrect 5 ms 896 KB Output isn't correct
13 Incorrect 5 ms 768 KB Output isn't correct
14 Correct 5 ms 896 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
16 Correct 4 ms 512 KB Output is correct
17 Correct 5 ms 512 KB Output is correct
18 Correct 5 ms 512 KB Output is correct
19 Correct 5 ms 640 KB Output is correct
20 Correct 5 ms 640 KB Output is correct
21 Correct 5 ms 640 KB Output is correct
22 Correct 5 ms 640 KB Output is correct
23 Correct 5 ms 768 KB Output is correct
24 Correct 5 ms 768 KB Output is correct
25 Correct 5 ms 896 KB Output is correct
26 Correct 5 ms 896 KB Output is correct
27 Runtime error 5 ms 640 KB Execution killed with signal 11 (could be triggered by violating memory limits)
28 Runtime error 6 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
29 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
30 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
31 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
32 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
33 Runtime error 6 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
34 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
35 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
36 Runtime error 5 ms 640 KB Execution killed with signal 11 (could be triggered by violating memory limits)
37 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
38 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
39 Runtime error 5 ms 640 KB Execution killed with signal 11 (could be triggered by violating memory limits)
40 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
41 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
42 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
43 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
44 Runtime error 5 ms 640 KB Execution killed with signal 11 (could be triggered by violating memory limits)
45 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
46 Runtime error 5 ms 640 KB Execution killed with signal 11 (could be triggered by violating memory limits)
47 Runtime error 6 ms 688 KB Execution killed with signal 11 (could be triggered by violating memory limits)
48 Runtime error 6 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
49 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
50 Runtime error 6 ms 640 KB Execution killed with signal 11 (could be triggered by violating memory limits)
51 Runtime error 5 ms 640 KB Execution killed with signal 11 (could be triggered by violating memory limits)
52 Runtime error 5 ms 640 KB Execution killed with signal 11 (could be triggered by violating memory limits)
53 Runtime error 5 ms 640 KB Execution killed with signal 11 (could be triggered by violating memory limits)
54 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
55 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
56 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
57 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
58 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
59 Runtime error 6 ms 640 KB Execution killed with signal 11 (could be triggered by violating memory limits)
60 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
61 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
62 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
63 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
64 Runtime error 6 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
65 Runtime error 6 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
66 Runtime error 6 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
67 Runtime error 6 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
68 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
69 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
70 Runtime error 5 ms 640 KB Execution killed with signal 11 (could be triggered by violating memory limits)
71 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
72 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
73 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
74 Runtime error 6 ms 640 KB Execution killed with signal 11 (could be triggered by violating memory limits)
75 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
76 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
77 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
78 Runtime error 6 ms 640 KB Execution killed with signal 11 (could be triggered by violating memory limits)
79 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
80 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
81 Runtime error 6 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
82 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
83 Runtime error 6 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
84 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
85 Runtime error 5 ms 640 KB Execution killed with signal 11 (could be triggered by violating memory limits)
86 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
87 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
88 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
89 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
90 Runtime error 6 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
91 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
92 Runtime error 5 ms 640 KB Execution killed with signal 11 (could be triggered by violating memory limits)