Submission #258446

# Submission time Handle Problem Language Result Execution time Memory
258446 2020-08-05T23:35:45 Z c4ts0up Mecho (IOI09_mecho) C++17
21 / 100
938 ms 35064 KB
/*
ID: c4ts0up
LANG: C++
TASK: Mecho
*/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define ff first
#define ss second

const int NMAX = 805;
const int INF = 1e9;

int n, s;
pair <int,int> home, mecho;
char grid[NMAX][NMAX];
int dist[NMAX][NMAX];

// hace el check a una coordenada
bool Check(int row, int col) {
	return (0 <= row && row < n && 0 <= col && col < n);
}

// BFS para encontrar el tiempo requerido para que las abejas lleguen a un lugar
void EncontrarDistancias() {
	queue <pair <int,int> > to;
	set <pair <int,int> > seen;
	
	for (int i=0; i<n; i++) for (int j=0; j<n; j++) {
		if (grid[i][j] == 'H') to.push({i,j}), dist[i][j] = 0;
		else dist[i][j] = INF;
	}

	while (!to.empty()) {
		pair <int,int> curr = to.front();
		to.pop();
		
		// ya lo visitamos
		if (seen.count(curr)) continue;
		
		if (grid[curr.ff][curr.ss] == 'T' || grid[curr.ff][curr.ss] == 'D') {
			seen.insert(curr);
			continue;
		}
		
		// visitamos a todos los vecinos
		if (!seen.count({curr.ff+1, curr.ss}) && Check(curr.ff+1, curr.ss)) to.push({curr.ff+1, curr.ss}), dist[curr.ff+1][curr.ss] = min(dist[curr.ff+1][curr.ss], dist[curr.ff][curr.ss]+1);
		if (!seen.count({curr.ff-1, curr.ss}) && Check(curr.ff-1, curr.ss)) to.push({curr.ff-1, curr.ss}), dist[curr.ff-1][curr.ss] = min(dist[curr.ff-1][curr.ss], dist[curr.ff][curr.ss]+1);
		if (!seen.count({curr.ff, curr.ss+1}) && Check(curr.ff, curr.ss+1)) to.push({curr.ff, curr.ss+1}), dist[curr.ff][curr.ss+1] = min(dist[curr.ff][curr.ss+1], dist[curr.ff][curr.ss]+1);
		if (!seen.count({curr.ff, curr.ss-1}) && Check(curr.ff, curr.ss-1)) to.push({curr.ff, curr.ss-1}), dist[curr.ff][curr.ss-1] = min(dist[curr.ff][curr.ss-1], dist[curr.ff][curr.ss]+1);
		
		seen.insert(curr);
	}
}

bool BFS(int minutos) {
	bool seen[NMAX][NMAX];
	queue <pair <int, pair <int,int> > > to;
	to.push({0, mecho});
	
	for (int i=0; i<=n; i++) for (int j=0; j<=n; j++) seen[i][j] = false;
	
	while (!to.empty()) {
		pair <int, pair <int,int> > curr = to.front();
		to.pop();
		
		int r = curr.ss.ff;
		int c = curr.ss.ss;
		int pasos = curr.ff;
		
		// ya lo visitamos, no tenemos que volver
		if (seen[r][c]) continue;
		
		// no podemos atravesarlo
		if (grid[r][c] == 'T') {
			seen[r][c] = true;
			continue;
		}
		else if (grid[r][c] == 'D') return true;
		
		// ya está infestado de abejas, o sea, bloqueado
		if (minutos + (curr.ff/s) >= dist[r][c]) {
			seen[r][c] = true;
			continue;
		}
		
		// visitamos a todos los vecinos
		
		
		if (!seen[r+1][c] && Check(r+1, c)) to.push({pasos, {r+1,c}});
		if (!seen[r-1][c] && Check(r-1, c)) to.push({pasos, {r-1,c}});
		if (!seen[r][c+1] && Check(r, c+1)) to.push({pasos, {r,c+1}});
		if (!seen[r][c-1] && Check(r, c-1)) to.push({pasos, {r,c-1}});
		
		seen[r][c] = true;
	}
	
	return false;
}


