답안 #53455

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
53455 2018-06-30T05:06:45 Z 윤교준(#1420) Mecho (IOI09_mecho) C++11
100 / 100
245 ms 6556 KB
#include <bits/stdc++.h>
#define rf(x) (x)=0;while(*p<48)p++;while(47<*p)(x)=((x)<<3)+((x)<<1)+(*p++&15);
//#define rf(x) (x)=0;while(*p<48)im=*p=='-';while(47<*p)(x)=((x)<<3)+((x)<<1)+(*p++&15);if(im)(x)=-(x);
#define pb push_back
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define befv(V) ((V)[(sz(V)-2)])
#define sorv(V) sort(allv(V))
#define revv(V) reverse(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
#define clv(V) (V).clear()
#define upmin(a,b) (a)=min((a),(b))
#define upmax(a,b) (a)=max((a),(b))
#define rb(x) ((x)&(-(x)))
#define cb(x) (x)=(!(x))
#define INF (0x3f3f3f3f)
#define INFLL (0x3f3f3f3f3f3f3f3fll)
#define INFST (0x7f7f7f7f)
#define INFLLST (0x7f7f7f7f7f7f7f7fll)
using namespace std;
typedef long long ll;
typedef __int128_t lll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ld, ld> pdd;
typedef complex<ld> base;
const ld EPS = (ld)1e-7;
const ld PI = acos(0) * 2;
bool isZero(const ld& x) { return abs(x) <= EPS; }
int sign(const ld& x) { return isZero(x) ? 0 : (0 < x ? 1 : -1); }
ll gcd(ll a, ll b) { for(;b;a%=b,swap(a,b)){} return abs(a); }
pll operator + (const pll& a, const pll& b) { return pll(a.first+b.first, a.second+b.second); }
pll operator - (const pll& a, const pll& b) { return pll(a.first-b.first, a.second-b.second); }
pll operator * (const pll& a, const ll& b) { return pll(a.first*b, a.second*b); }
ll operator * (const pll& a, const pll& b) { return a.first*b.second - b.first*a.second; }
ll ccw(const pll& a, const pll& b, const pll& c) { return a*b + b*c + c*a; }
int rd(int s, int e) { return rand()%(e-s+1) + s; }
void fg(vector<int> G[], int a, int b) { G[a].pb(b); G[b].pb(a); }
void fg(vector<pii> G[], int a, int b, int c) { G[a].pb({b, c}); G[b].pb({a, c}); }
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

const int MAXN = 805;

int B[MAXN][MAXN], C[MAXN][MAXN];
char A[MAXN][MAXN];

int Sy, Sx, Ey, Ex;
int N, K;

bool f(int t) {
	if(B[Sy][Sx] <= t) return false;

	for(int i = 0; i <= N; i++)
		fill(C[i], C[i]+N+1, INF);
	
	C[Sy][Sx] = t * K;

	queue<pii> PQ;
	PQ.push(pii(Sy, Sx));

	for(int y, x, dst; !PQ.empty(); PQ.pop()) {
		tie(y, x) = PQ.front();
		dst = C[y][x] + 1;

		//printf("t=%d, dst=%d, %d %d\n", t, dst-1, y, x);

		for(int dr = 0; dr < 4; dr++) {
			int ny = y+dy[dr], nx = x+dx[dr];
			if('G' != A[ny][nx] && 'D' != A[ny][nx]) continue;
			if(C[ny][nx] <= dst) continue;
			if(B[ny][nx] < dst / K + 1) continue;
			C[ny][nx] = dst;
			PQ.push(pii(ny, nx));

			if(Ey == ny && Ex == nx)
				return true;
		}
	}
	
	return false;
}

int getAns() {
	int s = 0, e = N*N+55; for(int m; s < e;) {
		m = (s+e+1)/2;
		//printf("TRY %d : %d\n", m, f(m));
		if(f(m)) s = m;
		else e = m-1;
	}
	return s;
}

int main() {
	scanf("%d%d", &N, &K);
	for(int i = 1; i <= N; i++) {
		scanf(" %s", A[i]+1);
		for(int j = 1; j <= N; j++) {
			if('M' == A[i][j]) {
				Sy = i; Sx = j;
				A[i][j] = 'G';
			} else if('D' == A[i][j]) {
				Ey = i; Ex = j;
			}
		}
	}

	{
		for(int i = 1; i <= N; i++)
			fill(B[i], B[i]+N+1, INF);

		queue<pii> PQ;

		for(int i = 1; i <= N; i++) for(int j = 1; j <= N; j++)
			if('H' == A[i][j]) {
				PQ.push(pii(i, j));
				B[i][j] = 0;
			}

		for(int y, x, dst; !PQ.empty(); PQ.pop()) {
			tie(y, x) = PQ.front();
			dst = B[y][x] + 1;

			for(int dr = 0; dr < 4; dr++) {
				int ny = y+dy[dr], nx = x+dx[dr];
				if('G' != A[ny][nx] || B[ny][nx] <= dst) continue;
				B[ny][nx] = dst;
				PQ.push(pii(ny, nx));
			}
		}
	}

/*
	for(int i = 1; i <= N; i++) {
		for(int j = 1; j <= N; j++)
			printf("%d ", B[i][j]);
		puts("");
	}
*/

	if(!f(0)) {
		puts("-1");
		return 0;
	}

	cout << getAns() << endl;
	return 0;
}

Compilation message

mecho.cpp: In function 'int main()':
mecho.cpp:99:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &K);
  ~~~~~^~~~~~~~~~~~~~~~
