답안 #103685

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
103685 2019-04-02T04:24:24 Z tugushka Mecho (IOI09_mecho) C++14
57 / 100
520 ms 66560 KB
#include<bits/stdc++.h>
#define mp make_pair
#define pb push_back
using namespace std;

using pii = pair < short, short >;
const int N = 805;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};

char a[N][N];
pii mecho, home;
int n, s, hive[N][N];
bool used[N][N];

void hive_bfs(){
	memset( hive, 11, sizeof(hive) );
	queue < pii > q;
	for(int i = 0 ; i < n ; i++) for(int j = 0 ; j < n ; j++){
		if( a[i][j] == 'H' ){
			q.push( mp( i, j ) );
			hive[i][j] = 0;
		}
	}

	int dis = 0;
	while( !q.empty() ){
		dis += s;
		int sz = q.size();
		while( sz-- ){
			short int x, y;
			tie(x, y) = q.front();
			q.pop();
			for(int i = 0 ; i < 4 ; i++){
				short int tx = x+dx[i], ty = y+dy[i];
				if( tx < 0 || ty < 0 || tx >= n || ty >= n ) continue;
				if( a[tx][ty] == 'G' && hive[tx][ty] > 2e6 ){
					hive[tx][ty] = dis;
					q.push( mp( tx, ty ) );
				}
			}
		}
	}

	/*
	for(int i = 0 ; i < n ; i++){
		for(int j = 0 ; j < n ; j++){
			if( hive[i][j] > 2e6 ) printf("@@ ");
			else printf("%2d ", hive[i][j]);
		}
		printf("\n");
	}
	*/
}

bool mecho_bfs(int start_time){
	memset( used, 0, sizeof(used) );

	if( start_time > hive[home.first][home.second] ){
		return false;
	}

	queue < pii > q;
	q.push( mecho );

	int dis = start_time;
	while( !q.empty() ){
		dis++;
		// cout << "dis " << dis << " : ";

		int sz = q.size();
		while( sz-- ){
			short int x, y;
			tie(x, y) = q.front();
			q.pop();
			for(int i = 0 ; i < 4 ; i++){
				short int tx = x+dx[i], ty = y+dy[i];
				if( tx < 0 || ty < 0 || tx >= n || ty >= n ) continue;
				if( !used[tx][ty] && a[tx][ty] == 'G' && hive[tx][ty] > dis ){
					used[tx][ty] = 1;
					if( used[ home.first ][ home.second ] ){
						return true;
					}
					// cout << "( " << tx << ',' << ty << " ), ";
					q.push( mp( tx, ty ) );
				}
			}
		}
		// cout << endl;
	}

	return used[ home.first ][ home.second ];
}

int main(){
	scanf("%d %d", &n, &s);
	for(int i = 0 ; i < n ; i++){
		scanf("%s", a[i]);
	}
	hive_bfs();
	for(int i = 0 ; i < n ; i++) for(int j = 0 ; j < n ; j++){
		if( a[i][j] == 'D' ){
			home = mp( i, j );
			a[i][j] = 'G';
		}
		if( a[i][j] == 'M' ){
			mecho = mp( i, j );
		}
	}

	if( !mecho_bfs( 0 ) ){
		printf("-1\n");
		return 0;
	}

	int l = 0, r = n*n, res;
	while( l <= r ){
		int mid = (l+r)/2;

		if( mecho_bfs( s*(mid) ) ){
			res = mid;
			l = mid+1;
		} else {
			r = mid-1;
		}
	}

	/*
	int res = 0;
	while( mecho_bfs( s*(res+1) ) ) res++;
	*/
	cout << res << endl;
}

Compilation message

mecho.cpp: In function 'int main()':
mecho.cpp:95:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &s);
  ~~~~~^~~~~~~~~~~~~~~~~
mecho.cpp:97:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s", a[i]);
   ~~~~~^~~~~~~~~~~~
mecho.cpp:131:10: warning: 'res' may be used uninitialized in this function [-Wmaybe-uninitialized]
  cout << res << endl;
          ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 3456 KB Output is correct
2 Correct 5 ms 3456 KB Output is correct
3 Correct 5 ms 3456 KB Output is correct
4 Correct 5 ms 3456 KB Output is correct
5 Correct 5 ms 3452 KB Output is correct
6 Correct 5 ms 3456 KB Output is correct
7 Correct 116 ms 4216 KB Output is correct
8 Correct 5 ms 3456 KB Output is correct
9 Correct 5 ms 3584 KB Output is correct
10 Correct 4 ms 3456 KB Output is correct
11 Correct 5 ms 3456 KB Output is correct
12 Incorrect 5 ms 3584 KB Output isn't correct
13 Incorrect 6 ms 3456 KB Output isn't correct
14 Correct 5 ms 3584 KB Output is correct
15 Correct 6 ms 3456 KB Output is correct
16 Correct 6 ms 3584 KB Output is correct
17 Correct 5 ms 3456 KB Output is correct
18 Correct 6 ms 3584 KB Output is correct
19 Correct 5 ms 3584 KB Output is correct
20 Correct 4 ms 3456 KB Output is correct
21 Correct 5 ms 3584 KB Output is correct
22 Correct 6 ms 3456 KB Output is correct
23 Correct 6 ms 3456 KB Output is correct
24 Correct 5 ms 3456 KB Output is correct
25 Correct 6 ms 3584 KB Output is correct
26 Correct 6 ms 3584 KB Output is correct
27 Correct 6 ms 3584 KB Output is correct
28 Correct 5 ms 3584 KB Output is correct
29 Correct 6 ms 3584 KB Output is correct
30 Correct 5 ms 3584 KB Output is correct
31 Correct 6 ms 3712 KB Output is correct
32 Correct 5 ms 3584 KB Output is correct
33 Correct 7 ms 3968 KB Output is correct
34 Runtime error 510 ms 66560 KB Execution killed with signal 9 (could be triggered by violating memory limits)
35 Correct 22 ms 3840 KB Output is correct
36 Correct 8 ms 3840 KB Output is correct
37 Runtime error 509 ms 66560 KB Execution killed with signal 9 (could be triggered by violating memory limits)
38 Correct 41 ms 3840 KB Output is correct
39 Correct 11 ms 3840 KB Output is correct
40 Runtime error 488 ms 66560 KB Execution killed with signal 9 (could be triggered by violating memory limits)
41 Correct 40 ms 3960 KB Output is correct
42 Correct 10 ms 3932 KB Output is correct
43 Runtime error 488 ms 66560 KB Execution killed with signal 9 (could be triggered by violating memory limits)
44 Correct 61 ms 3840 KB Output is correct
45 Correct 14 ms 3968 KB Output is correct
46 Runtime error 470 ms 66560 KB Execution killed with signal 9 (could be triggered by violating memory limits)
47 Correct 60 ms 3960 KB Output is correct
48 Correct 12 ms 3968 KB Output is correct
49 Runtime error 520 ms 66560 KB Execution killed with signal 9 (could be triggered by violating memory limits)
50 Correct 82 ms 3968 KB Output is correct
51 Correct 13 ms 3968 KB Output is correct
52 Runtime error 448 ms 66560 KB Execution killed with signal 9 (could be triggered by violating memory limits)
53 Correct 83 ms 4088 KB Output is correct
54 Correct 13 ms 4096 KB Output is correct
55 Runtime error 462 ms 66560 KB Execution killed with signal 9 (could be triggered by violating memory limits)
56 Correct 103 ms 4088 KB Output is correct
57 Correct 18 ms 4096 KB Output is correct
58 Runtime error 471 ms 66560 KB Execution killed with signal 9 (could be triggered by violating memory limits)
59 Correct 132 ms 4216 KB Output is correct
60 Correct 19 ms 4080 KB Output is correct
61 Runtime error 500 ms 66560 KB Execution killed with signal 9 (could be triggered by violating memory limits)
62 Correct 132 ms 4216 KB Output is correct
63 Correct 119 ms 4160 KB Output is correct
64 Runtime error 406 ms 66560 KB Execution killed with signal 9 (could be triggered by violating memory limits)
65 Correct 166 ms 4216 KB Output is correct
66 Correct 139 ms 4180 KB Output is correct
67 Correct 36 ms 4088 KB Output is correct
68 Correct 59 ms 4088 KB Output is correct
69 Correct 55 ms 4096 KB Output is correct
70 Correct 55 ms 4088 KB Output is correct
71 Correct 48 ms 4088 KB Output is correct
72 Incorrect 42 ms 4216 KB Output isn't correct
73 Incorrect 73 ms 4340 KB Output isn't correct
74 Correct 64 ms 4332 KB Output is correct
75 Correct 87 ms 4204 KB Output is correct
76 Correct 66 ms 4204 KB Output is correct
77 Correct 64 ms 4204 KB Output is correct
78 Correct 32 ms 4216 KB Output is correct
79 Correct 65 ms 4344 KB Output is correct
80 Correct 89 ms 4300 KB Output is correct
81 Correct 80 ms 4220 KB Output is correct
82 Correct 73 ms 4296 KB Output is correct
83 Correct 85 ms 4224 KB Output is correct
84 Correct 94 ms 4224 KB Output is correct
85 Correct 91 ms 4216 KB Output is correct
86 Correct 81 ms 4216 KB Output is correct
87 Correct 84 ms 4344 KB Output is correct
88 Correct 96 ms 4344 KB Output is correct
89 Correct 100 ms 4216 KB Output is correct
90 Correct 105 ms 4224 KB Output is correct
91 Correct 88 ms 4240 KB Output is correct
92 Correct 92 ms 4224 KB Output is correct