結果

問題 No.2731 Two Colors
ユーザー 唐揚げが壊わ唐揚げが壊わ
提出日時 2024-04-19 22:11:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 429 ms / 3,000 ms
コード長 9,552 bytes
コンパイル時間 3,066 ms
コンパイル使用メモリ 236,172 KB
実行使用メモリ 25,344 KB
最終ジャッジ日時 2024-10-11 15:32:28
合計ジャッジ時間 10,008 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 429 ms
25,344 KB
testcase_01 AC 426 ms
25,216 KB
testcase_02 AC 429 ms
25,216 KB
testcase_03 AC 5 ms
5,248 KB
testcase_04 AC 22 ms
5,248 KB
testcase_05 AC 15 ms
5,248 KB
testcase_06 AC 58 ms
6,816 KB
testcase_07 AC 73 ms
7,936 KB
testcase_08 AC 10 ms
5,248 KB
testcase_09 AC 271 ms
18,556 KB
testcase_10 AC 5 ms
5,248 KB
testcase_11 AC 234 ms
16,968 KB
testcase_12 AC 271 ms
18,668 KB
testcase_13 AC 197 ms
14,996 KB
testcase_14 AC 117 ms
10,240 KB
testcase_15 AC 9 ms
5,248 KB
testcase_16 AC 95 ms
8,988 KB
testcase_17 AC 147 ms
11,672 KB
testcase_18 AC 25 ms
5,248 KB
testcase_19 AC 99 ms
9,252 KB
testcase_20 AC 179 ms
13,824 KB
testcase_21 AC 26 ms
5,248 KB
testcase_22 AC 267 ms
18,288 KB
testcase_23 AC 63 ms
7,168 KB
testcase_24 AC 32 ms
5,332 KB
testcase_25 AC 3 ms
5,248 KB
testcase_26 AC 219 ms
15,532 KB
testcase_27 AC 272 ms
19,384 KB
testcase_28 AC 162 ms
13,044 KB
testcase_29 AC 49 ms
6,144 KB
testcase_30 AC 95 ms
9,216 KB
testcase_31 AC 55 ms
6,664 KB
testcase_32 AC 317 ms
20,884 KB
testcase_33 AC 2 ms
5,248 KB
testcase_34 AC 2 ms
5,248 KB
testcase_35 AC 2 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

// #include <atcoder/all>

// Ctrl + end

using namespace std;
using str = string;
using vs = vector<string>;
using ll = long long;
using ld = long double;
using vll = vector<long long>;
using vvll = vector<vector<long long>>;
using vvvll = vector<vector<vector<long long>>>;
using vvvvll = vector<vector<vector<vector<long long>>>>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using pll = pair<ll, ll>;
using vpll = vector<pair<ll, ll>>;

#define REP1(i, n) for (long long i = 0; i < n; i++)
#define REP2(i, n, m) for (long long i = n; i < m; i++)
#define REP3(i, n, d, m) \
  for (long long i = n; (n < m) ? (i < m) : (i > m); i += d)
#define OVERLOAD_REP(a, b, c, d, e, ...) e
#define REP(...) OVERLOAD_REP(__VA_ARGS__, REP3, REP2, REP1)(__VA_ARGS__)

#define REPv(itr, v) for (auto itr = v.begin(); itr != v.end(); itr++)

#define REPIN(itr, v)                                 \
  for (auto itr = v.begin(); itr != v.end(); itr++) { \
    cin >> *itr;                                      \
  }
#define been(vec) vec.begin(), vec.end()
#define LIN                   \
  = []() {                    \
    long long LONGLONG_INPUT; \
    cin >> LONGLONG_INPUT;    \
    return LONGLONG_INPUT;    \
  }()
#define SIN              \
  = []() {               \
    string STRING_INPUT; \
    cin >> STRING_INPUT; \
    return STRING_INPUT; \
  }()
#define SPNL(i, SIZE) (i + 1 == SIZE ? '\n' : ' ')

template <class T>
constexpr T gcd(const T &x, const T &y) {
  return (x % y) ? gcd(y, x % y) : y;
}
template <class T, class... R>
constexpr T gcd(const T &x, const R &...y) {
  return gcd(x, gcd(y...));
}
template <class T>
constexpr T lcm(const T &x, const T &y) {
  return x / gcd(x, y) * y;
}
template <class T, class... R>
constexpr T lcm(const T &x, const R &...y) {
  return lcm(x, lcm(y...));
}

