結果

問題 No.3063 幅優先探索
ユーザー jj1gujjj1guj
提出日時 2020-11-04 02:44:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 878 bytes
コンパイル時間 1,740 ms
コンパイル使用メモリ 203,232 KB
実行使用メモリ 9,164 KB
最終ジャッジ日時 2023-09-29 15:25:54
合計ジャッジ時間 2,856 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 27 ms
9,108 KB
testcase_07 AC 35 ms
9,164 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct place{
    int y;
    int x;
};

int R,C;
int dist[1000][1000];
int dy[4]={-1,1,0,0};
int dx[4]={0,0,-1,1};
place sn,gn,cn,nn;

string G[1000];

int main(){
    int i,j;
    cin>>R>>C>>sn.y>>sn.x>>gn.y>>gn.x;
    --sn.y;
    --sn.x;
    --gn.y;
    --gn.x;
    for(i=0;i<R;++i)cin>>G[i];
    for(i=0;i<R;++i)for(j=0;j<C;++j)dist[i][j]=-1;
    dist[sn.y][sn.x]=0;
    queue<place>que;
    que.push(sn);
    while(!que.empty()){
        cn=que.front();
        if(cn.y==gn.y && cn.x==gn.x)break;
        que.pop();
        for(i=0;i<4;++i){
            nn.y=cn.y+dy[i];
            nn.x=cn.x+dx[i];
            if(nn.y>=0 && nn.y<R && nn.x>=0 && nn.x<C && dist[nn.y][nn.x]==-1){
                dist[nn.y][nn.x]=dist[cn.y][cn.x]+1;
                que.push(nn);
            }
        }
    }
    cout<<dist[gn.x][gn.y]<<"\n";
}
0