답안 #124692

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
124692 2019-07-03T17:28:21 Z arthurconmy Mecho (IOI09_mecho) C++14
100 / 100
384 ms 7852 KB
// check ctrl+v :^)
/* Arthur Conmy IOI template - minimal! */
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <bitset>
#include <random>
#include <stack>
#include <deque>
#include <chrono>
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int,int>;
#define ff first
#define ss second
#define pb push_back
#define REP(i,a,b) \
for(int i = int(a); i<=int(b); i++)

int grid[802][802]; // 0: tree / boundary
					// 1: grass 
					// 2: hive
					// 3: mecho's home

int bee_dis[802][802];
int INF = int(1e9);
pii start;
int n,s;
bool vis[802][802];

bool try_it(int t)
{
	vector<pii> BFS = {start};

	int cur_dis=0;

	while(!BFS.empty())
	{
		// cout << cur_dis << endl;

		if(cur_dis%s == 0 && cur_dis!=0) t++;

		vector<pii> new_BFS;

		for(auto P:BFS)
		{
			if(bee_dis[P.ff][P.ss]<=t) continue;

			REP(x,-1,1)
			{
				REP(y,-1,1)
				{
					// cout << P.ff+x << " " << P.ff+y << endl;

					if(vis[P.ff+x][P.ss+y]) continue;
					if(abs(x)+abs(y)!=1) continue;
					if(bee_dis[P.ff+x][P.ss+y] <= t) continue; // do we need to check that the current square isn't now bad?
						
					if(grid[P.ff+x][P.ss+y] == 3) return true;

					if(grid[P.ff+x][P.ss+y] != 1) continue;

					new_BFS.pb({P.ff+x,P.ss+y});
					vis[P.ff+x][P.ss+y]=1;
				}
			}
		}

		BFS=new_BFS;
		cur_dis++;
	}

	return false;
}