mecho.cpp:101:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf(" %s", A[i]+1);
   ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 520 KB Output is correct
6 Correct 2 ms 536 KB Output is correct
7 Correct 110 ms 6424 KB Output is correct
8 Correct 2 ms 6424 KB Output is correct
9 Correct 2 ms 6424 KB Output is correct
10 Correct 2 ms 6424 KB Output is correct
11 Correct 2 ms 6424 KB Output is correct
12 Correct 2 ms 6424 KB Output is correct
13 Correct 3 ms 6424 KB Output is correct
14 Correct 2 ms 6424 KB Output is correct
15 Correct 2 ms 6424 KB Output is correct
16 Correct 2 ms 6424 KB Output is correct
17 Correct 2 ms 6424 KB Output is correct
18 Correct 2 ms 6424 KB Output is correct
19 Correct 2 ms 6424 KB Output is correct
20 Correct 2 ms 6424 KB Output is correct
21 Correct 2 ms 6424 KB Output is correct
22 Correct 3 ms 6424 KB Output is correct
23 Correct 3 ms 6424 KB Output is correct
24 Correct 3 ms 6424 KB Output is correct
25 Correct 2 ms 6424 KB Output is correct
26 Correct 3 ms 6424 KB Output is correct
27 Correct 2 ms 6424 KB Output is correct
28 Correct 2 ms 6424 KB Output is correct
29 Correct 2 ms 6424 KB Output is correct
30 Correct 2 ms 6424 KB Output is correct
31 Correct 3 ms 6424 KB Output is correct
32 Correct 4 ms 6424 KB Output is correct
33 Correct 6 ms 6424 KB Output is correct
34 Correct 6 ms 6424 KB Output is correct
35 Correct 23 ms 6424 KB Output is correct
36 Correct 8 ms 6424 KB Output is correct
37 Correct 7 ms 6424 KB Output is correct
38 Correct 29 ms 6424 KB Output is correct
39 Correct 13 ms 6424 KB Output is correct
40 Correct 10 ms 6424 KB Output is correct
41 Correct 56 ms 6424 KB Output is correct
42 Correct 10 ms 6424 KB Output is correct
43 Correct 10 ms 6424 KB Output is correct
44 Correct 47 ms 6424 KB Output is correct
45 Correct 12 ms 6424 KB Output is correct
46 Correct 16 ms 6424 KB Output is correct
47 Correct 55 ms 6424 KB Output is correct
48 Correct 13 ms 6424 KB Output is correct
49 Correct 13 ms 6424 KB Output is correct
50 Correct 70 ms 6424 KB Output is correct
51 Correct 15 ms 6424 KB Output is correct
52 Correct 15 ms 6424 KB Output is correct
53 Correct 90 ms 6424 KB Output is correct
54 Correct 17 ms 6424 KB Output is correct
55 Correct 16 ms 6424 KB Output is correct
56 Correct 99 ms 6424 KB Output is correct
57 Correct 26 ms 6424 KB Output is correct
58 Correct 23 ms 6424 KB Output is correct
59 Correct 130 ms 6424 KB Output is correct
60 Correct 21 ms 6424 KB Output is correct
61 Correct 22 ms 6424 KB Output is correct
62 Correct 157 ms 6424 KB Output is correct
63 Correct 172 ms 6424 KB Output is correct
64 Correct 245 ms 6424 KB Output is correct
65 Correct 175 ms 6424 KB Output is correct
66 Correct 152 ms 6424 KB Output is correct
67 Correct 37 ms 6424 KB Output is correct
68 Correct 53 ms 6424 KB Output is correct
69 Correct 52 ms 6424 KB Output is correct
70 Correct 49 ms 6424 KB Output is correct
71 Correct 46 ms 6424 KB Output is correct
72 Correct 35 ms 6424 KB Output is correct
73 Correct 42 ms 6424 KB Output is correct
74 Correct 70 ms 6424 KB Output is correct
75 Correct 78 ms 6500 KB Output is correct
76 Correct 73 ms 6500 KB Output is correct
77 Correct 95 ms 6500 KB Output is correct
78 Correct 31 ms 6500 KB Output is correct
79 Correct 65 ms 6500 KB Output is correct
80 Correct 70 ms 6500 KB Output is correct
81 Correct 77 ms 6500 KB Output is correct
82 Correct 67 ms 6500 KB Output is correct
83 Correct 94 ms 6500 KB Output is correct
84 Correct 122 ms 6500 KB Output is correct
85 Correct 117 ms 6500 KB Output is correct
86 Correct 96 ms 6556 KB Output is correct
87 Correct 90 ms 6556 KB Output is correct
88 Correct 94 ms 6556 KB Output is correct
89 Correct 113 ms 6556 KB Output is correct
90 Correct 108 ms 6556 KB Output is correct
91 Correct 103 ms 6556 KB Output is correct
92 Correct 127 ms 6556 KB Output is correct