結果

問題 No.34 砂漠の行商人
ユーザー te-shte-sh
提出日時 2020-01-25 13:35:14
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 807 ms / 5,000 ms
コード長 5,381 bytes
コンパイル時間 1,359 ms
コンパイル使用メモリ 150,764 KB
実行使用メモリ 25,968 KB
最終ジャッジ日時 2023-09-04 04:59:15
合計ジャッジ時間 5,630 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 26 ms
4,388 KB
testcase_05 AC 40 ms
6,608 KB
testcase_06 AC 6 ms
4,380 KB
testcase_07 AC 82 ms
7,948 KB
testcase_08 AC 106 ms
7,960 KB
testcase_09 AC 270 ms
7,892 KB
testcase_10 AC 39 ms
4,884 KB
testcase_11 AC 63 ms
4,380 KB
testcase_12 AC 10 ms
4,380 KB
testcase_13 AC 807 ms
25,968 KB
testcase_14 AC 621 ms
25,960 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 39 ms
4,504 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 5 ms
4,380 KB
testcase_19 AC 202 ms
9,740 KB
testcase_20 AC 317 ms
11,308 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 6 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 394 ms
15,468 KB
testcase_25 AC 29 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// URL: https://yukicoder.me/problems/no/34

import std.algorithm, std.array, std.container, std.math, std.range, std.typecons, std.string;

version(unittest) {} else
void main()
{
  int N, V, Sx, Sy, Gx, Gy; io.getV(N, V, Sx, Sy, Gx, Gy); --Sx; --Sy; --Gx; --Gy;
  int[][] L; io.getM(N, N, L);

  auto m = grid(L);
  alias Point = m.Point;
  auto S = Point(Sx, Sy), G = Point(Gx, Gy);

  auto v = Grid!int(N, N), inf = 10^^6;
  foreach (x; 0..N) foreach (y; 0..N) v[x, y] = inf;
  v[S] = 0;

  struct E { Point p; int d, h; }
  auto q = DList!E(E(S, 0, 0));
  while (!q.empty) {
    auto e = q.front; q.removeFront();
    foreach (np; m.around4(e.p)) {
      auto nh = e.h+m[np];
      if (nh >= V) continue;
      if (np == G) {
        io.put(e.d+1);
        return;
      }
      if (nh < v[np]) {
        v[np] = nh;
        q.insertBack(E(np, e.d+1, nh));
      }
    }
  }
  io.put(-1);
}

struct Point2(T)
{
  alias P = Point2!T, Op = string;
  T x, y;
  pure P opBinary(Op o)(P r) if (o=="+"||o=="-")
  { return mixin("P(x"~o~"r.x, y"~o~"r.y)"); }
  P opOpAssign(Op o)(P r) if (o=="+"||o=="-")
  { mixin("x"~o~"=r.x; y"~o~"=r.y;"); return this; }
  pure P opBinary(Op o)(T a) if (o=="*"||o=="/")
  { return mixin("P(x"~o~"a, y"~o~"a)"); }
  P opOpAssign(Op o)(T a) if (o=="*"||o=="/")
  { mixin("x"~o~"=a; y"~o~"=a;"); return this; }
  pure T opBinary(Op o: "*")(P r) { return x*r.x+y*r.y; }
  pure T hypot2() { return x^^2+y^^2; }
}

pure T cross(T)(Point2!T p1, Point2!T p2)
{ return p1.x*p2.y - p1.y*p2.x; }

struct Point3(T)
{
  alias P = Point3!T, Op = string;
  T x, y, z;
  pure P opBinary(Op o)(P r) if (o=="+"||o=="-")
  { return mixin("P(x"~o~"r.x, y"~o~"r.y, z"~o~"r.z)"); }
  P opOpAssign(Op o)(P r) if (o=="+"||o=="-")
  { mixin("x"~o~"=r.x; y"~o~"=r.y; z"~o~"=r.z;"); return this; }
  pure P opBinary(Op o)(T a) if (o=="*"||o=="/")
  { return mixin("P(x"~o~"a, y"~o~"a, z"~o~"a)"); }
  P opOpAssign(Op o)(T a) if (o=="*"||o=="/")
  { mixin("x"~o~"=a; y"~o~"=a; z"~o~"=a;"); return this; }
  pure T opBinary(Op o: "*")(P r) { return x*r.x+y*r.y+z*r.z; }
  pure T hypot2() { return x^^2+y^^2+z^^2; }
}

