Submission #716666

# Submission time Handle Problem Language Result Execution time Memory
716666 2023-03-30T18:33:45 Z xyzxyz Mecho (IOI09_mecho) C++14
100 / 100
155 ms 8728 KB
#include <bits/stdc++.h>

using namespace std;

vector<vector<int>> first_biene;
pair<int,int> zuhause;
int s,n;
vector<vector<int>> dist;


bool erreichbar(int i, int j, int zeit){
	vector<vector<int>> erreicht(n+2, vector<int>(n+2, 1e9));
	queue<pair<int,int>> next;
	next.push({i,j});
	erreicht[i][j] = zeit*s;
	while(!next.empty()){
        int i1 = next.front().first, j1 = next.front().second;
        //cout << i1 << " " << j1 << endl;
        next.pop();
        //cout << erreicht[i1][j1]+1 << " " << first_biene[i1][j1+1]*s << " " << erreicht[i1][j1+1] << endl;
        if(i1+1==zuhause.first && j1 == zuhause.second) return true;
        else if(i1==zuhause.first && j1+1 == zuhause.second) return true;
        else if(i1-1==zuhause.first && j1 == zuhause.second) return true;
        if(i1==zuhause.first && j1-1 == zuhause.second) return true;
        if(erreicht[i1][j1]+1 < first_biene[i1][j1+1]*s && erreicht[i1][j1]+1<erreicht[i1][j1+1]){
            erreicht[i1][j1+1] = erreicht[i1][j1]+1;
            next.push({i1, j1+1});
        }
        if(erreicht[i1][j1]+1 < first_biene[i1][j1-1]*s && erreicht[i1][j1]+1<erreicht[i1][j1-1]){
            erreicht[i1][j1-1] = erreicht[i1][j1]+1;
            next.push({i1, j1-1});
        }
        if(erreicht[i1][j1]+1 < first_biene[i1+1][j1]*s && erreicht[i1][j1]+1<erreicht[i1+1][j1]){
            erreicht[i1+1][j1] = erreicht[i1][j1]+1;
            next.push({i1+1, j1});
        }
        if(erreicht[i1][j1]+1 < first_biene[i1-1][j1]*s && erreicht[i1][j1]+1<erreicht[i1-1][j1]){
            erreicht[i1-1][j1] = erreicht[i1][j1]+1;
            next.push({i1-1, j1});
        }
	}
	/*for(vector<int> k: erreicht){
        for(int h: k) cout << h << " ";
        cout << endl;
	}*/
	return false;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	pair<int,int> start;
	cin >> n >> s;
	vector<vector<char>> karte(n+2, vector<char>(n+2, 'T'));
	first_biene = vector<vector<int>>(n+2, vector<int>(n+2,-1));
	queue<pair<int,pair<int,int>>> bienen;
	for (int i = 1; i <= n; i++){
		for(int j=1; j<=n; j++){
			cin >> karte[i][j];
			if(karte[i][j]=='M'){
				start = {i,j};
				karte[i][j] = 'G';
			}else if(karte[i][j] == 'D') zuhause = {i,j};
			else if(karte[i][j] == 'T'){
				first_biene[i][j] = 0;
			}else if(karte[i][j] == 'H'){
				first_biene[i][j] = 0;
				bienen.push({0,{i,j}});
			}
		}
	}
	while(!bienen.empty()){
		int zeitpunkt = bienen.front().first;
		int i = bienen.front().second.first, j = bienen.front().second.second;
		//cout << zeitpunkt << " " << i << " " << j << endl;
		bienen.pop();
		if(karte[i][j-1]=='G' && (first_biene[i][j-1]==-1 || zeitpunkt+1<first_biene[i][j-1])){
			///cout << "   " << i << " " << j-1 << endl;
			first_biene[i][j-1] = zeitpunkt+1;
			bienen.push({zeitpunkt+1, {i,j-1}});
		}
		if(karte[i-1][j]=='G' && (zeitpunkt+1 < first_biene[i-1][j] || first_biene[i-1][j] == -1)){
			//cout << "   " << i-1 << " " << j << endl;
			first_biene[i-1][j] = zeitpunkt+1;
			bienen.push({zeitpunkt+1, {i-1, j}});
		}
		if(karte[i+1][j]=='G' && (zeitpunkt+1 < first_biene[i+1][j] || first_biene[i+1][j] == -1)){
			//cout << "   " << i+1 << " " << j << endl;
			first_biene[i+1][j] = zeitpunkt+1;
			bienen.push({zeitpunkt+1, {i+1,j}});
		}
		if(karte[i][j+1]=='G' && (zeitpunkt+1 < first_biene[i][j+1] || first_biene[i][j+1] == -1)){
			//cout << "   " << i << " " << j+1 << endl;
			first_biene[i][j+1] = zeitpunkt+1;
			bienen.push({zeitpunkt+1, {i, j+1}});
		}
	}
	/*for (auto y : first_biene){
		for (auto x : y){
			cout << x << " ";
		}
		cout << endl;
	}
	*/
	int lower = 0, upper = first_biene[start.first][start.second]-1;
	while(lower<=upper){
		int zeit = (upper+lower)/2;
		//cout << "Zeit: " << zeit << endl;
		dist = vector<vector<int>>(n+2, vector<int>(n+2, 1e9));
		if(erreichbar(start.first, start.second, zeit)){
			//cout << "erreicht: " << zeit << endl;
			lower = zeit+1;
		}else{//nicht erreichbar in Zeit
			//cout << "nicht: " << zeit << endl;
			upper = zeit-1;
		}
		//cout << endl;
	}
	//cout << lower << " " << upper << endl;
	dist = vector<vector<int>>(n+2, vector<int>(n+2, 1e9));
	if(lower == 0 && !erreichbar(start.first, start.second, 0)) cout << "-1" << endl;
	else cout << lower-1;

	return 0;
}