template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// 3要素で比較するとfunc(a,b,comp)と勘違いされるから「{...}」使う
template <class... T>
constexpr auto min(const T &...a) {
  return min(initializer_list<common_type_t<T...>>{a...});
}
template <class... T>
constexpr auto max(const T &...a) {
  return max(initializer_list<common_type_t<T...>>{a...});
}

// tuple処理
template <size_t N = 0, class T, class F>
void iterate_tuple(T &t, const F &func) {
  if constexpr (N < tuple_size_v<T>) {
    auto &x = get<N>(t);
    func(x);
    iterate_tuple<N + 1>(t, func);
  }
}

// ストリーム演算子 入力
template <class T1, class T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class... T>
istream &operator>>(istream &is, tuple<T...> &t) {
  auto func = [&is](auto &t) { is >> t; };
  iterate_tuple(t, func);
  return is;
}
template <class T>
istream &operator>>(istream &is, vector<T> &v) {
  for (size_t i = 0, n = v.size(); i < n; i++) {
    is >> v[i];
  }
  return is;
}

// ストリーム演算子 出力
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << p.first << " " << p.second << " ";
  return os;
}
template <class... T>
ostream &operator<<(ostream &os, const tuple<T...> &t) {
  auto func = [&os](auto t) { os << t << " "; };
  iterate_tuple(t, func);
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (size_t i = 0, n = v.size(); i < n; i++) {
    os << v[i] << " ";
  }
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v) {
  for (size_t i = 0, n = v.size(); i < n; i++) {
    os << v[i] << endl;
  }
  return os;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const map<T1, T2> &mp) {
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    os << "[" << itr->first << "]" << itr->second << endl;
  }
  return os;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp) {
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    os << "[" << itr->first << "]" << itr->second << endl;
  }
  return os;
}

template <class T>
void input(T &a) {
  cin >> a;
}
template <class T, class... R>
void input(T &a, R &...b) {
  input(a), input(b...);
}

template <class... T>
constexpr void output(const T &...a) {
  (cout << ... << (cout << a, " "));
  cout << endl;
}

template <class T1, class T2>
pair<T1, T2> operator+(const pair<T1, T2> &a, const pair<T1, T2> &b) {
  pair<T1, T2> ret;
  ret = make_pair(a.first + b.first, a.second + b.second);
  return ret;
}
template <class T1, class T2, class T3>
pair<T1, T2> operator+(const pair<T1, T2> &a, const T3 &b) {
  pair<T1, T2> ret;
  ret = make_pair(a.first + b, a.second + b);
  return ret;
}
template <class T1, class T2>
pair<T1, T2> operator-(const pair<T1, T2> &a, const pair<T1, T2> &b) {
  pair<T1, T2> ret;
  ret = make_pair(a.first - b.first, a.second - b.second);
  return ret;
}
template <class T1, class T2, class T3>
pair<T1, T2> operator-(const pair<T1, T2> &a, const T3 &b) {
  pair<T1, T2> ret;
  ret = make_pair(a.first - b, a.second - b);
  return ret;
}
template <class T>
vector<T> operator+(const vector<T> &a, const vector<T> &b) {
  // aとbのサイズは一致する必要がある。
  assert(a.size() == b.size());
  vector<T> ret = a;
  for (size_t i = 0; i < ret.size(); i++) {
    ret[i] = a[i] + b[i];
  }
  return ret;
}
template <class T1, class T2>
vector<T1> operator+(const vector<T1> &a, const T2 &b) {
  vector<T1> ret = a;
  for (size_t i = 0; i < ret.size(); i++) {
    ret[i] = a[i] + b;
  }
  return ret;
}

