답안 #835354

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
835354 2023-08-23T13:39:16 Z mohammad_kilani Mecho (IOI09_mecho) C++17
84 / 100
143 ms 6932 KB
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define oo 1000000010

const int N = 810;

int n , s;

char grid[N][N];

int cost[N][N];

int dr[] = {0 , 1,  0 , -1};
int dc[] = {1 , 0 , -1 , 0};

int sr , sc , er , ec;

int cost2[N][N];

void BFSH(){
	queue < pair< int , int > > q;
	memset(cost , -1, sizeof(cost));
	for(int i = 0;i < n;i++){
		for(int j = 0 ;j < n;j++){
			if(grid[i][j] == 'H'){
				q.push(make_pair(i , j));
				cost[i][j] = 0;
			}
		}
	}
	int row , col;
	while(!q.empty()){
		row = q.front().first;
		col = q.front().second;
		q.pop();
		for(int nr , nc , i = 0 ;i < 4;i++){
			nr = row + dr[i];
			nc = col + dc[i];
			if(nr < 0 || nr >= n || nc < 0 || nc >= n || grid[nr][nc] == 'T' || cost[nr][nc] != -1) continue;
			cost[nr][nc] = cost[row][col] + 1;
			q.push(make_pair(nr , nc));
		}
	}
}

bool check(int mid){
	for(int i = 0 ;i < n;i++) for(int j = 0 ;j < n;j++) cost2[i][j] = -1;
	cost2[sr][sc] = mid * s;
	queue < pair< int , int > > q;
	q.push(make_pair(sr, sc));
	int row , col;
	while(!q.empty()){
		row = q.front().first;
		col = q.front().second;
		q.pop();
		if((cost2[row][col] / s) >= cost[row][col]) continue;
		for(int nr , nc , i = 0 ;i < 4;i++){
			nr = row + dr[i];
			nc = col + dc[i];
			if(nr < 0 || nr >= n || nc < 0 || nc >= n || grid[nr][nc] == 'T' || cost2[nr][nc] != -1) continue;
			cost2[nr][nc] = cost2[row][col] + 1;
			q.push(make_pair(nr , nc));
		}
	}
	if(cost2[er][ec] == -1 || (cost2[er][ec] / s) >= cost[er][ec])
		return false;
	return true;
}

int main(){
	scanf("%d%d",&n,&s);
	for(int i = 0 ;i < n;i++){
		scanf("%s",grid[i]);
		for(int j = 0 ;j < n;j++){
			if(grid[i][j] == 'M')
				sr = i , sc = j;
			if(grid[i][j] == 'D')
				er = i , ec = j;
		}
	}
	BFSH();

	int low = 0 , high = n * n , mid , ans = -1;

	while(high >= low){
		mid = (low + high) / 2;
		if(check(mid))
			ans = mid , low = mid + 1;
		else
			high = mid - 1;
	}
	cout << ans << endl;
	return 0;
} 

Compilation message

mecho.cpp: In function 'int main()':
mecho.cpp:72:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |  scanf("%d%d",&n,&s);
      |  ~~~~~^~~~~~~~~~~~~~
mecho.cpp:74:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |   scanf("%s",grid[i]);
      |   ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2772 KB Output is correct
