-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday15.cs
More file actions
48 lines (41 loc) · 1.22 KB
/
Copy pathday15.cs
File metadata and controls
48 lines (41 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Collections.Generic;
using PII = System.ValueTuple<int, int>;
class Day15 {
static PII[] deltas = {(-1, 0), (1, 0), (0, -1), (0, 1)};
static string[] lines;
static int N, M;
static int cost(int x, int y) {
return 1 + (x/N + y/M + lines[x%N][y%M] - '1') % 9;
}
static int bfs(int n, int m) {
int[,] dis = new int[n, m];
bool[,] done = new bool[n, m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
dis[i, j] = 1000000000;
PriorityQueue<PII, int> q = new PriorityQueue<PII, int>();
q.Enqueue((0, 0), 0);
dis[0, 0] = 0;
while (q.Count > 0) {
var (x, y) = q.Dequeue();
if (done[x, y]) continue;
done[x, y] = true;
foreach (var (dx, dy) in deltas) {
int xx = x + dx, yy = y + dy;
if (0 <= xx && xx < n && 0 <= yy && yy < m) {
int d = dis[x, y] + cost(xx, yy);
if (dis[xx, yy] > d) q.Enqueue((xx, yy), dis[xx, yy] = d);
}
}
}
return dis[n - 1, m - 1];
}
static void Main(string[] args) {
lines = System.IO.File.ReadAllLines(args[0]);
N = lines.GetLength(0);
M = lines[0].Length;
Console.WriteLine(bfs(N, M));
Console.WriteLine(bfs(5*N, 5*M));
}
}