Submission #901634


Source Code Expand

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
using namespace std;

#define rep(i,a,n) for(int i=a; i<n; i++)
#define repr(i,a,n) for(int i=a; i>=n; i--)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) )
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long int ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
int const MOD = 1000000007;

namespace std {
    bool operator<(const P& a, const P& b) {
        return a.X != b.X ? a.X < b.X : a.Y < b.Y;
    }
}

int r, c;
int sx, sy, gx, gy;
char board[51][51];
int dist[51][51];
bool isthrough[51][51];

int main() {
    cin >> r >> c;
    cin >> sx >> sy >> gx >> gy;
    sx--; sy--; gx--; gy--;
    rep(i,0,r) rep(j,0,c) cin >> board[i][j];

    dist[sx][sy] = 0;
    isthrough[sx][sy] = true;
    queue<pii> q;
    q.push(pii(sx, sy));

    while(!q.empty()) {
        pii t = q.front(); q.pop();
        if(t == pii(gx, gy)) break;
        int x, y; x = t.fr; y = t.sc;

        if(x != 0 && board[x-1][y] != '#' && !isthrough[x-1][y]) {
            dist[x-1][y] = dist[x][y] + 1;
            isthrough[x-1][y] = true;
            q.push(pii(x-1, y));
        }
        if(x != r-1 && board[x+1][y] != '#' && !isthrough[x+1][y]) {
            dist[x+1][y] = dist[x][y] + 1;
            isthrough[x+1][y] = true;
            q.push(pii(x+1, y));
        }
        if(y != 0 && board[x][y-1] != '#' && !isthrough[x][y-1]) {
            dist[x][y-1] = dist[x][y] + 1;
            isthrough[x][y-1] = true;
            q.push(pii(x, y-1));
        }
        if(y != c-1 && board[x][y+1] != '#' && !isthrough[x][y+1]) {
            dist[x][y+1] = dist[x][y] + 1;
            isthrough[x][y+1] = true;
            q.push(pii(x, y+1));
        }
    }
    cout << dist[gx][gy] << endl;
    return 0;
}

Submission Info

Submission Time
Task C - 幅優先探索
User tsutaj
Language C++ (G++ 4.6.4)
Score 100
Code Size 2596 Byte
Status AC
Exec Time 20 ms
Memory 924 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 3
AC × 25
Set Name Test Cases
Sample subtask0_sample01.txt, subtask0_sample02.txt, subtask0_sample03.txt
All subtask0_sample01.txt, subtask0_sample02.txt, subtask0_sample03.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask1_14.txt, subtask1_15.txt, subtask1_16.txt, subtask1_17.txt, subtask1_18.txt, subtask1_19.txt, subtask1_20.txt, subtask1_21.txt, subtask1_22.txt
Case Name Status Exec Time Memory
subtask0_sample01.txt AC 19 ms 788 KB
subtask0_sample02.txt AC 19 ms 796 KB
subtask0_sample03.txt AC 19 ms 796 KB
subtask1_01.txt AC 19 ms 924 KB
subtask1_02.txt AC 17 ms 796 KB
subtask1_03.txt AC 18 ms 800 KB
subtask1_04.txt AC 19 ms 924 KB
subtask1_05.txt AC 18 ms 796 KB
subtask1_06.txt AC 19 ms 796 KB
subtask1_07.txt AC 19 ms 800 KB
subtask1_08.txt AC 19 ms 900 KB
subtask1_09.txt AC 19 ms 800 KB
subtask1_10.txt AC 18 ms 924 KB
subtask1_11.txt AC 19 ms 796 KB
subtask1_12.txt AC 19 ms 800 KB
subtask1_13.txt AC 19 ms 840 KB
subtask1_14.txt AC 19 ms 800 KB
subtask1_15.txt AC 20 ms 800 KB
subtask1_16.txt AC 19 ms 800 KB
subtask1_17.txt AC 19 ms 800 KB
subtask1_18.txt AC 17 ms 800 KB
subtask1_19.txt AC 18 ms 792 KB
subtask1_20.txt AC 19 ms 924 KB
subtask1_21.txt AC 19 ms 796 KB
subtask1_22.txt AC 18 ms 924 KB