Submission #961013


Source Code Expand

#include <string>
#include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>

int M = 1000000007;

bool check (int x, int y, int W, int H) {
  return 0 <= x && x < W && 0 <= y && y < H;
}

int count (std::vector<int> const & a, std::vector<int> & memo, int x, int y, int W, int H) {
  if (memo[x + W * y] >= 0) {
    return memo[x + W * y];
  }
  int total = 1;
  if (check(x - 1, y, W, H) && (a[x + W * y] > a[x - 1 + W * y])) {
    total = (total + count(a, memo, x - 1, y , W, H)) % M;
  }
  if (check(x + 1, y, W, H) && (a[x + W * y] > a[x + 1 + W * y])) {
    total = (total + count(a, memo, x + 1, y , W, H)) % M;
  }
  if (check(x, y - 1, W, H) && (a[x + W * y] > a[x + W * (y - 1)])) {
    total = (total + count(a, memo, x, y - 1, W, H)) % M;
  }
  if (check(x, y + 1, W, H) && (a[x + W * y] > a[x + W * (y + 1)])) {
    total = (total + count(a, memo, x, y + 1, W, H)) % M;
  }
  memo[x + W * y] = total;
  return total;
}

int main (int argc, char ** argv) {
  std::string line;
  std::getline(std::cin, line);

  char * ptr1 = &line[0];
  char * ptr2;
  int H = std::strtol(ptr1, &ptr2, 10);
  ptr1 = ptr2;
  int W = std::strtol(ptr1, &ptr2, 10);

  std::vector<int> a(W * H, 0);
  std::vector<std::tuple<int, int, int>> b(W * H);
  std::vector<int> memo(W * H, -1);

  for (int y = 0; y < H; ++y) {
    std::getline(std::cin, line);
    char * ptr1 = &line[0];
    char * ptr2;
    for (int x = 0; x < W; ++x) {
      int z = std::strtol(ptr1, &ptr2, 10);
      a[x + W * y] = z;
      b[x + W * y] = std::tuple<int, int, int>(x, y, z);
      ptr1 = ptr2;
    }
  }

  std::sort(b.begin(), b.end(), [] (auto l, auto r) { return std::get<2>(l) < std::get<2>(r); });

  int total = 0;
  for (auto const & v : b) {
    total = (total + count(a, memo, std::get<0>(v), std::get<1>(v), W, H)) % M;
  }
  std::cout << total << std::endl;
}

Submission Info

Submission Time
Task D - 経路
User toufu12345
Language C++14 (GCC 5.4.1)
Score 100
Code Size 1928 Byte
Status AC
Exec Time 502 ms
Memory 19840 KiB

Judge Result

Set Name sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 2
AC × 22
Set Name Test Cases
sample sample01.txt, sample02.txt
All 00.txt, 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, sample01.txt, sample02.txt
Case Name Status Exec Time Memory
00.txt AC 256 ms 19840 KiB
01.txt AC 310 ms 19712 KiB
02.txt AC 155 ms 19712 KiB
03.txt AC 3 ms 256 KiB
04.txt AC 3 ms 256 KiB
05.txt AC 3 ms 256 KiB
06.txt AC 3 ms 256 KiB
07.txt AC 3 ms 256 KiB
08.txt AC 3 ms 256 KiB
09.txt AC 3 ms 256 KiB
10.txt AC 4 ms 256 KiB
11.txt AC 501 ms 19840 KiB
12.txt AC 490 ms 19840 KiB
13.txt AC 490 ms 19840 KiB
14.txt AC 489 ms 19840 KiB
15.txt AC 385 ms 19840 KiB
16.txt AC 502 ms 19840 KiB
17.txt AC 496 ms 19840 KiB
18.txt AC 161 ms 19840 KiB
19.txt AC 140 ms 19840 KiB
sample01.txt AC 3 ms 256 KiB
sample02.txt AC 3 ms 256 KiB