-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoombs.cpp
124 lines (106 loc) · 3.44 KB
/
Boombs.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
/* https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1594
* #dijkstra #shortest-path
*/
#include<iostream>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<utility>
#include<climits>
using namespace std;
struct comp {
template<typename T>
bool operator()(const T& l, const T& r) const {
if (l.first == r.first) {
return l.second < r.second;
} else {
return l.first < r.first;
}
}
};
typedef pair<int, int> Point;
typedef vector<set<Point, comp> > BoombsGraph;
typedef map<Point, int, comp> Distance;
typedef pair<Point, int> Node;
struct compNode {
bool operator()(const Node& l, const Node& r) const {
return l.second > r.second;
}
};
bool isValidPoint(BoombsGraph& boombsGraph, Point& point, int max_rows, int max_cols) {
if (point.first < 0 || point.first >= max_rows) return false;
if (point.second < 0 || point.second >= max_cols) return false;
if (boombsGraph[point.first].find(point) != boombsGraph[point.first].end()) {
return false;
}
return true;
}
int Dijkstra(BoombsGraph& boombsGraph, Point source, Point destination, int max_rows, int max_cols) {
int ret = INT_MAX;
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
vector<Distance> distArr(max_rows);
distArr[source.first].insert(Node(source, 0));
priority_queue<Node, vector<Node>, compNode > pq;
pq.push(Node(source, 0));
while(!pq.empty()) {
Node node = pq.top();
pq.pop();
Point point = node.first;
int dist = node.second;
if (distArr[point.first].find(point) != distArr[point.first].end() && dist > distArr[point.first][point]) {
continue;
}
for (int i=0; i < 4; ++i) {
Point p(point.first + dx[i], point.second + dy[i]);
if (isValidPoint(boombsGraph, p, max_rows, max_cols)) {
int newdist = dist + 1;
if(distArr[p.first].find(p) != distArr[p.first].end()) {
if (newdist < distArr[p.first][p]) {
distArr[p.first][p] = newdist;
pq.push(Node(p, newdist));
}
} else {
distArr[p.first].insert(pair<Point, int>(p, newdist));
pq.push(Node(p, newdist));
}
}
}
}
if (distArr[destination.first].find(destination) != distArr[destination.first].end()) {
ret = distArr[destination.first][destination];
} else {
BoombsGraph bg(max_rows);
ret = Dijkstra(bg, source, destination, max_rows, max_cols);
}
return ret;
}
int main() {
int r, c;
while(true) {
cin >> r >> c;
if (r==0 && c==0) {
break;
}
int numberBoombsRow;
cin >> numberBoombsRow;
// graph of points at which bombs is set.
BoombsGraph boombsGraph(r);
for (int i=0; i < numberBoombsRow; ++i) {
int row, cols, col;
cin >> row >> cols;
for (int c=0; c < cols; c++) {
cin >> col;
boombsGraph[row].insert(Point(row, col));
}
}
int sx, sy, dx, dy;
cin >> sx >> sy;
cin >> dx >> dy;
int ret = Dijkstra(boombsGraph, Point(sx, sy), Point(dx, dy), r, c);
cout << ret << endl;
}
return 0;
}