2 Incorrect 1 ms 2868 KB Output isn't correct
3 Correct 1 ms 2868 KB Output is correct
4 Incorrect 1 ms 2868 KB Output isn't correct
5 Correct 1 ms 2900 KB Output is correct
6 Incorrect 1 ms 2900 KB Output isn't correct
7 Incorrect 53 ms 6768 KB Output isn't correct
8 Correct 2 ms 2900 KB Output is correct
9 Correct 2 ms 2900 KB Output is correct
10 Correct 1 ms 2864 KB Output is correct
11 Correct 1 ms 2900 KB Output is correct
12 Incorrect 1 ms 3028 KB Output isn't correct
13 Correct 1 ms 3028 KB Output is correct
14 Correct 2 ms 3128 KB Output is correct
15 Correct 1 ms 2900 KB Output is correct
16 Correct 1 ms 2900 KB Output is correct
17 Correct 1 ms 2900 KB Output is correct
18 Correct 1 ms 2864 KB Output is correct
19 Correct 1 ms 2900 KB Output is correct
20 Correct 2 ms 3028 KB Output is correct
21 Correct 1 ms 3028 KB Output is correct
22 Correct 2 ms 3028 KB Output is correct
23 Correct 1 ms 2996 KB Output is correct
24 Correct 1 ms 3028 KB Output is correct
25 Correct 1 ms 3028 KB Output is correct
26 Correct 1 ms 3028 KB Output is correct
27 Correct 1 ms 3028 KB Output is correct
28 Correct 1 ms 3028 KB Output is correct
29 Correct 2 ms 3156 KB Output is correct
30 Correct 2 ms 3156 KB Output is correct
31 Correct 1 ms 3156 KB Output is correct
32 Correct 2 ms 3096 KB Output is correct
33 Correct 4 ms 4308 KB Output is correct
34 Correct 4 ms 4308 KB Output is correct
35 Correct 16 ms 4296 KB Output is correct
36 Correct 5 ms 4564 KB Output is correct
37 Correct 5 ms 4616 KB Output is correct
38 Correct 18 ms 4564 KB Output is correct
39 Correct 5 ms 4820 KB Output is correct
40 Correct 8 ms 4796 KB Output is correct
41 Correct 26 ms 4820 KB Output is correct
42 Correct 6 ms 5076 KB Output is correct
43 Correct 7 ms 5060 KB Output is correct
44 Correct 30 ms 5076 KB Output is correct
45 Correct 7 ms 5332 KB Output is correct
46 Correct 8 ms 5332 KB Output is correct
47 Correct 36 ms 5332 KB Output is correct
48 Correct 9 ms 5568 KB Output is correct
49 Correct 9 ms 5604 KB Output is correct
50 Correct 40 ms 5588 KB Output is correct
51 Correct 10 ms 5844 KB Output is correct
52 Correct 10 ms 5820 KB Output is correct
53 Correct 52 ms 5856 KB Output is correct
54 Correct 11 ms 6100 KB Output is correct
55 Correct 12 ms 6120 KB Output is correct
56 Correct 62 ms 6116 KB Output is correct
57 Correct 13 ms 6396 KB Output is correct
58 Correct 14 ms 6336 KB Output is correct
59 Correct 73 ms 6380 KB Output is correct
60 Correct 14 ms 6612 KB Output is correct
61 Correct 15 ms 6572 KB Output is correct
62 Correct 79 ms 6656 KB Output is correct
63 Correct 76 ms 6544 KB Output is correct
64 Correct 143 ms 6652 KB Output is correct
65 Correct 109 ms 6612 KB Output is correct
66 Correct 86 ms 6552 KB Output is correct
67 Correct 80 ms 6652 KB Output is correct
68 Correct 33 ms 6568 KB Output is correct
69 Correct 33 ms 6596 KB Output is correct
70 Correct 25 ms 6600 KB Output is correct
71 Correct 28 ms 6596 KB Output is correct
72 Correct 22 ms 6600 KB Output is correct
73 Correct 22 ms 6860 KB Output is correct
74 Correct 37 ms 6856 KB Output is correct
75 Correct 45 ms 6836 KB Output is correct
76 Correct 40 ms 6932 KB Output is correct
77 Correct 39 ms 6868 KB Output is correct
78 Correct 64 ms 6808 KB Output is correct
79 Correct 31 ms 6900 KB Output is correct
80 Correct 40 ms 6908 KB Output is correct
81 Correct 44 ms 6908 KB Output is correct
82 Correct 39 ms 6868 KB Output is correct
83 Correct 50 ms 6864 KB Output is correct
84 Correct 44 ms 6776 KB Output is correct
85 Correct 53 ms 6864 KB Output is correct
86 Correct 48 ms 6772 KB Output is correct
87 Correct 48 ms 6868 KB Output is correct
88 Correct 51 ms 6740 KB Output is correct
89 Correct 51 ms 6724 KB Output is correct
90 Correct 53 ms 6808 KB Output is correct
91 Correct 52 ms 6740 KB Output is correct
92 Correct 58 ms 6720 KB Output is correct