Submission #53453

# Submission time Handle Problem Language Result Execution time Memory
53453 2018-06-30T04:56:36 Z 윤교준(#1420) Mecho (IOI09_mecho) C++11
38 / 100
287 ms 36760 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; 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);
   ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 376 KB Output isn't correct
2 Incorrect 2 ms 492 KB Output isn't correct
3 Incorrect 2 ms 492 KB Output isn't correct
4 Incorrect 2 ms 496 KB Output isn't correct
5 Correct 2 ms 500 KB Output is correct
6 Correct 3 ms 632 KB Output is correct
7 Correct 117 ms 7076 KB Output is correct
8 Incorrect 2 ms 7076 KB Output isn't correct
9 Correct 3 ms 7076 KB Output is correct
10 Correct 2 ms 7076 KB Output is correct
11 Correct 2 ms 7076 KB Output is correct
12 Incorrect 3 ms 7076 KB Output isn't correct
13 Incorrect 3 ms 7076 KB Output isn't correct
14 Correct 3 ms 7076 KB Output is correct
15 Incorrect 2 ms 7076 KB Output isn't correct
16 Correct 3 ms 7076 KB Output is correct
17 Incorrect 2 ms 7076 KB Output isn't correct
18 Correct 2 ms 7076 KB Output is correct
19 Incorrect 3 ms 7076 KB Output isn't correct
20 Correct 3 ms 7076 KB Output is correct
21 Incorrect 2 ms 7076 KB Output isn't correct
22 Correct 2 ms 7076 KB Output is correct
23 Incorrect 2 ms 7076 KB Output isn't correct
24 Correct 3 ms 7076 KB Output is correct
25 Incorrect 2 ms 7076 KB Output isn't correct
26 Correct 3 ms 7076 KB Output is correct
27 Incorrect 3 ms 7076 KB Output isn't correct
28 Correct 3 ms 7076 KB Output is correct
29 Incorrect 4 ms 7076 KB Output isn't correct
30 Correct 4 ms 7076 KB Output is correct
31 Incorrect 2 ms 7076 KB Output isn't correct
32 Correct 4 ms 7076 KB Output is correct
33 Incorrect 7 ms 7076 KB Output isn't correct
34 Correct 8 ms 7076 KB Output is correct
35 Correct 23 ms 7076 KB Output is correct
36 Incorrect 10 ms 7076 KB Output isn't correct
37 Correct 8 ms 7076 KB Output is correct
38 Correct 32 ms 7076 KB Output is correct
39 Incorrect 9 ms 7076 KB Output isn't correct
40 Correct 10 ms 7076 KB Output is correct
41 Correct 42 ms 7076 KB Output is correct
42 Incorrect 11 ms 7076 KB Output isn't correct
43 Correct 11 ms 7076 KB Output is correct
44 Correct 60 ms 7228 KB Output is correct
45 Incorrect 13 ms 7860 KB Output isn't correct
46 Correct 18 ms 8156 KB Output is correct
47 Correct 72 ms 8560 KB Output is correct
48 Incorrect 14 ms 9260 KB Output isn't correct
49 Correct 13 ms 9616 KB Output is correct
50 Correct 78 ms 9972 KB Output is correct
51 Incorrect 17 ms 10712 KB Output isn't correct
52 Correct 16 ms 11128 KB Output is correct
53 Correct 112 ms 11612 KB Output is correct
54 Incorrect 18 ms 12344 KB Output isn't correct
55 Correct 20 ms 12876 KB Output is correct
56 Correct 128 ms 13412 KB Output is correct
57 Incorrect 21 ms 14168 KB Output isn't correct
58 Correct 20 ms 14732 KB Output is correct
59 Correct 147 ms 15272 KB Output is correct
60 Incorrect 26 ms 16336 KB Output isn't correct
61 Correct 23 ms 16964 KB Output is correct
62 Correct 175 ms 17592 KB Output is correct
63 Correct 160 ms 18348 KB Output is correct
64 Correct 287 ms 18968 KB Output is correct
65 Correct 226 ms 19480 KB Output is correct
66 Incorrect 170 ms 20108 KB Output isn't correct
67 Correct 53 ms 20736 KB Output is correct
68 Correct 66 ms 21328 KB Output is correct
69 Correct 62 ms 22024 KB Output is correct
70 Correct 59 ms 22716 KB Output is correct
71 Correct 49 ms 23360 KB Output is correct
72 Incorrect 37 ms 23932 KB Output isn't correct
73 Incorrect 44 ms 24656 KB Output isn't correct
74 Correct 77 ms 25284 KB Output is correct
75 Correct 101 ms 26076 KB Output is correct
76 Correct 107 ms 26544 KB Output is correct
77 Correct 91 ms 27220 KB Output is correct
78 Correct 43 ms 27828 KB Output is correct
79 Correct 69 ms 28460 KB Output is correct
80 Correct 66 ms 29084 KB Output is correct
81 Correct 78 ms 29712 KB Output is correct
82 Correct 72 ms 30340 KB Output is correct
83 Correct 96 ms 30892 KB Output is correct
84 Correct 92 ms 31528 KB Output is correct
85 Correct 89 ms 32280 KB Output is correct
86 Correct 92 ms 33028 KB Output is correct
87 Correct 90 ms 33580 KB Output is correct
88 Correct 95 ms 34136 KB Output is correct
89 Correct 118 ms 34760 KB Output is correct
90 Correct 126 ms 35520 KB Output is correct
91 Correct 108 ms 36156 KB Output is correct
92 Correct 99 ms 36760 KB Output is correct