Submission #465145

#TimeUsernameProblemLanguageResultExecution timeMemory
465145MohamedFaresNebiliMecho (IOI09_mecho)C++14
4 / 100
58 ms34508 KiB
#include <bits/stdc++.h>
 
using namespace std;
 
using ll  = long long;
using ld  = long double;
using db  = double;
using ii  = pair<int, int>;
using pl  = pair<ll, ll>;
using vi  = vector<int>;
using vl  = vector<ll>;
using vii = vector<ii>;
using vpl = vector<pl>;
 
#define mp make_pair
#define pb push_back
#define pp pop_back
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define sz(v) ((int)v.size())
#define all(x) (x).begin() , (x).end()
#define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 
ld dist(ld x, ld y, ld a, ld b) { return sqrt((x-a)*(x-a) + (y-b)*(y-b)); }
ll gcd(ll a , ll b){ return b ? gcd(b , a % b) : a ;}
ll lcm(ll a , ll b){ return (a * b) / gcd(a , b);}
ll fact(ll n) { return n > 1?(n * fact(n-1)):1;}
void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void IO(string s = "") { if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } }
 
const int N = 2024;
const int inf = 1e9;
const int MOD = 1e9+7;
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up
const int dr[8] = {1,1,0,-1,-1,-1, 0, 1}, dc[8] = {0,1,1, 1, 0,-1,-1,-1};  // S,SE,E,NE,N,NW,W,SW
const long long INF = 1e18;
const double EPS = 1e-9;
const double PI = 3.14159265358979323846;
 
/** |||||||||||||||||||||||||||||||| SOLUTION |||||||||||||||||||||||||||||||| **/
 
ll n, m; char grid[N][N]; ll d[N][N];
ll vis[N][N]; ll sx, sy, hx, hy;
bool bfs(ll v)
{
    if(v*m>=d[sx][sy]) return false;
    memset(vis, 0, sizeof(vis)); vis[sx][sy]=1;
    deque<pair<ll, pl>>q; q.pb(mp(v*m, mp(sx, sy)));
    while(!q.empty()) {
        ll w=q.front().ff, a=q.front().ss.ff, b=q.front().ss.ss; q.pop_front();
        if(grid[a][b]=='D') return true;
        for(ll l=0;l<4;l++) {
            ll x=a+nx[l], y=b+ny[l];
            if(x>=0&&x<n&&y>=0&&y<n) {
                if(grid[x][y]=='T'||w+1>=d[x][y]||vis[x][y]) continue;
                q.pb(mp(w+1, mp(x, y))); vis[x][y]=1;
            }
        }
    }
    return false;
}
 
void solve()
{
    /// JUST KEEP GOING
    cin>>n>>m; deque<pl>q; memset(d, -1, sizeof d);
    for(ll l=0;l<n;l++) {
        for(ll i=0;i<n;i++) {
            cin>>grid[l][i]; 
            if(grid[l][i]=='H') { d[l][i]=0; q.pb(mp(l, i)); }
            else if(grid[l][i]=='M') { sx=l; sy=i; grid[l][i]=='G'; }
            else if(grid[l][i]=='D') { hx=l; hy=i; }
        }
    }
    while(!q.empty()) {
        ll a=q.front().ff, b=q.front().ss; q.pop_front();
        for(ll l=0;l<4;l++) {
            ll x=a+nx[l], y=b+ny[l];
            if(x>=0&&x<n&&y>=0&&y<n) {
                if(d[x][y]==-1&&grid[x][y]=='G') {
                    d[x][y]=d[a][b]+m; q.push_back(mp(x, y));
                }
            }
        }
    }
    ll lo=-1, hi=1e9; d[hx][hy]=1e9;
    while(hi-lo>1) {
        ll mid=(lo+hi)/2;
        if(bfs(mid)) { lo=mid; }
        else hi=mid;
    }
    cout<<lo<<'\n';
}
 
int32_t main()
{
 
    FAST; /// IO("lasers");
    int t; t=1;
    while(t--) {
        solve();
    }
    return 0;
 
}
 
/*** Stuff you should look for :
   * int overflow, array bounds.
   * special cases (n=1?).
   * Observe The Constraints.
   * do smth instead of nothing and stay organized.
   * Reformulate The Problem Into Something More Theoretical.
   * WRITE STUFF DOWN.
   * DON'T GET STUCK ON ONE APPROACH.
   * Don't Spend Too Much Time On The Problem: Move On!
   * If You Don't Fight You Cannot Win.
***/

Compilation message (stderr)

mecho.cpp: In function 'void solve()':
mecho.cpp:74:62: warning: statement has no effect [-Wunused-value]
   74 |             else if(grid[l][i]=='M') { sx=l; sy=i; grid[l][i]=='G'; }
      |                                                    ~~~~~~~~~~^~~~~
mecho.cpp: In function 'void setIn(std::string)':
mecho.cpp:30:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 | void setIn(string s) { freopen(s.c_str(),"r",stdin); }
      |                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
mecho.cpp: In function 'void setOut(std::string)':
mecho.cpp:31:32: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 | void setOut(string s) { freopen(s.c_str(),"w",stdout); }
      |                         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...