int main() {
	/*//
	freopen("", "r", stdin);
	freopen("", "w", stdout);
	//*/
	
	cin >> n >> s;
	for (int i=0; i<n; i++) {
		string aux;
		cin >> aux;
		for (int j=0; j<n; j++) {
			grid[i][j] = aux[j];
			
			if (grid[i][j] == 'M') mecho = {i,j};
			else if (grid[i][j] == 'D') home = {i,j};
		}
		
		
	}
	
	EncontrarDistancias();
	
	if (!BFS(0)) {
		cout << -1 << endl;
		exit(0);
	}
	
	int lb = 0, ub = 1e6, mid;
	while (lb <= ub) {
		mid = (lb+ub)/2;
		
		if (BFS(mid)) lb = mid+1;
		else ub = mid-1;
	}
	
	//cerr << "lb = " << lb << "; ub = " << ub << "; mid = " << mid << endl;
	cout << ub-1 << endl;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Incorrect 0 ms 384 KB Output isn't correct
3 Correct 1 ms 384 KB Output is correct
4 Incorrect 1 ms 384 KB Output isn't correct
5 Incorrect 1 ms 384 KB Output isn't correct
6 Incorrect 1 ms 384 KB Output isn't correct
7 Incorrect 826 ms 34808 KB Output isn't correct
8 Incorrect 1 ms 384 KB Output isn't correct
9 Incorrect 1 ms 384 KB Output isn't correct
10 Incorrect 1 ms 384 KB Output isn't correct
11 Incorrect 1 ms 384 KB Output isn't correct
12 Incorrect 2 ms 768 KB Output isn't correct
13 Incorrect 2 ms 640 KB Output isn't correct
14 Incorrect 3 ms 768 KB Output isn't correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 512 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
21 Correct 1 ms 640 KB Output is correct
22 Correct 1 ms 640 KB Output is correct
23 Correct 2 ms 640 KB Output is correct
24 Correct 2 ms 640 KB Output is correct
25 Correct 2 ms 768 KB Output is correct
26 Correct 2 ms 768 KB Output is correct
27 Correct 3 ms 896 KB Output is correct
28 Correct 3 ms 896 KB Output is correct
29 Correct 3 ms 896 KB Output is correct
30 Correct 3 ms 896 KB Output is correct
31 Correct 3 ms 896 KB Output is correct
32 Correct 3 ms 896 KB Output is correct
33 Correct 60 ms 7928 KB Output is correct
34 Correct 70 ms 7800 KB Output is correct
35 Incorrect 129 ms 7928 KB Output isn't correct
36 Correct 92 ms 9848 KB Output is correct
37 Correct 95 ms 9976 KB Output is correct
38 Incorrect 179 ms 9828 KB Output isn't correct
39 Correct 102 ms 12152 KB Output is correct
40 Correct 122 ms 12152 KB Output is correct
41 Incorrect 230 ms 12152 KB Output isn't correct
42 Correct 127 ms 14712 KB Output is correct
43 Correct 149 ms 14796 KB Output is correct
44 Incorrect 314 ms 14688 KB Output isn't correct
45 Correct 157 ms 17400 KB Output is correct
46 Correct 189 ms 17528 KB Output is correct
47 Incorrect 364 ms 17424 KB Output isn't correct
48 Correct 194 ms 20472 KB Output is correct
49 Correct 216 ms 20344 KB Output is correct
50 Incorrect 449 ms 20600 KB Output isn't correct
51 Correct 221 ms 23672 KB Output is correct
52 Correct 261 ms 23672 KB Output is correct
53 Incorrect 514 ms 23672 KB Output isn't correct
54 Correct 272 ms 27328 KB Output is correct
55 Correct 309 ms 27128 KB Output is correct
56 Incorrect 609 ms 27256 KB Output isn't correct
57 Correct 288 ms 30840 KB Output is correct
58 Correct 340 ms 30968 KB Output is correct
59 Incorrect 711 ms 30840 KB Output isn't correct
60 Correct 386 ms 34772 KB Output is correct
61 Correct 388 ms 34808 KB Output is correct
62 Incorrect 827 ms 34808 KB Output isn't correct
63 Incorrect 856 ms 34424 KB Output isn't correct
64 Correct 851 ms 34428 KB Output is correct
65 Incorrect 836 ms 34424 KB Output isn't correct
66 Incorrect 808 ms 34592 KB Output isn't correct
67 Incorrect 797 ms 34552 KB Output isn't correct
68 Incorrect 771 ms 34632 KB Output isn't correct
69 Incorrect 765 ms 34424 KB Output isn't correct
70 Incorrect 802 ms 34668 KB Output isn't correct
71 Incorrect 824 ms 34552 KB Output isn't correct
72 Incorrect 838 ms 34644 KB Output isn't correct
73 Incorrect 827 ms 34804 KB Output isn't correct
74 Correct 871 ms 34836 KB Output is correct
75 Incorrect 852 ms 34760 KB Output isn't correct
76 Incorrect 873 ms 34784 KB Output isn't correct
77 Incorrect 833 ms 34936 KB Output isn't correct
78 Incorrect 783 ms 35064 KB Output isn't correct
79 Correct 750 ms 34808 KB Output is correct
80 Incorrect 757 ms 34808 KB Output isn't correct
81 Incorrect 769 ms 34764 KB Output isn't correct
82 Incorrect 795 ms 34916 KB Output isn't correct
83 Incorrect 794 ms 34888 KB Output isn't correct
84 Correct 815 ms 34748 KB Output is correct
85 Incorrect 850 ms 34936 KB Output isn't correct
86 Incorrect 828 ms 34760 KB Output isn't correct
87 Incorrect 860 ms 34840 KB Output isn't correct
88 Incorrect 859 ms 34904 KB Output isn't correct
89 Correct 938 ms 34736 KB Output is correct
90 Incorrect 841 ms 34780 KB Output isn't correct
91 Incorrect 903 ms 34728 KB Output isn't correct
92 Incorrect 845 ms 34792 KB Output isn't correct