Submission #8831748


Source Code Expand

Copy
import std.stdio : writeln, readln;
import std.string : chomp, split;
import std.conv : to;
import std.container : DList;
import std.typecons : Tuple;

void main()
{
    long R, C, SY, SX, GY, GX;
    {
        auto tmp = readln().split().to!(long[])();
        R = tmp[0];
        C = tmp[1];
    }
    {
        auto tmp = readln().split().to!(long[])();
        SY = tmp[0] - 1;
        SX = tmp[1] - 1;
    }
    {
        auto tmp = readln().split().to!(long[])();
        GY = tmp[0] - 1;
        GX = tmp[1] - 1;
    }

    auto Field = new string[](R);
    foreach (ref c; Field)
        c = readln.chomp;
    auto dist = new long[][](R, C);
    foreach (ref row; dist)
        row[] = -1;
    dist[SY][SX] = 0;

    DList!(Tuple!(long, long)) Q;
    Q.insert(Tuple!(long, long)(SY, SX));

    while (true)
    {
        auto p = Q.front;
        Q.removeFront();

        if (p[0] == GY && p[1] == GX)
        {
            writeln(dist[p[0]][p[1]]);
            return;
        }

        if (dist[p[0] + 1][p[1] + 0] == -1 && Field[p[0] + 1][p[1] + 0] == '.' && p[0] + 1 < R)
        {
            Q.insert(Tuple!(long, long)(p[0] + 1, p[1] + 0));
            dist[p[0] + 1][p[1] + 0] = dist[p[0]][p[1]] + 1;
        }
        if (dist[p[0] - 1][p[1] + 0] == -1 && Field[p[0] - 1][p[1] + 0] == '.' && 0 < p[0])
        {
            Q.insert(Tuple!(long, long)(p[0] - 1, p[1] + 0));
            dist[p[0] - 1][p[1] + 0] = dist[p[0]][p[1]] + 1;
        }
        if (dist[p[0] + 0][p[1] + 1] == -1 && Field[p[0] + 0][p[1] + 1] == '.' && p[1] + 1 < C)
        {
            Q.insert(Tuple!(long, long)(p[0] + 0, p[1] + 1));
            dist[p[0] + 0][p[1] + 1] = dist[p[0]][p[1]] + 1;
        }
        if (dist[p[0] + 0][p[1] - 1] == -1 && Field[p[0] + 0][p[1] - 1] == '.' && 0 < p[1])
        {
            dist[p[0] + 0][p[1] - 1] = dist[p[0]][p[1]] + 1;
            Q.insert(Tuple!(long, long)(p[0] + 0, p[1] - 1));
        }
    }
}

Submission Info

Submission Time
Task C - 幅優先探索
User kotet
Language D (DMD64 v2.070.1)
Score 100
Code Size 2016 Byte
Status
Exec Time 1 ms
Memory 380 KB

Judge Result

Set Name Score / Max Score Test Cases
Sample 0 / 0 subtask0_sample01.txt, subtask0_sample02.txt, subtask0_sample03.txt
All 100 / 100 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 1 ms 256 KB
subtask0_sample02.txt 1 ms 256 KB
subtask0_sample03.txt 1 ms 380 KB
subtask1_01.txt 1 ms 380 KB
subtask1_02.txt 1 ms 380 KB
subtask1_03.txt 1 ms 380 KB
subtask1_04.txt 1 ms 380 KB
subtask1_05.txt 1 ms 380 KB
subtask1_06.txt 1 ms 380 KB
subtask1_07.txt 1 ms 256 KB
subtask1_08.txt 1 ms 256 KB
subtask1_09.txt 1 ms 380 KB
subtask1_10.txt 1 ms 256 KB
subtask1_11.txt 1 ms 380 KB
subtask1_12.txt 1 ms 380 KB
subtask1_13.txt 1 ms 380 KB
subtask1_14.txt 1 ms 256 KB
subtask1_15.txt 1 ms 380 KB
subtask1_16.txt 1 ms 380 KB
subtask1_17.txt 1 ms 380 KB
subtask1_18.txt 1 ms 380 KB
subtask1_19.txt 1 ms 380 KB
subtask1_20.txt 1 ms 380 KB
subtask1_21.txt 1 ms 380 KB
subtask1_22.txt 1 ms 380 KB