Submission #10093207


Source Code Expand

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

#define N 50
#define W 50
#define H 50
#define INF 1e8

struct Point {
  int x, y;
  Point(int x, int y) : x(x), y(y) {}
};

template<class T = int>
class Node_ {
public:
  Point p;
  T cost;
  Node_(int x, int y, T cost) : p(x, y), cost(cost) {}
  bool operator < (const Node_& node) const { return cost < node.cost; }
  bool operator > (const Node_& node) const { return cost > node.cost; }
};

using Node = Node_<int>;

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

/**
 * g: 2次元グリッド (vector<vector<int>>)
 * p: 開始位置
 * 
 * g[y][x]
 *  -1    : 行き止まり
 *  その他 : そのマスを踏んだときのコスト
 **/
vector<vector<int>> dijkstra(const vector<string>& g, const Point& p) {
  
  vector<vector<int>> dist(H, vector<int>(W, INF));
  vector<vector<int>> visited(H, vector<int>(W, 0));
  priority_queue<Node, vector<Node>, greater<Node>> pq;

  map<char, int> char_to_cost{
    {'#', -1},
    {'.', 1}
  };

  pq.emplace(p.x, p.y, 0);
  dist[p.y][p.x] = 0;

  while (!pq.empty()) {
    const Node node = pq.top(); pq.pop();
    int x = node.p.x, y = node.p.y;
    int cost = node.cost;

    if (visited[y][x]) continue;
    visited[y][x] = true;

    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || ny < 0 || nx >= W || ny >= H) continue;
      if (visited[ny][nx]) continue;

      int cell_cost = char_to_cost[g[ny][nx]];
      if (cell_cost == -1) continue;

      int next_cost = cost + cell_cost;
      if (next_cost < dist[ny][nx]) {
        dist[ny][nx] = next_cost;
        pq.emplace(nx, ny, next_cost); // 最短経路更新出来れば更新
      }
    }
  }
  return dist;
}

int main(void) {
  int R, C;
  int sx, sy, gx, gy;

  cin >> R >> C;
  cin >> sy >> sx;
  cin >> gy >> gx;
  sx--;sy--;gx--;gy--;

  vector<string> g(R);
  for (int r = 0; r < R; r++) {
    cin >> g[r];
  }

  vector<vector<int>> dist = dijkstra(g, Point(sx, sy));
  cout << dist[gy][gx] << endl;

  return 0;
}

Submission Info

Submission Time
Task A - 幅優先探索
User tkzw_21
Language C++14 (GCC 5.4.1)
Score 100
Code Size 2133 Byte
Status AC
Exec Time 1 ms
Memory 256 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 1 ms 256 KB
subtask0_sample02.txt AC 1 ms 256 KB
subtask0_sample03.txt AC 1 ms 256 KB
subtask1_01.txt AC 1 ms 256 KB
subtask1_02.txt AC 1 ms 256 KB
subtask1_03.txt AC 1 ms 256 KB
subtask1_04.txt AC 1 ms 256 KB
subtask1_05.txt AC 1 ms 256 KB
subtask1_06.txt AC 1 ms 256 KB
subtask1_07.txt AC 1 ms 256 KB
subtask1_08.txt AC 1 ms 256 KB
subtask1_09.txt AC 1 ms 256 KB
subtask1_10.txt AC 1 ms 256 KB
subtask1_11.txt AC 1 ms 256 KB
subtask1_12.txt AC 1 ms 256 KB
subtask1_13.txt AC 1 ms 256 KB
subtask1_14.txt AC 1 ms 256 KB
subtask1_15.txt AC 1 ms 256 KB
subtask1_16.txt AC 1 ms 256 KB
subtask1_17.txt AC 1 ms 256 KB
subtask1_18.txt AC 1 ms 256 KB
subtask1_19.txt AC 1 ms 256 KB
subtask1_20.txt AC 1 ms 256 KB
subtask1_21.txt AC 1 ms 256 KB
subtask1_22.txt AC 1 ms 256 KB