結果

問題 No.340 雪の足跡
ユーザー maimai
提出日時 2016-06-13 19:02:31
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 374 ms / 1,000 ms
コード長 2,174 bytes
コンパイル時間 1,768 ms
コンパイル使用メモリ 175,276 KB
実行使用メモリ 23,372 KB
最終ジャッジ日時 2024-10-11 05:15:23
合計ジャッジ時間 8,069 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
typedef long long int ll;

int w,h;
int dp[1001][1001];
int vroad[1001][1001];
int hroad[1001][1001];

int vimos[1001][1001];
int himos[1001][1001];

int main(){
    int i,j,k,l;
    int x,y,d;
    int m,n;
    cin >>w>>h>>n;
    
    for (i=0;i<n;i++){
        int u1,u2;
        scanf("%d",&m);
        scanf("%d",&u1);
        for (j=1;j<=m;j++){
            scanf("%d",&u2);
            if (u1-w>=u2){ // down(flip->up)
                vimos[u2%w][u2/w]=max(u1/w-u2/w,vimos[u2%w][u2/w]);
            }else if (u1>u2){ // left
                himos[u2%w][u2/w]=max(u1-u2,himos[u2%w][u2/w]);
            }else if(u1+w>u2){ // right
                himos[u1%w][u1/w]=max(u2-u1,himos[u1%w][u1/w]);
            }else{ // up(flip->down)
                vimos[u1%w][u1/w]=max(u2/w-u1/w,vimos[u1%w][u1/w]);
            }
            u1=u2;
        }
    }
    for (x=0;x<w;x++){
        i=0;
        for (y=0;y<h;y++){
            if (i--<vimos[x][y] && vimos[x][y]) i=vimos[x][y]-1;
            vroad[x][y]=(0<=i);
        }
    }
    for (y=0;y<h;y++){
        i=0;
        for (x=0;x<w;x++){
            if (i--<himos[x][y] && himos[x][y]) i=himos[x][y]-1;
            hroad[x][y]=(0<=i);
        }
    }
    //for (y=0;y<h;y++){
    //    for (x=0;x<w;x++){
    //        printf("%d%d ",vroad[x][y],hroad[x][y]);
    //    }
    //    cout<<endl;
    //}
    
    queue<vector<int>> q;
    q.push(vector<int>{0,0,0});
    while (!q.empty()){
        vector<int> &e=q.front();
        x=e[0];y=e[1];d=e[2];q.pop();
        if (dp[x][y]) continue;
        if (x==w-1 && y==h-1){
            cout<<d<<endl;
            return 0;
        }
        //printf("%d %d %d\n",x,y,d);
        dp[x][y]=1;
        if (x!=0 && hroad[x-1][y]){q.push(vector<int>{x-1,y,d+1});}
        if (        hroad[x  ][y]){q.push(vector<int>{x+1,y,d+1});}
        if (y!=0 && vroad[x][y-1]){q.push(vector<int>{x,y-1,d+1});}
        if (        vroad[x][y  ]){q.push(vector<int>{x,y+1,d+1});}
    }
    cout<<"Odekakedekinai.."<<endl;
    return 0;
    
}
0