結果

問題 No.340 雪の足跡
ユーザー DrafearDrafear
提出日時 2016-02-03 17:02:49
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,856 bytes
コンパイル時間 2,177 ms
コンパイル使用メモリ 170,488 KB
実行使用メモリ 36,916 KB
最終ジャッジ日時 2023-10-21 18:50:29
合計ジャッジ時間 6,388 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,732 KB
testcase_01 AC 3 ms
7,736 KB
testcase_02 AC 2 ms
5,704 KB
testcase_03 AC 3 ms
7,732 KB
testcase_04 AC 3 ms
7,736 KB
testcase_05 WA -
testcase_06 AC 3 ms
7,732 KB
testcase_07 AC 3 ms
5,744 KB
testcase_08 AC 3 ms
7,744 KB
testcase_09 AC 2 ms
7,724 KB
testcase_10 AC 3 ms
7,748 KB
testcase_11 AC 4 ms
9,940 KB
testcase_12 AC 4 ms
9,956 KB
testcase_13 AC 4 ms
10,096 KB
testcase_14 AC 13 ms
14,600 KB
testcase_15 AC 15 ms
16,672 KB
testcase_16 AC 10 ms
12,376 KB
testcase_17 AC 17 ms
16,716 KB
testcase_18 AC 15 ms
16,736 KB
testcase_19 AC 18 ms
16,716 KB
testcase_20 AC 128 ms
36,512 KB
testcase_21 AC 74 ms
7,740 KB
testcase_22 AC 20 ms
33,924 KB
testcase_23 WA -
testcase_24 AC 112 ms
36,376 KB
testcase_25 AC 142 ms
36,916 KB
testcase_26 AC 23 ms
14,272 KB
testcase_27 AC 6 ms
7,748 KB
testcase_28 AC 20 ms
14,456 KB
testcase_29 AC 148 ms
33,108 KB
testcase_30 AC 103 ms
22,380 KB
testcase_31 AC 156 ms
34,524 KB
testcase_32 AC 186 ms
36,916 KB
testcase_33 AC 154 ms
36,916 KB
testcase_34 AC 187 ms
36,916 KB
testcase_35 AC 150 ms
36,916 KB
testcase_36 AC 150 ms
36,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 int 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;
}

int W, H;
int m[2000][2000] = {0};
int mx[2000][2000] = {0};
int 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(int idx) {
	return P(idx%W, idx/W);
}
void paint(int a, int b) {
	P p1 = toP(a), p2 = toP(b);
	int x1 = p1.first, y1 = p1.second;
	int 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(int x, int y) {
	return x < 0 || x >= W || y < 0 || y >= H;
}
struct Node { int x, y, step; };
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	int N; cin >> W >> H >> N;
	REP(i,N) {
		int M; cin >> M;
		vector<int> v(M+1); cin >> v;
		REP(j,M) {
			paint(v[j], v[j+1]);
		}
	}
	initM();
/*
	REP(i,H) {
		REP(j,W) {
			cout << m[i][j] << " ";
		}
		cout << endl;
	}
*/
	vector< vector<int> > dist(H, vector<int>(W, inf));
	queue<Node> Q; Q.push({0, 0, 0}); dist[0][0] = 0;
	while ( !Q.empty() ) {
		Node node = Q.front(); Q.pop();
		int 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) {
			int nx = x + dx[d];
			int 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] == inf) {
		cout << "Odekakedekinai.." << endl;
	}
	else {
		cout << dist[H-1][W-1] << endl;
	}
}
0