-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuva-10967.cpp
103 lines (87 loc) · 2.27 KB
/
uva-10967.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
//uva 10967
//The Great Escape
#include <iostream>
#include <climits>
#include <vector>
#include <string>
#include <map>
#include <set>
using namespace std;
int get_dir(char dir)
{
if(dir == 'N')
return 0;
else if(dir == 'W')
return 1;
else if(dir == 'S')
return 2;
else
return 3;
}
int find_time(char dir, char next_dir)
{
int curr_dir = get_dir(dir);
return min(abs(curr_dir - next_dir), 4 - abs(curr_dir - next_dir));
}
int Dijkstra(vector <string> & maze, map <pair <int, int>, int> & doors)
{
int m = maze.size();
int n = maze[0].size();
int x_next[] = {-1, 0, 1, 0};//north, west, south, east
int y_next[] = {0, -1, 0, 1};
vector < vector <int> > min_time(m, vector <int> (n, INT_MAX));
set < pair <int, pair <int, int> > > Heap;
min_time[m - 1][0] = 0;
Heap.insert(make_pair(0, make_pair(m - 1, 0)));
while(!Heap.empty()){
int front_time = Heap.begin()->first;
pair <int, int> front = Heap.begin()->second;
Heap.erase(Heap.begin());
if(front == make_pair(0, n - 1))
return min_time[0][n - 1];
for(int i = 0; i < 4; ++i){
int x_n = front.first + x_next[i];
int y_n = front.second + y_next[i];
if(x_n >= 0 && x_n < m && y_n >= 0 && y_n < n && maze[x_n][y_n] != '#'){
if(maze[x_n][y_n] != '.' && get_dir(maze[x_n][y_n]) != ((i + 2) % 4))
continue;
int new_time = front_time + 1;
if(maze[front.first][front.second] != '.')
new_time += find_time(maze[front.first][front.second], i) * doors[make_pair(front.first, front.second)];
if(new_time < min_time[x_n][y_n]){
Heap.erase(make_pair(min_time[x_n][y_n], make_pair(x_n, y_n)));
min_time[x_n][y_n] = new_time;
Heap.insert(make_pair(min_time[x_n][y_n], make_pair(x_n, y_n)));
}
}
}
}
return -1;
}
int main(void)
{
int T = 0;
cin >> T;
while(T--){
int m, n;
cin >> m >> n;
vector <string> maze(m);
map <pair <int, int>, int> doors;
for(int i = 0; i < m; ++i){
string tmp;
cin >> tmp;
maze[i] = tmp;
for(int j = 0; j < n; ++j)
if(maze[i][j] == 'N' || maze[i][j] == 'W' || maze[i][j] == 'S' || maze[i][j] == 'E')
doors[make_pair(i, j)] = 0;
}
for(auto & a : doors)
cin >> a.second;
int result = Dijkstra(maze, doors);
if(result == -1)
cout << "Poor Kianoosh" << endl;
else
cout << result << endl;
}
return 0;
}