-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp081.cpp
147 lines (109 loc) · 2.68 KB
/
p081.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Path sum: two ways
//
// https://projecteuler.net/problem=81
// https://www.hackerrank.com/contests/projecteuler/challenges/euler081
#include <vector>
#include <queue>
#include <iostream>
#include <fstream>
#include <string>
#include <locale>
#include <cassert>
typedef unsigned long long MatrixValue;
struct Matrix : public std::vector<MatrixValue>
{
size_type n;
MatrixValue v(size_type x, size_type y) const
{
return at(x + y * n);
}
};
struct Visited : public std::vector<bool>
{
size_type n;
bool v(size_type x, size_type y) const
{
return at(x + y * n);
}
void set(size_type x, size_type y)
{
at(x + y * n) = true;
}
};
struct Cell
{
size_t x, y;
MatrixValue v;
Cell(size_t x, size_t y, MatrixValue v) : x(x), y(y), v(v)
{
}
bool operator<(const Cell& r) const
{
return v > r.v;
}
};
struct comma_is_space : std::ctype<char>
{
comma_is_space() : std::ctype<char>(get_table()) {}
static mask const* get_table()
{
static mask rc[table_size];
rc[static_cast<size_t>(',')] = std::ctype_base::space;
rc[static_cast<size_t>('\n')] = std::ctype_base::space;
return &rc[0];
}
};
bool read(Matrix& matrix)
{
matrix.n = 80;
matrix.resize(matrix.n * matrix.n);
std::ifstream f("p081_matrix.txt");
if (f.is_open())
{
size_t i = 0;
// modifie le séparateur de lecture
f.imbue(std::locale(f.getloc(), new comma_is_space));
while (!f.eof() && i < matrix.size())
f >> matrix[i++];
return i == matrix.size();
}
return false;
}
MatrixValue solve(const Matrix& matrix)
{
const size_t n = matrix.n;
Visited visited;
std::priority_queue<Cell> stack;
visited.n = n;
visited.resize(matrix.size(), false);
stack.push(Cell(0, 0, matrix.v(0, 0)));
while (! stack.empty())
{
Cell e = stack.top();
stack.pop();
if (visited.v(e.x, e.y))
continue;
visited.set(e.x, e.y);
if (e.x == n - 1 && e.y == n - 1)
return e.v;
if (e.x < n - 1)
stack.push(Cell(e.x + 1, e.y, e.v + matrix.v(e.x + 1, e.y)));
if (e.y < n - 1)
stack.push(Cell(e.x, e.y + 1, e.v + matrix.v(e.x, e.y + 1)));
}
return 0;
}
int main()
{
Matrix matrix;
if (! read(matrix))
{
// HackerRank
std::cin >> matrix.n;
matrix.resize(matrix.n * matrix.n);
for (size_t i = 0; i < matrix.n * matrix.n; ++i)
std::cin >> matrix[i];
}
std::cout << solve(matrix) << std::endl;
return 0;
}