# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1008468 | ByeWorld | Mecho (IOI09_mecho) | C++14 | 215 ms | 14940 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
#define ll long long
#define int long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<int,int> pii;
typedef pair<pii, int> ipii;
const int MAXN = 1010;
const int MAXA = 2e3+10;
const int INF = 1e18+10;
const int LOG = 63;
const int MOD = 1e9+7;
const int SQRT = 450;
const vector<int> NOL = {};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const vector<int> dx = {1, -1, 0, 0};
const vector<int> dy = {0, 0, 1, -1};
int n, m, k;
char c[MAXN][MAXN];
int dis[MAXN][MAXN], val[MAXN][MAXN];
int sta, en;
bool valid(int x, int y){
if(x<1 || x>n || y<1 || y>m) return 0;
if(c[x][y] == 'T') return 0;
return 1;
}
queue <pii> q;
void bfs(){
while(!q.empty()){
int dist = q.front().fi, x = q.front().se/MAXN, y = q.front().se%MAXN;
q.pop();
if(dist > dis[x][y]) continue;
for(int i=0; i<4; i++){
int nx = x+dx[i], ny = y+dy[i];
if(!valid(nx, ny)) continue;
if(c[nx][ny] == 'D') continue;
if(dis[nx][ny] <= dis[x][y]+k) continue;
dis[nx][ny] = dis[x][y]+k;
q.push({dis[nx][ny], nx*MAXN+ny});
}
}
}
bool bisa(int num){
if(dis[sta/MAXN][sta%MAXN] <= num) return 0;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++) val[i][j] = INF;
q.push({num, sta}); val[sta/MAXN][sta%MAXN] = num;
while(!q.empty()){
int dist = q.front().fi, x = q.front().se/MAXN, y = q.front().se%MAXN;
q.pop();
if(dist >= dis[x][y]) continue; // bee ke sini
if(dist > val[x][y]) continue;
for(int i=0; i<4; i++){
int nx = x+dx[i], ny = y+dy[i];
if(!valid(nx, ny)) continue;
if(val[nx][ny] <= val[x][y]+1) continue;
if(dis[nx][ny] <= val[x][y]+1) continue;
val[nx][ny] = val[x][y]+1;
q.push({val[nx][ny], nx*MAXN+ny});
}
}
return (val[en/MAXN][en%MAXN] < INF);
}
signed main(){
// ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> k; m = n;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++) dis[i][j] = INF;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
cin >> c[i][j];
if(c[i][j]=='H'){
q.push({0, i*MAXN+j});
dis[i][j] = 0;
} else if(c[i][j]=='D') en = i*MAXN+j;
else if(c[i][j]=='M') sta = i*MAXN+j;
}
}
bfs();
// for(int i=1; i<=n; i++){
// for(int j=1; j<=m; j++) cout << dis[i][j] << " \n"[j==m];
// }
int l=0, r=INF, mid=0, cnt=-1;
while(l<=r){
mid = md;
if(bisa(mid)){ cnt = mid; l = mid+1;}
else r = mid-1;
}
cout << (cnt==-1 ? -1 : cnt/k) << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |