답안 #258861

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
258861 2020-08-06T16:02:32 Z c4ts0up Mecho (IOI09_mecho) C++17
100 / 100
277 ms 5024 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];
bool seen[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;
	
	for (int i=0; i<=n; i++) for (int j=0; j<=n; j++) seen[i][j] = false;
	
	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[curr.ff][curr.ss]) continue;
		
		if (grid[curr.ff][curr.ss] == 'T' || grid[curr.ff][curr.ss] == 'D') {
			seen[curr.ff][curr.ss] = true;
			continue;
		}
		
		// visitamos a todos los vecinos
		if (Check(curr.ff+1, curr.ss) && dist[curr.ff+1][curr.ss] > dist[curr.ff][curr.ss]+1) to.push({curr.ff+1, curr.ss}), dist[curr.ff+1][curr.ss] = dist[curr.ff][curr.ss]+1;
		if (Check(curr.ff-1, curr.ss) && dist[curr.ff-1][curr.ss] > dist[curr.ff][curr.ss]+1) to.push({curr.ff-1, curr.ss}), dist[curr.ff-1][curr.ss] = dist[curr.ff][curr.ss]+1;
		if (Check(curr.ff, curr.ss+1) && dist[curr.ff][curr.ss+1] > dist[curr.ff][curr.ss]+1) to.push({curr.ff, curr.ss+1}), dist[curr.ff][curr.ss+1] = dist[curr.ff][curr.ss]+1;
		if (Check(curr.ff, curr.ss-1) && dist[curr.ff][curr.ss-1] > dist[curr.ff][curr.ss]+1) to.push({curr.ff, curr.ss-1}), dist[curr.ff][curr.ss-1] = dist[curr.ff][curr.ss]+1;
		
		seen[curr.ff][curr.ss] = true;
	}
}

bool BFS(int minutos) {
	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;
		
		//cerr << "Visitando <" << r << ", " << c << "> luego de " << pasos << " pasos (pasos/s = " << (max(pasos, 0)/s) << ")" << endl;
		
		// 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 + (max(pasos, 0)/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+1, {r+1,c}});
		if (!seen[r-1][c] && Check(r-1, c)) to.push({pasos+1, {r-1,c}});
		if (!seen[r][c+1] && Check(r, c+1)) to.push({pasos+1, {r,c+1}});
		if (!seen[r][c-1] && Check(r, c-1)) to.push({pasos+1, {r,c-1}});
		
		seen[r][c] = true;
	}
	
	return false;
}


int main() {
	/*//
	freopen("test.in", "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();
	
	/*//
	for (int i=0; i<n; i++) {
		for (int j=0; j<n; j++) {
			if (grid[i][j] == 'T' || grid[i][j] == 'D') cerr << grid[i][j] << " ";
			else cerr << dist[i][j] << " ";
		}
		cerr << endl;
	}
	//*/
	
	if (!BFS(0)) {
		cout << -1 << endl;
		exit(0);
	}
	
	int lb = 0, ub = 1e6, mid;
	while (lb <= ub) {
		mid = (lb+ub)/2;
		
		//cerr << "--- mid = " << mid << endl;
		if (BFS(mid)) lb = mid+1;
		else ub = mid-1;
	}
	
	//cerr << "lb = " << lb << "; ub = " << ub << "; mid = " << mid << endl;
	cout << ub << endl;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 171 ms 4896 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 640 KB Output is correct
13 Correct 1 ms 512 KB Output is correct
14 Correct 1 ms 640 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 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 512 KB Output is correct
22 Correct 1 ms 512 KB Output is correct
23 Correct 1 ms 512 KB Output is correct
24 Correct 1 ms 512 KB Output is correct
25 Correct 1 ms 640 KB Output is correct
26 Correct 1 ms 640 KB Output is correct
27 Correct 1 ms 640 KB Output is correct
28 Correct 1 ms 640 KB Output is correct
29 Correct 1 ms 640 KB Output is correct
30 Correct 1 ms 640 KB Output is correct
31 Correct 1 ms 640 KB Output is correct
32 Correct 1 ms 640 KB Output is correct
33 Correct 10 ms 2048 KB Output is correct
34 Correct 11 ms 2048 KB Output is correct
35 Correct 33 ms 2048 KB Output is correct
36 Correct 14 ms 2432 KB Output is correct
37 Correct 14 ms 2304 KB Output is correct
38 Correct 43 ms 2432 KB Output is correct
39 Correct 17 ms 2688 KB Output is correct
40 Correct 17 ms 2688 KB Output is correct
41 Correct 55 ms 2688 KB Output is correct
42 Correct 21 ms 2944 KB Output is correct
43 Correct 21 ms 2944 KB Output is correct
44 Correct 72 ms 2944 KB Output is correct
45 Correct 24 ms 3200 KB Output is correct
46 Correct 25 ms 3200 KB Output is correct
47 Correct 91 ms 3280 KB Output is correct
48 Correct 29 ms 3448 KB Output is correct
49 Correct 29 ms 3456 KB Output is correct
50 Correct 101 ms 3576 KB Output is correct
51 Correct 34 ms 3832 KB Output is correct
52 Correct 35 ms 3840 KB Output is correct
53 Correct 132 ms 3960 KB Output is correct
54 Correct 39 ms 4084 KB Output is correct
55 Correct 39 ms 4096 KB Output is correct
56 Correct 142 ms 4088 KB Output is correct
57 Correct 44 ms 4472 KB Output is correct
58 Correct 49 ms 4472 KB Output is correct
59 Correct 167 ms 4472 KB Output is correct
60 Correct 50 ms 4728 KB Output is correct
61 Correct 50 ms 4728 KB Output is correct
62 Correct 181 ms 4728 KB Output is correct
63 Correct 170 ms 4856 KB Output is correct
64 Correct 242 ms 4856 KB Output is correct
65 Correct 277 ms 4732 KB Output is correct
66 Correct 235 ms 4856 KB Output is correct
67 Correct 65 ms 4728 KB Output is correct
68 Correct 96 ms 4728 KB Output is correct
69 Correct 93 ms 4728 KB Output is correct
70 Correct 85 ms 4728 KB Output is correct
71 Correct 73 ms 4728 KB Output is correct
72 Correct 65 ms 4732 KB Output is correct
73 Correct 81 ms 4984 KB Output is correct
74 Correct 108 ms 4988 KB Output is correct
75 Correct 117 ms 4984 KB Output is correct
76 Correct 101 ms 4984 KB Output is correct
77 Correct 106 ms 4984 KB Output is correct
78 Correct 60 ms 4984 KB Output is correct
79 Correct 103 ms 4984 KB Output is correct
80 Correct 108 ms 4984 KB Output is correct
81 Correct 114 ms 4984 KB Output is correct
82 Correct 95 ms 4984 KB Output is correct
83 Correct 128 ms 4984 KB Output is correct
84 Correct 132 ms 4984 KB Output is correct
85 Correct 116 ms 4984 KB Output is correct
86 Correct 126 ms 5024 KB Output is correct
87 Correct 119 ms 4900 KB Output is correct
88 Correct 136 ms 4984 KB Output is correct
89 Correct 137 ms 4856 KB Output is correct
90 Correct 140 ms 4880 KB Output is correct
91 Correct 134 ms 4856 KB Output is correct
92 Correct 134 ms 4856 KB Output is correct