-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem83.h
More file actions
74 lines (65 loc) · 1.96 KB
/
problem83.h
File metadata and controls
74 lines (65 loc) · 1.96 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// Created by Annie & Pratim on 12/12/22.
//
#ifndef PROJECT_EULER_PROBLEM83_H
#define PROJECT_EULER_PROBLEM83_H
#include "iostream"
#include "vector"
#include "fstream"
#include "sstream"
#include "queue"
#include "unordered_map"
#include "../include/string_lib.h"
using namespace std;
void read_matrix(string filename, int matrix[80][80]) {
ifstream file(filename);
string line;
int i = 0;
while (getline(file, line)) {
vector<string> tokens = split(line, ',');
for (int j = 0; j < tokens.size(); j++) {
matrix[i][j] = stoi(tokens[j]);
}
i++;
}
}
long djikstra(int M, int N, int matrix[80][80]) {
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> queue;
bool seen[80][80] = {false};
int dist[80][80];
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
dist[i][j] = INT_MAX;
}
}
dist[0][0] = matrix[0][0];
queue.push(make_tuple(0, 0, 0));
while (!queue.empty()) {
tuple<int, int, int> top = queue.top();
queue.pop();
int d, i, j;
tie(d, i, j) = top;
if (seen[i][j]) continue;
seen[i][j] = true;
for (int di = -1; di <= 1; di++) {
for (int dj = -1; dj <= 1; dj++) {
if (abs(di) == abs(dj)) continue;
int ni = i + di;
int nj = j + dj;
if (ni < 0 || ni >= M || nj < 0 || nj >= N || seen[ni][nj]) continue;
int nd = min(dist[ni][nj], dist[i][j] + matrix[ni][nj]);
dist[ni][nj] = nd;
queue.push(make_tuple(nd, ni, nj));
}
}
}
return dist[M - 1][N - 1];
}
void problem_83_solution(bool log) {
int M = 80, N = 80;
int matrix[80][80];
read_matrix("misc/p083_matrix.txt", matrix);
long solution = djikstra(M, N, matrix);
if (log) cout << solution << endl;
}
#endif //PROJECT_EULER_PROBLEM83_H