結果
| 問題 |
No.2928 Gridpath
|
| コンテスト | |
| ユーザー |
startcpp
|
| 提出日時 | 2024-10-12 15:34:23 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 12 ms / 2,000 ms |
| コード長 | 2,808 bytes |
| コンパイル時間 | 1,154 ms |
| コンパイル使用メモリ | 103,164 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-12 15:34:26 |
| 合計ジャッジ時間 | 1,982 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 20 |
ソースコード
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cstdio>
#include <cmath>
#include <cassert>
#define rep(i, n) for(i = 0; i < n; i++)
#define int long long
using namespace std;
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
int h, w, sy, sx, gy, gx;
void dfs(int y, int x, vector<bool> &used, vector<int> &path, vector<vector<int>> &paths) {
path.push_back(y * w + x);
used[y * w + x] = true;
bool ng = false;
for (int i = 0; i < h - 1; i++) {
for (int j = 0; j < w - 1; j++) {
if (used[i * w + j] && used[i * w + j + 1] && used[(i + 1) * w + j] && used[(i + 1) * w + j + 1]) {
ng = true;
break;
}
}
if (ng) break;
}
if (ng) {
used[y * w + x] = false;
path.pop_back();
return;
}
if (y == gy && x == gx) {
paths.push_back(path);
used[y * w + x] = false;
path.pop_back();
return;
}
for (int i = 0; i < 4; i++) {
int ny = y + dy[i];
int nx = x + dx[i];
if (ny < 0 || ny >= h || nx < 0 || nx >= w || used[ny * w + nx]) continue;
dfs(ny, nx, used, path, paths);
}
path.pop_back();
used[y * w + x] = false;
}
bool check(vector<int> path) {
const int INF = 114514;
int i;
vector<vector<bool>> wall(h, vector<bool>(w, true));
vector<vector<int>> dist(h, vector<int>(w, INF));
rep(i, path.size()) { wall[path[i] / w][path[i] % w] = false; }
typedef pair<int, int> P;
queue<P> que;
que.push(P(sy, sx));
dist[sy][sx] = 0;
while (!que.empty()) {
P now = que.front();
que.pop();
int y = now.first;
int x = now.second;
rep(i, 4) {
int ny = y + dy[i];
int nx = x + dx[i];
if (ny < 0 || ny >= h || nx < 0 || nx >= w || wall[ny][nx] || dist[ny][nx] <= dist[y][x] + 1) continue;
dist[ny][nx] = dist[y][x] + 1;
que.push(P(ny, nx));
}
}
/*cout << "path: "; rep(i, path.size()) cout << path[i] << " "; cout << endl;
rep(i, h) {
for (int j = 0; j < w; j++) {
if (dist[i][j] >= INF) cout << "x ";
else cout << dist[i][j] << " ";
}
cout << endl;
}*/
return dist[gy][gx] == path.size() - 1;
}
signed main() {
cin >> h >> w >> sy >> sx >> gy >> gx;
sy--; sx--; gy--; gx--;
vector<int> path;
vector<vector<int>> paths;
vector<bool> used(h * w);
dfs(sy, sx, used, path, paths);
int ans = 0;
for (vector<int> path: paths) {
if (check(path)) ans++;
}
cout << ans << endl;
return 0;
}
startcpp