int main()
{
	#ifdef ARTHUR_LOCAL
		ifstream cin("input.txt");
	#endif

	REP(i,0,801)
	{
		REP(j,0,801)
		{
			bee_dis[i][j]=INF;
		}
	}

	cin>>n>>s;

	vector<pii> bees;

	REP(i,1,n)
	{
		string row;
		cin>>row;

		REP(j,1,n)
		{
			if(row[j-1]=='G') grid[i][j]=1;
			if(row[j-1]=='H')
			{
				grid[i][j]=2;
				bees.pb({i,j});
				bee_dis[i][j]=0;
			}
			if(row[j-1]=='M')
			{
				grid[i][j]=1; // Mecho's start is grassy
				start={i,j};
			}
			if(row[j-1]=='D')
			{
				grid[i][j]=3;
			}
		}
	}

	// precompute bee_dis

	while(!bees.empty())
	{
		vector<pii> new_bees;

		for(auto b:bees)
		{
			REP(x,-1,1)
			{
				REP(y,-1,1)
				{
					if(abs(x)+abs(y)!=1) continue;
					if(bee_dis[b.ff+x][b.ss+y] != INF) continue;
					if(grid[b.ff+x][b.ss+y] != 1) continue;

					bee_dis[b.ff+x][b.ss+y]=1+bee_dis[b.ff][b.ss];
					new_bees.pb({b.ff+x,b.ss+y});
				}
			}
		}

		bees=new_bees;
	}

	// REP(i,1,n)
	// {
	// 	REP(j,1,n)
	// 	{
	// 		if(bee_dis[i][j]==INF)
	// 		{
	// 			cout << "-1 ";
	// 			continue;
	// 		}

	// 		cout << bee_dis[i][j] << " ";
	// 		if(bee_dis[i][j]<10) cout << " "; 
	// 	}
	// 	cout << endl;
	// }

	vis[start.ff][start.ss]=1;
	int l=0;
	int r=n*n; // bin search parameters

	while(l<r)
	{
		// cout << l << " " << r << endl;

		REP(i,0,801)
		{
			REP(j,0,801)
			{
				vis[i][j]=0;
			}
		}
		vis[start.ff][start.ss]=1;

		int mid=(l+r)/2;

		if(try_it(mid))
		{
			if(l==mid)
			{
				REP(i,0,801)
				{
					REP(j,0,801)
					{
						vis[i][j]=0;
					}
				}
				vis[start.ff][start.ss]=1;
			
				if(try_it(r)) l=r;
				else r=l;
			}
			
			else l=mid;
		}

		else
		{
			r=mid-1;
		}
	}

	REP(i,0,801)
	{
		REP(j,0,801)
		{
			vis[i][j]=0;
		}
	}
	vis[start.ff][start.ss]=1;

	if(!try_it(0)) cout << -1 << endl;
	else cout << l << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3448 KB Output is correct
2 Correct 6 ms 3448 KB Output is correct
3 Correct 6 ms 3448 KB Output is correct
4 Correct 7 ms 3448 KB Output is correct
5 Correct 6 ms 3576 KB Output is correct
6 Correct 7 ms 3576 KB Output is correct
7 Correct 208 ms 7072 KB Output is correct
8 Correct 7 ms 3576 KB Output is correct
9 Correct 8 ms 3448 KB Output is correct
10 Correct 8 ms 3448 KB Output is correct
11 Correct 7 ms 3576 KB Output is correct
12 Correct 9 ms 3624 KB Output is correct
13 Correct 8 ms 3576 KB Output is correct
14 Correct 9 ms 3704 KB Output is correct
15 Correct 8 ms 3576 KB Output is correct
16 Correct 8 ms 3576 KB Output is correct
17 Correct 8 ms 3532 KB Output is correct
18 Correct 8 ms 3592 KB Output is correct
19 Correct 9 ms 3576 KB Output is correct
20 Correct 8 ms 3580 KB Output is correct
21 Correct 9 ms 3576 KB Output is correct
22 Correct 9 ms 3692 KB Output is correct
23 Correct 9 ms 3576 KB Output is correct
24 Correct 9 ms 3576 KB Output is correct
25 Correct 9 ms 3704 KB Output is correct
26 Correct 10 ms 3744 KB Output is correct
27 Correct 10 ms 3704 KB Output is correct
28 Correct 9 ms 3704 KB Output is correct
29 Correct 10 ms 3704 KB Output is correct
30 Correct 10 ms 3704 KB Output is correct
31 Correct 10 ms 3824 KB Output is correct
32 Correct 10 ms 3704 KB Output is correct
33 Correct 23 ms 4728 KB Output is correct
34 Correct 24 ms 4728 KB Output is correct
35 Correct 58 ms 4728 KB Output is correct
36 Correct 27 ms 4856 KB Output is correct
37 Correct 28 ms 4856 KB Output is correct
38 Correct 69 ms 4984 KB Output is correct
39 Correct 31 ms 5068 KB Output is correct
40 Correct 33 ms 5112 KB Output is correct
41 Correct 90 ms 5240 KB Output is correct
42 Correct 36 ms 5240 KB Output is correct
43 Correct 37 ms 5240 KB Output is correct
44 Correct 122 ms 5368 KB Output is correct
45 Correct 41 ms 5496 KB Output is correct
46 Correct 44 ms 5624 KB Output is correct
47 Correct 142 ms 5624 KB Output is correct
48 Correct 48 ms 5752 KB Output is correct
49 Correct 49 ms 5752 KB Output is correct
50 Correct 162 ms 5772 KB Output is correct
51 Correct 53 ms 5880 KB Output is correct
52 Correct 56 ms 5880 KB Output is correct
53 Correct 180 ms 5880 KB Output is correct
54 Correct 60 ms 6136 KB Output is correct
55 Correct 61 ms 6136 KB Output is correct
56 Correct 241 ms 6192 KB Output is correct
57 Correct 67 ms 6392 KB Output is correct
58 Correct 70 ms 6264 KB Output is correct
59 Correct 256 ms 6396 KB Output is correct
60 Correct 74 ms 6520 KB Output is correct
61 Correct 78 ms 6492 KB Output is correct
62 Correct 306 ms 6520 KB Output is correct
63 Correct 241 ms 6648 KB Output is correct
64 Correct 329 ms 6648 KB Output is correct
65 Correct 384 ms 6636 KB Output is correct
66 Correct 328 ms 6520 KB Output is correct
67 Correct 242 ms 6520 KB Output is correct
68 Correct 119 ms 6652 KB Output is correct
69 Correct 147 ms 6648 KB Output is correct
70 Correct 116 ms 6648 KB Output is correct
71 Correct 121 ms 6904 KB Output is correct
72 Correct 90 ms 6648 KB Output is correct
73 Correct 113 ms 7716 KB Output is correct
74 Correct 159 ms 7704 KB Output is correct
75 Correct 156 ms 7708 KB Output is correct
76 Correct 147 ms 7744 KB Output is correct
77 Correct 138 ms 7852 KB Output is correct
78 Correct 146 ms 7404 KB Output is correct
79 Correct 131 ms 7460 KB Output is correct
80 Correct 142 ms 7492 KB Output is correct
81 Correct 143 ms 7296 KB Output is correct
82 Correct 137 ms 7384 KB Output is correct
83 Correct 152 ms 7272 KB Output is correct
84 Correct 161 ms 7244 KB Output is correct
85 Correct 182 ms 7244 KB Output is correct
86 Correct 168 ms 7384 KB Output is correct
87 Correct 160 ms 7368 KB Output is correct
88 Correct 159 ms 7412 KB Output is correct
89 Correct 170 ms 7328 KB Output is correct
90 Correct 199 ms 7508 KB Output is correct
91 Correct 196 ms 7304 KB Output is correct
92 Correct 205 ms 7412 KB Output is correct