結果

問題 No.971 いたずらっ子
ユーザー ateate
提出日時 2020-01-17 23:57:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,885 bytes
コンパイル時間 3,423 ms
コンパイル使用メモリ 225,504 KB
実行使用メモリ 68,236 KB
最終ジャッジ日時 2023-09-08 08:56:56
合計ジャッジ時間 9,596 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(ll i=0;i<(n);++i)
using ll = long long;
using pll = pair<ll,ll>;
constexpr ll INF = (1LL<<60);
constexpr ll MOD = (1e9+7);
//constexpr ll MOD = (998244353);
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}return 0;}
#if loc||debg||local||debug
void dump(){cerr<<endl;}
template<class T,class... Ts> void dump(T&& h, Ts&&... t){cerr<<h<<", ";dump(forward<Ts>(t)...);}
#else
template<class T,class... Ts> void dump(T&& h, Ts&&... t){}
#endif
template<class T> istream &operator>>(istream&is,vector<T>&v){for(auto &elemnt:v)is>>elemnt;return is;}
template<class T,class U> istream &operator>>(istream&is,pair<T,U>&p){is>>p.first>>p.second;return is;}
template<class T> ostream &operator<<(ostream& os,vector<T>const& v){for(auto const& vi:v)os<<vi<<" ";return os;}
template<class T>vector<T> vec(size_t a){return vector<T>(a);}
template<class T, class... Ts>auto vec(size_t a, Ts... ts){return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...));}
template<class T>vector<T> _vec(size_t a,T v){return vector<T>(a,v);}
template<class T, class... Ts>auto _vec(size_t a, Ts... ts){return vector<decltype(_vec<T>(ts...))>(a, _vec<T>(ts...));}

constexpr ll dx[]={1,0,-1,0,1,1,-1,-1};
constexpr ll dy[]={0,1,0,-1,1,-1,1,-1};

int main(){

  int h,w;
  cin>>h>>w;
  vector<string> s(h);
  cin>>s;

  auto grid_bfs = [&](int sy,int sx){
    using T3 = std::tuple<int,int,int>;
    auto chmin = [](auto& a,auto b){if(a>b){a=b;return true;}return false;};
    constexpr int inf = 1<<29;
    constexpr int dx[] = {0,0,1,-1,1,-1,1,-1};
    constexpr int dy[] = {1,-1,0,0,1,-1,-1,1};
    std::queue<T3> que;
    que.emplace(sy,sx,0);
    auto dist = std::vector<std::vector<int>>(h,std::vector<int>(w,inf));
    while(!que.empty()){
      int y,x,c;
      std::tie(y,x,c) = que.front();
      que.pop();
      if(chmin(dist[y][x],c)){
        for(int i=0;i<4;++i){
          int ny=y+dy[i];
          int nx=x+dx[i];
          if(ny==-1||nx==-1)continue;
          if(ny==h||nx==w)continue;     //
          //if(s[ny][nx]!='.')continue;   //
          que.emplace(ny,nx,c+1);
        }
      }
    }
    return dist;
  };

  auto d0 = grid_bfs(0,0);

  using T3 = tuple<ll,ll,ll>;
  using T4 = tuple<ll,ll,ll,ll>;
  priority_queue<T3,vector<T3>,greater<>> que;
  que.emplace(0,0,0);
  auto dist = _vec<ll>(h,w,INF);
  while(!que.empty()){
    auto [y,x,c] = que.top();
    que.pop();
    if(chmin(dist[y][x],c)){
      rep(i,4){
        auto ny = y+dy[i];
        auto nx = x+dx[i];
        auto nc = c+1;
        if(ny==-1||nx==-1)continue;
        if(ny==h||nx==w)continue;
        if(s[ny][nx]=='k')nc += d0[ny][nx];
        que.emplace(ny,nx,nc);
      }
    }
  }
  for(auto y:dist)dump(y);
  cout<<(dist.back().back())<<endl;


}
0