結果

問題 No.340 雪の足跡
ユーザー rickytheta
提出日時 2016-01-29 23:43:43
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 490 ms / 1,000 ms
コード長 3,521 bytes
コンパイル時間 1,411 ms
コンパイル使用メモリ 165,528 KB
実行使用メモリ 23,168 KB
最終ジャッジ日時 2024-09-21 18:48:57
合計ジャッジ時間 7,967 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int,int> pii;
#define REP(i,n) for(ll i=0;i<n;++i)
#define REPR(i,n) for(ll i=1;i<n;++i)
#define FOR(i,a,b) for(ll i=a;i<b;++i)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()
#define MOD (ll)(1e9+7)
#define ADD(a,b) a=((a)+(b))%MOD
#define FIX(a) ((a)%MOD+MOD)%MOD
#define V_MAX 1000010
#define INF 100000000
int g[V_MAX];
int lt[V_MAX][4];
// right, up, left, down
int dir[4] = {1,2,4,8};
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
int w,h;
int gp(int x,int y){
return x+y*w;
}
void add_edge(int src,int dst){
if(src>dst)swap(src,dst);
int sx,sy,tx,ty;
sx = src%w;
sy = src/w;
tx = dst%w;
ty = dst/w;
if(sx==tx){
FOR(i,sy,ty){
if(lt[gp(sx,i)][1]!=gp(sx,i)){
i = lt[gp(sx,i)][1]/w;
if(i>=ty)break;
}
g[sx+i*w] |= dir[1];
g[sx+(i+1)*w] |= dir[3];
lt[sx+i*w][1] = max(lt[sx+i*w][1],dst);
}
}else{
FOR(i,sx,tx){
if(lt[gp(i,sy)][0]!=gp(i,sy)){
i = lt[gp(i,sy)][0]%w;
if(i>=tx)break;
}
g[sy*w+i] |= dir[0];
g[sy*w+i+1] |= dir[2];
lt[sy*w+i][0] = max(lt[sy*w+i][0],dst);
}
}
}
int main(){
// w,h<1e3
// |V| = wh < 1e6
// n < 1e3
// m < 1e3
// |E| = nm < 1e6
int n;
cin>>w>>h>>n;
if(w==1 && h==1){
cout<<0<<endl;
return 0;
}
REP(i,w*h)REP(j,4)lt[i][j]=i;
while(n--){
int m;
cin>>m;
int bef;
cin>>bef;
REP(i,m){
int cur;
cin>>cur;
add_edge(bef,cur);
bef = cur;
}
}
queue<pii> Q;
int s = 0;
int t = w*h-1;
Q.push(make_pair(0,0));
int res = -1;
vector<bool> used(w*h,false);
while(!Q.empty()){
pii P = Q.front(); Q.pop();
// int pos = P.second;
int pos = P.first;
if(used[pos])continue;
used[pos] = true;
// int wei = INF-P.first;
int wei = P.second;
int x = pos%w;
int y = pos/w;
// FOR(i,0,2){
// int dst = g[pos][i];
// int tx = x+dx[i];
// int ty = y+dy[i];
// int tpos = gp(tx,ty);
// int cnt = 1;
// while(0<=tx && tx<w && 0<=ty && ty<h && tpos <= dst && !used[tpos]){
// if(tpos==t){
// cout<<(wei+1)<<endl;
// return 0;
// }
// Q.push(make_pair(INF-wei-cnt,tpos));
// tx += dx[i];
// ty += dy[i];
// tpos = gp(tx,ty);
// ++cnt;
// }
// }
// FOR(i,2,4){
// int dst = g[pos][i];
// int tx = x+dx[i];
// int ty = y+dy[i];
// int tpos = gp(tx,ty);
// int cnt = 1;
// while(0<=tx && tx<w && 0<=ty && ty<h && tpos >= dst && !used[tpos]){
// if(tpos==t){
// cout<<(wei+1)<<endl;
// return 0;
// }
// Q.push(make_pair(INF-wei-cnt,tpos));
// tx += dx[i];
// ty += dy[i];
// tpos = gp(tx,ty);
// ++cnt;
// }
// }
REP(i,4){
if(!(g[pos] & dir[i]))continue;
int tx = x+dx[i];
int ty = y+dy[i];
if(tx<0 || tx>=w)continue;
if(ty<0 || ty>=h)continue;
int tpos = ty*w+tx;
if(tpos<0 || tpos>=w*h)continue;
if(used[tpos])continue;
if(tpos == t){
cout<<(wei+1)<<endl;
return 0;
}
Q.push(make_pair(tpos,wei+1));
}
}
cout<<"Odekakedekinai.."<<endl;
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0