結果
| 問題 |
No.340 雪の足跡
|
| コンテスト | |
| ユーザー |
Drafear
|
| 提出日時 | 2016-02-03 17:04:59 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,896 bytes |
| コンパイル時間 | 1,429 ms |
| コンパイル使用メモリ | 169,384 KB |
| 実行使用メモリ | 47,104 KB |
| 最終ジャッジ日時 | 2024-09-21 20:09:07 |
| 合計ジャッジ時間 | 5,794 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 31 WA * 1 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
#define EACH(i,a) for (auto& i : a)
#define FOR(i,a,b) for (ll i=(a);i<(b);i++)
#define RFOR(i,a,b) for (ll i=(b)-1;i>=(a);i--)
#define REP(i,n) for (ll i=0;i<(n);i++)
#define RREP(i,n) for (ll i=(n)-1;i>=0;i--)
#define debug(x) cout<<#x<<": "<<x<<endl
#define pb push_back
#define ALL(a) (a).begin(),(a).end()
const ll linf = 1e18;
const ll inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);
template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
EACH(x,vec) is >> x;
return is;
}
template<typename T>
ostream& operator<<(ostream& os, vector<T>& vec) {
REP(i,vec.size()) {
if (i) os << " ";
os << vec[i];
}
return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector< vector<T> >& vec) {
REP(i,vec.size()) {
if (i) os << endl;
os << vec[i];
}
return os;
}
ll W, H;
ll m[2000][2000] = {0};
ll mx[2000][2000] = {0};
ll my[2000][2000] = {0};
void initM() {
REP(y, H+1) REP(x, W+1) {
mx[y][x+1] += mx[y][x];
}
REP(y, H+1) REP(x, W+1) {
my[y+1][x] += my[y][x];
}
REP(y, H+2) REP(x, W+2) {
m[y][x] = mx[y][x] + my[y][x];
}
}
P toP(ll idx) {
return P(idx%W, idx/W);
}
void pall(ll a, ll b) {
P p1 = toP(a), p2 = toP(b);
ll x1 = p1.first, y1 = p1.second;
ll x2 = p2.first, y2 = p2.second;
// cout << a << " " << b << " " << x1 << " " << y1 << " " << x2 << " " << y2 << endl;
if (x1 > x2) swap(x1, x2);
if (y1 > y2) swap(y1, y2);
if (x1 == x2) {
++my[y1][x1];
--my[y2+1][x1];
}
else {
assert(y1 == y2);
++mx[y1][x1];
--mx[y1][x2+1];
}
}
bool isOut(ll x, ll y) {
return x < 0 || x >= W || y < 0 || y >= H;
}
struct Node { ll x, y, step; };
ll dx[] = {0, 1, 0, -1};
ll dy[] = {-1, 0, 1, 0};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
ll N; cin >> W >> H >> N;
if (W == 1 && H == 1) {
cout << "0" << endl;
return 0;
}
REP(i,N) {
ll M; cin >> M;
vector<ll> v(M+1); cin >> v;
REP(j,M) {
pall(v[j], v[j+1]);
}
}
initM();
/*
REP(i,H) {
REP(j,W) {
cout << m[i][j] << " ";
}
cout << endl;
}
*/
vector< vector<ll> > dist(H, vector<ll>(W, linf));
queue<Node> Q; Q.push({0, 0, 0}); dist[0][0] = 0;
while ( !Q.empty() ) {
Node node = Q.front(); Q.pop();
ll x = node.x, y = node.y, step = node.step;
if ( isOut(x, y) ) continue;
if (step > dist[y][x]) continue;
if (!m[y][x]) continue;
REP(d, 4) {
ll nx = x + dx[d];
ll ny = y + dy[d];
if ( !isOut(nx, ny) && step+1 < dist[ny][nx] ) {
if ( (d % 2 == 0 && my[ny][nx] && my[y][x]) || (d % 2 == 1 && mx[ny][nx] && mx[y][x]) ) {
dist[ny][nx] = step+1;
Q.push({nx, ny, step+1});
}
}
}
}
if (!m[H-1][W-1] || dist[H-1][W-1] == linf) {
cout << "Odekakedekinai.." << endl;
}
else {
cout << dist[H-1][W-1] << endl;
}
}
Drafear