답안 #233152

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
233152 2020-05-19T15:02:42 Z anubhavdhar Mecho (IOI09_mecho) C++14
76 / 100
218 ms 7800 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;
	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] == 'M'))
			{
				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'))
				{
					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));
				}
		}
		bool found = false;
		for(pair<int, int> p : {mp(0, -1), mp(0, 1), mp(1, 0), mp(-1, 0)})
				if(inside(H	+ p.ff, K+p.ss)&&(disM[H + p.ff][K + p.ss] != inf))
					lo = mid, found = true;
		if(!found)	hi = 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));
			}
	}

	bool found = false;
	for(pair<int, int> p : {mp(0, -1), mp(0, 1), mp(1, 0), mp(-1, 0)})
			if(inside(H	+ p.ff, K+p.ss)&&(disM[H + p.ff][K + p.ss] != inf))
				found = true;
	// F0R(i, N)	
	// {
	// 	F0R(j, N)
	// 		if(disM[i][j] == inf) cout<<'-' ;
	// 		else cout<<disM[i][j];
	// 	cout<<' '<<'\n';
	// }



	cout<<((!found) ? -1 : lo );

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 111 ms 7544 KB Output is correct
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 4 ms 512 KB Output is correct
12 Correct 5 ms 896 KB Output is correct
13 Correct 5 ms 768 KB Output is correct
14 Correct 5 ms 896 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
16 Correct 5 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 768 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 6 ms 896 KB Output is correct
27 Correct 5 ms 896 KB Output is correct
28 Correct 5 ms 896 KB Output is correct
29 Correct 5 ms 896 KB Output is correct
30 Correct 5 ms 896 KB Output is correct
31 Correct 5 ms 1024 KB Output is correct
32 Correct 6 ms 1024 KB Output is correct
33 Incorrect 21 ms 3456 KB Output isn't correct
34 Incorrect 27 ms 3456 KB Output isn't correct
35 Correct 30 ms 3456 KB Output is correct
36 Incorrect 27 ms 3960 KB Output isn't correct
37 Incorrect 33 ms 3840 KB Output isn't correct
38 Correct 38 ms 3840 KB Output is correct
39 Incorrect 32 ms 4352 KB Output isn't correct
40 Incorrect 41 ms 4352 KB Output isn't correct
41 Correct 49 ms 4352 KB Output is correct
42 Incorrect 38 ms 4856 KB Output isn't correct
43 Incorrect 49 ms 4736 KB Output isn't correct
44 Correct 58 ms 4736 KB Output is correct
45 Incorrect 46 ms 5240 KB Output isn't correct
46 Incorrect 62 ms 5248 KB Output isn't correct
47 Correct 74 ms 5248 KB Output is correct
48 Incorrect 57 ms 5684 KB Output isn't correct
49 Incorrect 73 ms 5632 KB Output isn't correct
50 Correct 87 ms 5632 KB Output is correct
51 Incorrect 62 ms 6016 KB Output isn't correct
52 Incorrect 85 ms 6272 KB Output isn't correct
53 Correct 104 ms 6136 KB Output is correct
54 Incorrect 71 ms 6528 KB Output isn't correct
55 Incorrect 94 ms 6528 KB Output isn't correct
56 Correct 128 ms 6528 KB Output is correct
57 Incorrect 81 ms 6912 KB Output isn't correct
58 Incorrect 112 ms 6912 KB Output isn't correct
59 Correct 134 ms 6904 KB Output is correct
60 Incorrect 91 ms 7424 KB Output isn't correct
61 Incorrect 121 ms 7424 KB Output isn't correct
62 Correct 164 ms 7544 KB Output is correct
63 Correct 130 ms 7416 KB Output is correct
64 Correct 218 ms 7416 KB Output is correct
65 Correct 191 ms 7424 KB Output is correct
66 Correct 177 ms 7416 KB Output is correct
67 Correct 138 ms 7396 KB Output is correct
68 Correct 68 ms 7416 KB Output is correct
69 Correct 74 ms 7416 KB Output is correct
70 Correct 67 ms 7416 KB Output is correct
71 Correct 59 ms 7544 KB Output is correct
72 Correct 61 ms 7416 KB Output is correct
73 Correct 60 ms 7680 KB Output is correct
74 Correct 99 ms 7672 KB Output is correct
75 Correct 91 ms 7672 KB Output is correct
76 Correct 83 ms 7800 KB Output is correct
77 Correct 91 ms 7672 KB Output is correct
78 Correct 94 ms 7680 KB Output is correct
79 Correct 89 ms 7700 KB Output is correct
80 Correct 87 ms 7672 KB Output is correct
81 Correct 99 ms 7676 KB Output is correct
82 Correct 93 ms 7704 KB Output is correct
83 Correct 95 ms 7552 KB Output is correct
84 Correct 107 ms 7544 KB Output is correct
85 Correct 94 ms 7552 KB Output is correct
86 Correct 106 ms 7672 KB Output is correct
87 Correct 102 ms 7544 KB Output is correct
88 Correct 96 ms 7552 KB Output is correct
89 Correct 121 ms 7544 KB Output is correct
90 Correct 112 ms 7544 KB Output is correct
91 Correct 109 ms 7544 KB Output is correct
92 Correct 107 ms 7544 KB Output is correct