// 二次元配列aを右回転する
template <class T>
constexpr vector<vector<T>> rotate_90deg(const vector<vector<T>> &a) {
  vector<vector<T>> b(a.front().size(), vector<T>(a.size()));
  for (size_t i = 0; i < a.front().size(); i++) {
    for (size_t j = 0; j < a.size(); j++) {
      b[i][j] = (a[a.size() - 1 - j][i]);
    }
  }
  return b;
}
// 二次元配列aをelementで指定した要素を含み切るようにクリッピング
vector<string> clip_vec2(const vector<string> &a, const char element) {
  ll left = LLONG_MAX, right = -1, top = LLONG_MAX, bottom = -1;
  for (size_t i = 0; i < a.size(); i++) {
    for (size_t j = 0; j < a.front().size(); j++) {
      if (a[i][j] == element) {
        chmin(top, (ll)i);
        chmax(bottom, (ll)i);
        chmin(left, (ll)j);
        chmax(right, (ll)j);
      }
    }
  }
  vector<string> b(abs(bottom - top), string(abs(right - left), ' '));
  for (size_t i = 0; i < b.size(); i++) {
    for (size_t j = 0; j < b.front().size(); j++) {
      b[i][j] = a[i + top][j + left];
    }
  }
  return b;
}
template <class T>
constexpr vector<vector<T>> clip_vec2(const vector<vector<T>> &a,
                                      const T element) {
  ll left = LLONG_MAX, right = -1, top = LLONG_MAX, bottom = -1;
  for (size_t i = 0; i < a.size(); i++) {
    for (size_t j = 0; j < a.front().size(); j++) {
      if (a[i][j] == element) {
        chmin(top, (ll)i);
        chmax(bottom, (ll)i);
        chmin(left, (ll)j);
        chmax(right, (ll)j);
      }
    }
  }
  vector<vector<T>> b(abs(bottom - top), vector<T>(abs(right - left)));
  for (size_t i = 0; i < b.size(); i++) {
    for (size_t j = 0; j < b.front().size(); j++) {
      b[i][j] = a[i + top][j + left];
    }
  }
  return b;
}
// 二次元配列aをelementで指定した要素で囲い込む
vector<string> envelope_vec2(const vector<string> &a, const char element) {
  vector<string> b = a;
  for (size_t i = 0; i < b.size(); i++) {
    b[i].insert(b[i].end(), {element, element});
    std::rotate(b[i].rbegin(), b[i].rbegin() + 1, b[i].rend());
  }
  b.push_back(string(b[0].size(), element));
  b.push_back(b.back());
  std::rotate(b.rbegin(), b.rbegin() + 1, b.rend());
  return b;
}
template <class T>
constexpr vector<vector<T>> envelope_vec2(const vector<vector<T>> &a,
                                          const T element) {
  vector<vector<T>> b = a;
  for (size_t i = 0; i < b.size(); i++) {
    b[i].insert(b[i].end(), {element, element});
    std::rotate(b[i].rbegin(), b[i].rbegin() + 1, b[i].rend());
  }
  b.push_back(vector<T>(b[0].size(), element));
  b.push_back(b.back());
  std::rotate(b.rbegin(), b.rbegin() + 1, b.rend());
  return b;
}

const long long mod1000 = 1000000007;
const long long mod998 = 998244353;
const string yes = "Yes";
const string no = "No";

int main() {
  ll h, w;
  input(h, w);
  vvll a(h, vll(w));
  input(a);

  a = envelope_vec2(a, LLONG_MAX);

  vector<priority_queue<pair<ll, pll>, vector<pair<ll, pll>>, greater<>>> x(2);
  vpll d{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

  vvll p(h + 2, vll(w + 2, 0b1000));
  x[0].push({-1ll, {1ll, 1ll}});
  x[1].push({-1ll, {h, w}});
  p[1][1] = 0;
  p[h][w] = 1;
  ll cnt = 0;
  while (1) {
    auto [num, poi] = x[cnt % 2].top();
    x[cnt % 2].pop();
    p[poi.first][poi.second] = cnt % 2;
#if LOCAL_ENVIROMENT_FLAG
    output(p);
#endif
    REP(i, 4) {
      pll npoi = poi + d[i];
      if (p[npoi.first][npoi.second] == (cnt + 1) % 2) {
        cout << cnt - 1 << endl;
        exit(0);
      }
      if (p[npoi.first][npoi.second] <= 1 ||
          (p[npoi.first][npoi.second] | (2 << (cnt % 2))) ==
              p[npoi.first][npoi.second])
        continue;
      p[npoi.first][npoi.second] |= (2 << (cnt % 2));
      x[cnt % 2].emplace(a[npoi.first][npoi.second], npoi);
    }
    cnt++;
  }
}
0