結果

問題 No.2928 Gridpath
ユーザー Kude
提出日時 2024-10-12 15:26:13
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,696 bytes
コンパイル時間 3,548 ms
コンパイル使用メモリ 269,696 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-12 15:26:18
合計ジャッジ時間 3,827 ms
ジャッジサーバーID
(参考情報)
judge2 / judge
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
const int di[] = {1, 0, -1, 0};
const int dj[] = {0, 1, 0, -1};

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w;
  cin >> h >> w;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  bool visited[6][6]{};
  int ans = 0;
  auto dfs = [&](auto&& self, int x, int y) -> void {
    if (x == gx && y == gy) {
      ans++;
      return;
    }
    visited[x][y] = true;
    rep(k1, 4) {
      int nx = x + di[k1], ny = y + dj[k1];
      if (0 <= nx && nx < h && 0 <= ny && ny < w && !visited[nx][ny]) {
        // neighbor check
        bool ok = true;
        rep(k2, 4) if ((k1 ^ 2) != k2) {
          int nx2 = nx + di[k2], ny2 = ny + dj[k2];
          if (0 <= nx2 && nx2 < h && 0 <= ny2 && ny2 < w && visited[nx2][ny2]) {
            ok = false;
            break;
          }
        }
        if (ok) self(self, nx, ny);
      }
    }
    visited[x][y] = false;
  };
  dfs(dfs, sx, sy);
  cout << ans << '\n';
}
0