Submission #835363

# Submission time Handle Problem Language Result Execution time Memory
835363 2023-08-23T13:46:54 Z mohammad_kilani Mecho (IOI09_mecho) C++17
84 / 100
148 ms 6312 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]);
      |   ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2772 KB Output is correct
2 Incorrect 1 ms 2772 KB Output isn't correct
3 Correct 1 ms 2772 KB Output is correct
4 Incorrect 1 ms 2772 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 57 ms 6136 KB Output isn't correct
8 Correct 1 ms 2900 KB Output is correct
9 Correct 1 ms 2900 KB Output is correct
10 Correct 1 ms 2900 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 2 ms 3028 KB Output is correct
14 Correct 1 ms 3028 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 2900 KB Output is correct
19 Correct 1 ms 2900 KB Output is correct
20 Correct 1 ms 2900 KB Output is correct
21 Correct 1 ms 3028 KB Output is correct
22 Correct 1 ms 3028 KB Output is correct
23 Correct 1 ms 3028 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 1 ms 3156 KB Output is correct
30 Correct 1 ms 3156 KB Output is correct
31 Correct 1 ms 3156 KB Output is correct
32 Correct 1 ms 3156 KB Output is correct
33 Correct 4 ms 4180 KB Output is correct
34 Correct 4 ms 4180 KB Output is correct
35 Correct 15 ms 4180 KB Output is correct
36 Correct 4 ms 4436 KB Output is correct
37 Correct 5 ms 4436 KB Output is correct
38 Correct 20 ms 4456 KB Output is correct
39 Correct 5 ms 4564 KB Output is correct
40 Correct 6 ms 4564 KB Output is correct
41 Correct 33 ms 4644 KB Output is correct
42 Correct 6 ms 4848 KB Output is correct
43 Correct 7 ms 4804 KB Output is correct
44 Correct 30 ms 4844 KB Output is correct
45 Correct 7 ms 4948 KB Output is correct
46 Correct 8 ms 4948 KB Output is correct
47 Correct 34 ms 4948 KB Output is correct
48 Correct 8 ms 5204 KB Output is correct
49 Correct 9 ms 5204 KB Output is correct
50 Correct 43 ms 5148 KB Output is correct
51 Correct 10 ms 5448 KB Output is correct
52 Correct 10 ms 5436 KB Output is correct
53 Correct 52 ms 5440 KB Output is correct
54 Correct 11 ms 5588 KB Output is correct
55 Correct 11 ms 5648 KB Output is correct
56 Correct 64 ms 5588 KB Output is correct
57 Correct 12 ms 5844 KB Output is correct
58 Correct 13 ms 5844 KB Output is correct
59 Correct 67 ms 5828 KB Output is correct
60 Correct 13 ms 5972 KB Output is correct
61 Correct 14 ms 5972 KB Output is correct
62 Correct 79 ms 6036 KB Output is correct
63 Correct 76 ms 6024 KB Output is correct
64 Correct 148 ms 6024 KB Output is correct
65 Correct 109 ms 5972 KB Output is correct
66 Correct 89 ms 6028 KB Output is correct
67 Correct 80 ms 5972 KB Output is correct
68 Correct 36 ms 6056 KB Output is correct
69 Correct 33 ms 5976 KB Output is correct
70 Correct 25 ms 5968 KB Output is correct
71 Correct 32 ms 6088 KB Output is correct
72 Correct 21 ms 5972 KB Output is correct
73 Correct 21 ms 6228 KB Output is correct
74 Correct 37 ms 6228 KB Output is correct
75 Correct 58 ms 6300 KB Output is correct
76 Correct 42 ms 6312 KB Output is correct
77 Correct 39 ms 6224 KB Output is correct
78 Correct 48 ms 6192 KB Output is correct
79 Correct 31 ms 6192 KB Output is correct
80 Correct 39 ms 6280 KB Output is correct
81 Correct 43 ms 6280 KB Output is correct
82 Correct 38 ms 6276 KB Output is correct
83 Correct 50 ms 6240 KB Output is correct
84 Correct 44 ms 6244 KB Output is correct
85 Correct 54 ms 6244 KB Output is correct
86 Correct 47 ms 6152 KB Output is correct
87 Correct 47 ms 6228 KB Output is correct
88 Correct 63 ms 6188 KB Output is correct
89 Correct 51 ms 6192 KB Output is correct
90 Correct 53 ms 6196 KB Output is correct
91 Correct 61 ms 6188 KB Output is correct
92 Correct 64 ms 6100 KB Output is correct