/*
-1 -1 -1 -1 -1 -1 -1 -1 -1
-1 0 0 0 0 0 0 0 -1
-1 0 5 5 5 5 5 0 -1
-1 0 4 4 4 4 4 0 -1
-1 4 3 3 3 3 3 -1 -1
-1 0 2 2 2 2 2 0 -1
-1 0 1 1 1 1 1 0 -1
-1 0 0 0 0 0 0 0 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1
*/
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 78 ms 8620 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 7 ms 1936 KB Output is correct
34 Correct 7 ms 1936 KB Output is correct
35 Correct 14 ms 1956 KB Output is correct
36 Correct 8 ms 2388 KB Output is correct
37 Correct 9 ms 2388 KB Output is correct
38 Correct 19 ms 2440 KB Output is correct
39 Correct 11 ms 2988 KB Output is correct
40 Correct 11 ms 3044 KB Output is correct
41 Correct 23 ms 2900 KB Output is correct
42 Correct 13 ms 3588 KB Output is correct
43 Correct 14 ms 3588 KB Output is correct
44 Correct 30 ms 3540 KB Output is correct
45 Correct 16 ms 4312 KB Output is correct
46 Correct 17 ms 4280 KB Output is correct
47 Correct 36 ms 4292 KB Output is correct
48 Correct 18 ms 5008 KB Output is correct
49 Correct 19 ms 5012 KB Output is correct
50 Correct 47 ms 5000 KB Output is correct
51 Correct 22 ms 5860 KB Output is correct
52 Correct 25 ms 5888 KB Output is correct
53 Correct 51 ms 5752 KB Output is correct
54 Correct 27 ms 6724 KB Output is correct
55 Correct 27 ms 6732 KB Output is correct
56 Correct 63 ms 6696 KB Output is correct
57 Correct 31 ms 7708 KB Output is correct
58 Correct 30 ms 7636 KB Output is correct
59 Correct 76 ms 7636 KB Output is correct
60 Correct 34 ms 8648 KB Output is correct
61 Correct 35 ms 8640 KB Output is correct
62 Correct 90 ms 8592 KB Output is correct
63 Correct 101 ms 8580 KB Output is correct
64 Correct 153 ms 8520 KB Output is correct
65 Correct 155 ms 8600 KB Output is correct
66 Correct 114 ms 8604 KB Output is correct
67 Correct 117 ms 8600 KB Output is correct
68 Correct 53 ms 8608 KB Output is correct
69 Correct 51 ms 8624 KB Output is correct
70 Correct 46 ms 8672 KB Output is correct
71 Correct 43 ms 8620 KB Output is correct
72 Correct 43 ms 8644 KB Output is correct
73 Correct 35 ms 8544 KB Output is correct
74 Correct 62 ms 8628 KB Output is correct
75 Correct 63 ms 8596 KB Output is correct
76 Correct 59 ms 8624 KB Output is correct
77 Correct 65 ms 8644 KB Output is correct
78 Correct 73 ms 8628 KB Output is correct
79 Correct 57 ms 8640 KB Output is correct
80 Correct 68 ms 8728 KB Output is correct
81 Correct 64 ms 8652 KB Output is correct
82 Correct 63 ms 8644 KB Output is correct
83 Correct 72 ms 8644 KB Output is correct
84 Correct 63 ms 8624 KB Output is correct
85 Correct 63 ms 8608 KB Output is correct
86 Correct 74 ms 8624 KB Output is correct
87 Correct 62 ms 8624 KB Output is correct
88 Correct 72 ms 8632 KB Output is correct
89 Correct 68 ms 8612 KB Output is correct
90 Correct 77 ms 8632 KB Output is correct
91 Correct 77 ms 8636 KB Output is correct
92 Correct 78 ms 8608 KB Output is correct