-
Notifications
You must be signed in to change notification settings - Fork 0
/
10798_BewaryofRoses.cc
146 lines (121 loc) · 2.66 KB
/
10798_BewaryofRoses.cc
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
/*
*
* Author : fcbruce <fcbruce8964@gmail.com>
*
* Time : Mon 23 May 2016 14:49:32
*
*/
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define sqr(x) ((x)*(x))
#ifdef _WIN32
#define lld "%I64d"
#else
#define lld "%lld"
#endif
const int INF = 0x3f3f3f3f;
const long long INFLL = 0x3f3f3f3f3f3f3f3fll;
const double PI = acos(-1.0);
const double eps = 1e-10;
typedef long long LL;
typedef int itn;
const int maxn = 0;
const int maxm = 0;
int n;
struct heapNode
{
int x, y, d, rose[4];
heapNode() {}
heapNode(int x,int y, int r0, int r1, int r2, int r3)
{
this->x = x;
this->y = y;
rose[0] = r0;
rose[1] = r1;
rose[2] = r2;
rose[3] = r3;
d = std::max(std::max(r0, r1), std::max(r2, r3));
}
void modify()
{
d = *std::max_element(rose, rose + 4);
}
bool operator < (const heapNode &rhs) const
{
return d > rhs.d;
}
};
bool vis[21][21][11][11][11][11];
char g[22][22];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
inline bool onedge(int x, int y)
{
return 0 == x || x == n - 1 || 0 == y || y == n - 1;
}
int bfs(int sx, int sy)
{
memset(vis, false, sizeof vis);
std::priority_queue<heapNode> q;
vis[sx][sy][0][0][0][0] = true;
q.push(heapNode(sx, sy, 0, 0, 0, 0));
while (!q.empty())
{
heapNode current = q.top(); q.pop();
if (onedge(current.x, current.y)) return current.d;
for (int i = 0; i < 4; i++)
{
heapNode next = current;
next.x += dx[i];
next.y += dy[i];
if (g[next.x][next.y] == 'R') next.rose[0]++;
if (g[n - 1 - next.y][next.x] == 'R') next.rose[1]++;
if (g[next.y][n - 1 - next.x] == 'R') next.rose[2]++;
if (g[n - 1 - next.x][n - 1 - next.y] == 'R') next.rose[3]++;
next.modify();
if (vis[next.x][next.y][next.rose[0]][next.rose[1]][next.rose[2]][next.rose[3]]) continue;
vis[next.x][next.y][next.rose[0]][next.rose[1]][next.rose[2]][next.rose[3]] = true;
q.push(next);
}
}
return 0;
}
inline void find(int &x, int &y)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (g[i][j] == 'P')
{
x = i;
y = j;
return ;
}
}
int main()
{
#ifdef FCBRUCE
freopen("/home/fcbruce/code/t", "r", stdin);
#endif // FCBRUCE
while (scanf("%d", &n) == 1 && n != 0)
{
for (int i = 0; i < n; i++) scanf("%s", g[i]);
int x = 0, y = 0;
find(x, y);
printf("At most %d rose(s) trampled.\n", bfs(x, y));
}
return 0;
}