답안 #53454

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
53454 2018-06-30T05:01:52 Z 윤교준(#1420) Mecho (IOI09_mecho) C++11
54 / 100
234 ms 6552 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) / K) 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 Incorrect 2 ms 376 KB Output isn't correct
2 Incorrect 2 ms 376 KB Output isn't correct
3 Incorrect 2 ms 428 KB Output isn't correct
4 Incorrect 2 ms 508 KB Output isn't correct
5 Correct 2 ms 560 KB Output is correct
6 Correct 2 ms 560 KB Output is correct
7 Correct 106 ms 6320 KB Output is correct
8 Incorrect 2 ms 6320 KB Output isn't correct
9 Correct 2 ms 6320 KB Output is correct
10 Correct 2 ms 6320 KB Output is correct
11 Correct 2 ms 6320 KB Output is correct
12 Correct 2 ms 6320 KB Output is correct
13 Correct 2 ms 6320 KB Output is correct
14 Correct 2 ms 6320 KB Output is correct
15 Incorrect 2 ms 6320 KB Output isn't correct
16 Correct 2 ms 6320 KB Output is correct
17 Incorrect 2 ms 6320 KB Output isn't correct
18 Correct 2 ms 6320 KB Output is correct
19 Incorrect 2 ms 6320 KB Output isn't correct
20 Correct 2 ms 6320 KB Output is correct
21 Incorrect 2 ms 6320 KB Output isn't correct
22 Correct 2 ms 6320 KB Output is correct
23 Incorrect 2 ms 6320 KB Output isn't correct
24 Correct 3 ms 6320 KB Output is correct
25 Incorrect 2 ms 6320 KB Output isn't correct
26 Correct 2 ms 6320 KB Output is correct
27 Incorrect 2 ms 6320 KB Output isn't correct
28 Correct 2 ms 6320 KB Output is correct
29 Incorrect 2 ms 6320 KB Output isn't correct
30 Correct 2 ms 6320 KB Output is correct
31 Incorrect 2 ms 6320 KB Output isn't correct
32 Correct 2 ms 6320 KB Output is correct
33 Incorrect 6 ms 6320 KB Output isn't correct
34 Correct 7 ms 6320 KB Output is correct
35 Correct 23 ms 6320 KB Output is correct
36 Incorrect 8 ms 6320 KB Output isn't correct
37 Correct 7 ms 6320 KB Output is correct
38 Correct 31 ms 6320 KB Output is correct
39 Incorrect 9 ms 6320 KB Output isn't correct
40 Correct 11 ms 6320 KB Output is correct
41 Correct 44 ms 6320 KB Output is correct
42 Incorrect 10 ms 6320 KB Output isn't correct
43 Correct 10 ms 6320 KB Output is correct
44 Correct 54 ms 6320 KB Output is correct
45 Incorrect 12 ms 6320 KB Output isn't correct
46 Correct 12 ms 6320 KB Output is correct
47 Correct 65 ms 6320 KB Output is correct
48 Incorrect 14 ms 6320 KB Output isn't correct
49 Correct 14 ms 6320 KB Output is correct
50 Correct 80 ms 6320 KB Output is correct
51 Incorrect 17 ms 6320 KB Output isn't correct
52 Correct 15 ms 6320 KB Output is correct
53 Correct 104 ms 6320 KB Output is correct
54 Incorrect 17 ms 6320 KB Output isn't correct
55 Correct 16 ms 6320 KB Output is correct
56 Correct 115 ms 6320 KB Output is correct
57 Incorrect 21 ms 6320 KB Output isn't correct
58 Correct 24 ms 6320 KB Output is correct
59 Correct 176 ms 6320 KB Output is correct
60 Incorrect 21 ms 6320 KB Output isn't correct
61 Correct 21 ms 6320 KB Output is correct
62 Correct 151 ms 6320 KB Output is correct
63 Correct 136 ms 6320 KB Output is correct
64 Correct 234 ms 6320 KB Output is correct
65 Correct 186 ms 6396 KB Output is correct
66 Incorrect 159 ms 6396 KB Output isn't correct
67 Correct 37 ms 6396 KB Output is correct
68 Correct 58 ms 6396 KB Output is correct
69 Correct 55 ms 6396 KB Output is correct
70 Correct 53 ms 6396 KB Output is correct
71 Correct 46 ms 6396 KB Output is correct
72 Correct 34 ms 6396 KB Output is correct
73 Correct 49 ms 6436 KB Output is correct
74 Correct 76 ms 6436 KB Output is correct
75 Correct 85 ms 6436 KB Output is correct
76 Correct 82 ms 6436 KB Output is correct
77 Correct 89 ms 6436 KB Output is correct
78 Correct 53 ms 6512 KB Output is correct
79 Correct 68 ms 6512 KB Output is correct
80 Correct 74 ms 6528 KB Output is correct
81 Correct 84 ms 6528 KB Output is correct
82 Correct 84 ms 6528 KB Output is correct
83 Correct 122 ms 6528 KB Output is correct
84 Correct 116 ms 6552 KB Output is correct
85 Correct 96 ms 6552 KB Output is correct
86 Correct 102 ms 6552 KB Output is correct
87 Correct 96 ms 6552 KB Output is correct
88 Correct 99 ms 6552 KB Output is correct
89 Correct 117 ms 6552 KB Output is correct
90 Correct 114 ms 6552 KB Output is correct
91 Correct 105 ms 6552 KB Output is correct
92 Correct 104 ms 6552 KB Output is correct