# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
716657 | xyzxyz | Mecho (IOI09_mecho) | C++14 | 1096 ms | 65536 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> first_biene;
pair<int,int> zuhause;
int s;
vector<vector<int>> dist;
bool erreichbar(int i, int j, int zeit, int gemachte_schritte){
//cout << i << " " << j << " " << gemachte_schritte << " " << zeit << endl;
dist[i][j] = zeit;
if(i == zuhause.first && j==zuhause.second) return true;
if (gemachte_schritte >= s){
zeit++;
gemachte_schritte = 0;
}
if (first_biene[i][j] <= zeit){
return false;
}
bool tmp = false;
if(!tmp && dist[i-1][j]>=zeit) tmp = erreichbar(i-1, j, zeit, gemachte_schritte+1);
if(!tmp && dist[i+1][j]>=zeit) tmp = erreichbar(i+1, j, zeit, gemachte_schritte+1);
if(!tmp && dist[i][j+1]>=zeit) tmp = erreichbar(i, j+1, zeit, gemachte_schritte+1);
if(!tmp && dist[i][j-1]>=zeit) tmp = erreichbar(i, j-1, zeit, gemachte_schritte+1);
return tmp;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
pair<int,int> start;
int n;
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];
while(lower<upper-1){
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, 0)){
//cout << "erreicht: " << zeit << endl;
lower = zeit;
}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, 0)) cout << "-1" << endl;
else cout << upper;
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 |
---|---|---|---|---|
Fetching results... |