結果

問題 No.340 雪の足跡
ユーザー はまやんはまやん
提出日時 2017-02-26 17:18:26
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 239 ms / 1,000 ms
コード長 3,373 bytes
コンパイル時間 1,959 ms
コンパイル使用メモリ 179,308 KB
実行使用メモリ 13,100 KB
最終ジャッジ日時 2024-06-11 16:44:57
合計ジャッジ時間 5,907 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define INF INT_MAX/2
struct SectionStructure {
set<pair<int, int>> buf;
SectionStructure() {
buf.insert({ -INF, -INF });
buf.insert({ INF , INF });
}
void add(int l, int r) {
auto ite = buf.lower_bound({l, -INF});
if (ite->first == INF) { //
ite--;
if (ite->second <= l) { //
buf.insert({ l, r });
} else { //
int L = ite->first;
int R = ite->second;
buf.erase(ite);
buf.insert({ L, max(r, R) });
}
} else { //
auto pre = buf.upper_bound({r, -INF});
auto pp = pre;
pp--;
int L = min(l, ite->first);
int R = max(r, pp->second);
buf.erase(ite, pre);
buf.insert({ L, R });
}
}
set<pair<int, int>>::iterator get(int x) {
auto ite = buf.lower_bound({ x, -INF });
if (ite->first == INF) return buf.end();
if (ite->second < x) return buf.end();
return ite;
}
set<pair<int, int>>::iterator end() { return buf.end(); }
int check(int a, int b) {
if (a > b) swap(a, b);
auto aa = buf.upper_bound({ a, INF });
aa--;
if (aa->first == -INF) return false;
return b <= aa->second;
}
void print() {
printf("<>\n");
for (auto p : buf) printf("[%d,%d]\n", p.first, p.second);
}
};
//-----------------------------------------------------------------
int W, H, N;
int M[1010];
int B[1010][1010];
//-----------------------------------------------------------------
SectionStructure vec[1010];
SectionStructure hor[1010];
void test() {
SectionStructure ss;
ss.add(0, 1);
ss.print();
/*ss.add(2, 3);
ss.print();
ss.add(2, 5);
ss.print();*/
cout << ((int)ss.check(1, 2)) << endl;
}
//-----------------------------------------------------------------
void makeEdge() {
rep(i, 0, N) {
rep(j, 0, M[i]) {
int a = B[i][j];
int b = B[i][j + 1];
if (a > b) swap(a, b);
int ya = a / W; int xa = a % W;
int yb = b / W; int xb = b % W;
if (ya == yb) { // x
hor[ya].add(xa, xb);
} else { // y
vec[xa].add(ya, yb);
}
}
}
}
//-----------------------------------------------------------------
bool done[1010][1010];
int dist[1010][1010];
int dx[4] = { 0, 1, 0, -1 };
int dy[4] = { -1, 0, 1, 0 };
int bfs() {
queue<int> que;
done[0][0] = true;
dist[0][0] = 0;
que.push(0);
while (!que.empty()) {
int y = que.front() / 1010;
int x = que.front() % 1010;
que.pop();
if (y == H - 1 && x == W - 1) return dist[y][x];
rep(i, 0, 4) {
int xx = x + dx[i];
int yy = y + dy[i];
if (xx < 0 || W <= xx) continue;
if (yy < 0 || H <= yy) continue;
if (i % 2 == 1) {
if (!hor[y].check(x, xx)) continue;
} else {
if (!vec[x].check(y, yy)) continue;
}
if (done[yy][xx]) continue;
done[yy][xx] = 1;
dist[yy][xx] = dist[y][x] + 1;
que.push(yy * 1010 + xx);
}
}
return -1;
}
//-----------------------------------------------------------------
int main() {
//test(); return 0;
cin >> W >> H >> N;
rep(i, 0, N) {
scanf("%d", &M[i]);
rep(j, 0, M[i] + 1) scanf("%d", &B[i][j]);
}
makeEdge();
//rep(y, 0, H) hor[y].print();
//rep(x, 0, W) vec[x].print();
int ans = bfs();
if (0 <= ans)
cout << ans << endl;
else
cout << "Odekakedekinai.." << endl;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0