pure Point3!T cross(T)(Point3!T p1, Point3!T p2)
{ return Point3!T(p1.y*p2.z - p1.z*p2.y, p1.z*p2.x - p1.x*p2.z, p1.x*p2.y - p1.y*p2.x); }

struct Grid(T)
{
  alias G = Grid!T, P = Point2!int, Point = P;
  size_t c, r;
  T[][] data;

  this(size_t c, size_t r) { this.c = c; this.r = r; data = new T[][](r, c); }
  this(T[][] data) { c = data[0].length; r = data.length; this.data = data; }

  pure P i2p(size_t i) { return P(cast(int)(i%c), cast(int)(i/c)); }
  pure size_t p2i(P p) { return p.x*c + p.y; }

  pure T opIndex(size_t x, size_t y) { return data[y][x]; }
  pure T opIndex(P p) { return data[p.y][p.x]; }
  G opIndexAssign(T v, size_t x, size_t y) { data[y][x] = v; return this; }
  G opIndexAssign(T v, P p) { data[p.y][p.x] = v; return this; }
  G opIndexOpAssign(string op)(T v, size_t x, size_t y)
  { mixin("data[y][x]"~op~"=v;"); return this; }
  G opIndexOpAssign(string op)(T v, P p)
  { mixin("data[p.y][p.x]"~op~"=v;"); return this; }
  G opIndexUnary(string op)(size_t x, size_t y) if (op=="++"||op=="--")
  { mixin(op~"data[y][x];"); return this; }
  G opIndexUnary(string op)(P p) if (op=="++"||op=="--")
  { mixin(op~"data[p.y][p.x];"); return this; }

  pure bool valid(size_t x, size_t y) { return 0 <= x && x < c && 0 <= y && y < r; }
  pure bool valid(P p) { return valid(p.x, p.y); }
  auto d4 = [P(-1, 0), P(0, -1), P(1, 0), P(0, 1)];
  pure auto around4(P p) { return d4.map!(d => d+p).filter!(np => valid(np)); }
  auto d8 = [P(-1, 0), P(-1, -1), P(0, -1), P(1, -1), P(1, 0), P(1, 1), P(0, 1), P(-1, 1)];
  pure auto around8(P p) { return d8.map!(d => d+p).filter!(np => valid(np)); }
}
Grid!T grid(T)(T[][] data) { return Grid!T(data); }

auto io = IO!()();
import std.stdio;
struct IO(string floatFormat = "%.10f", string delimiter = " ", alias IN = stdin, alias OUT = stdout)
{
  import std.conv, std.format, std.meta, std.traits;
  alias assignable = hasAssignableElements;

  auto getV(T...)(ref T v) { foreach (ref w; v) get(w); }
  auto getA(T)(size_t n, ref T v) if (assignable!T) { v = new T(n); foreach (ref w; v) get(w); }
  auto getC(T...)(size_t n, ref T v) if (allSatisfy!(assignable, T))
  {
    foreach (ref w; v) w = new typeof(w)(n);
    foreach (i; 0..n) foreach (ref w; v) get(w[i]);
  }
  auto getM(T)(size_t r, size_t c, ref T v) if (assignable!T && assignable!(ElementType!T))
  {
    v = new T(r); foreach (ref w; v) getA(c, w);
  }

  auto put(bool flush = false, T...)(T v)
  {
    foreach (i, w; v) { putA(w); if (i < v.length-1) OUT.write(delimiter); }
    OUT.writeln;
    static if (flush) OUT.flush();
  }
  auto putB(S, T)(bool c, S t, T f) { if (c) put(t); else put(f); }
  auto putRaw(T...)(T v) { OUT.write(v); OUT.writeln; }

  private
  {
    dchar[] buf;
    auto sp = (new dchar[](0)).splitter;
    void nextLine() { IN.readln(buf); sp = buf.splitter; }
    auto get(T)(ref T v) { if (sp.empty) nextLine(); v = sp.front.to!T; sp.popFront(); }

    auto putR(T)(T v)
    {
      auto w = v;
      while (!w.empty) { putA(w.front); w.popFront(); if (!w.empty) OUT.write(delimiter); }
    }
    auto putA(T)(T v)
    {
      static if (isInputRange!T && !isSomeString!T) putR(v);
      else if (isFloatingPoint!T) OUT.write(format(floatFormat, v));
      else OUT.write(v);
    }
  }
}
0