Submission #835394

# Submission time Handle Problem Language Result Execution time Memory
835394 2023-08-23T14:04:31 Z mohammad_kilani Mecho (IOI09_mecho) C++17
93 / 100
141 ms 6308 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] >= cost[row][col] * s) 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));
		}
	}
	//cout << "check " << mid << " " << cost2[er][ec] << " " << cost[er][ec] << endl;
	if(cost2[er][ec] == -1 || cost2[er][ec] == cost[er][ec] * s)
		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 = cost[sr][sc] - 1 , 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:73:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |  scanf("%d%d",&n,&s);
      |  ~~~~~^~~~~~~~~~~~~~
mecho.cpp:75:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |   scanf("%s",grid[i]);
      |   ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2772 KB Output is correct
2 Correct 1 ms 2772 KB Output is correct
3 Correct 1 ms 2772 KB Output is correct
4 Incorrect 3 ms 2772 KB Output isn't correct
5 Correct 1 ms 2900 KB Output is correct
6 Correct 1 ms 2948 KB Output is correct
7 Correct 65 ms 6136 KB Output is 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 1 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 2 ms 2900 KB Output is correct
17 Correct 2 ms 2900 KB Output is correct
18 Correct 1 ms 2900 KB Output is correct
19 Correct 2 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 2 ms 3028 KB Output is correct
25 Correct 1 ms 3028 KB Output is correct
26 Correct 2 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 1 ms 3028 KB Output is correct
31 Correct 1 ms 3156 KB Output is correct
32 Correct 2 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 14 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 19 ms 4436 KB Output is correct
39 Correct 5 ms 4564 KB Output is correct
40 Correct 6 ms 4564 KB Output is correct
41 Correct 22 ms 4564 KB Output is correct
42 Correct 6 ms 4820 KB Output is correct
43 Correct 7 ms 4820 KB Output is correct
44 Correct 34 ms 4820 KB Output is correct
45 Correct 7 ms 4948 KB Output is correct
46 Correct 8 ms 4948 KB Output is correct
47 Correct 39 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 39 ms 5204 KB Output is correct
51 Correct 9 ms 5332 KB Output is correct
52 Correct 10 ms 5332 KB Output is correct
53 Correct 47 ms 5432 KB Output is correct
54 Correct 10 ms 5588 KB Output is correct
55 Correct 15 ms 5588 KB Output is correct
56 Correct 55 ms 5632 KB Output is correct
57 Correct 12 ms 5844 KB Output is correct
58 Correct 14 ms 5844 KB Output is correct
59 Correct 62 ms 5844 KB Output is correct
60 Correct 13 ms 5972 KB Output is correct
61 Correct 15 ms 6040 KB Output is correct
62 Correct 77 ms 6032 KB Output is correct
63 Correct 82 ms 6024 KB Output is correct
64 Correct 141 ms 6028 KB Output is correct
65 Correct 129 ms 5976 KB Output is correct
66 Correct 91 ms 6028 KB Output is correct
67 Correct 87 ms 5972 KB Output is correct
68 Correct 38 ms 5968 KB Output is correct
69 Correct 30 ms 6056 KB Output is correct
70 Correct 29 ms 5972 KB Output is correct
71 Correct 22 ms 5972 KB Output is correct
72 Correct 27 ms 5968 KB Output is correct
73 Correct 30 ms 6220 KB Output is correct
74 Correct 49 ms 6304 KB Output is correct
75 Correct 45 ms 6220 KB Output is correct
76 Correct 42 ms 6308 KB Output is correct
77 Correct 54 ms 6216 KB Output is correct
78 Correct 55 ms 6276 KB Output is correct
79 Correct 43 ms 6288 KB Output is correct
80 Correct 43 ms 6280 KB Output is correct
81 Correct 54 ms 6276 KB Output is correct
82 Correct 43 ms 6288 KB Output is correct
83 Correct 61 ms 6252 KB Output is correct
84 Correct 51 ms 6240 KB Output is correct
85 Correct 55 ms 6244 KB Output is correct
86 Correct 55 ms 6164 KB Output is correct
87 Correct 49 ms 6236 KB Output is correct
88 Correct 60 ms 6188 KB Output is correct
89 Correct 51 ms 6184 KB Output is correct
90 Correct 63 ms 6188 KB Output is correct
91 Correct 57 ms 6100 KB Output is correct
92 Correct 61 ms 6192 KB Output is correct