#include <stdio.h>
#define NM 13
#define P 1594323
int min(int a, int b) { return a < b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int di[] = { -1, 1, 0, 0 };
int dj[] = { 0, 0, -1, 1 };
int pp3[NM + 1];
void init() {
int i;
pp3[0] = 1;
for (i = 1; i <= NM; i++)
pp3[i] = pp3[i - 1] * 3;
}
char digit(int b, int h) {
return b / pp3[h] % 3;
}
double dp[P]; char visited[P]; int n, m, k1, k2;
void sort(double *aa, int l, int r) {
while (l < r) {
int i = l, j = l, k = r;
double a = aa[l + rand_() % (r - l)], tmp;
while (j < k)
if (aa[j] == a)
j++;
else if (aa[j] < a) {
tmp = aa[i], aa[i] = aa[j], aa[j] = tmp;
i++, j++;
} else {
k--;
tmp = aa[j], aa[j] = aa[k], aa[k] = tmp;
}
sort(aa, l, i);
l = k;
}
}
double solve(int b, int turn) {
static double aa[NM * 4];
int k, g, h, h_, i, i_, j, j_, d;
if (!visited[b]) {
visited[b] = 1;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++) {
h = i * m + j;
if (digit(b, h) == turn)
for (g = 0; g < 4; g++) {
i_ = i + di[g], j_ = j + dj[g], h_ = i_ * m + j_;
if (i_ >= 0 && i_ < n && j_ >= 0 && j_ < m && (d = digit(b, h_)) != 0)
solve(b - turn * pp3[h] + (turn - d) * pp3[h_], turn ^ 3);
}
}
k = 0;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++) {
h = i * m + j;
if (digit(b, h) == turn)
for (g = 0; g < 4; g++) {
i_ = i + di[g], j_ = j + dj[g], h_ = i_ * m + j_;
if (i_ >= 0 && i_ < n && j_ >= 0 && j_ < m && (d = digit(b, h_)) != 0)
aa[k++] = solve(b - turn * pp3[h] + (turn - d) * pp3[h_], turn ^ 3);
}
}
sort(aa, 0, k);
k = min(k, turn == 1 ? k1 : k2);
dp[b] = 0;
if (k != 0) {
for (g = 0; g < k; g++)
dp[b] += 1 - aa[g];
dp[b] /= k;
}
}
return dp[b];
}
int main() {
static char cc[NM];
int i, j, h, b;
init();
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
scanf("%s", cc + i * m);
scanf("%d%d", &k1, &k2);
b = 0;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++) {
h = i * m + j;
if (cc[h] == 'J')
b += pp3[h];
else if (cc[h] == 'D')
b += pp3[h] * 2;
}
printf("%.3f\n", solve(b, 1));
return 0;
}
Compilation message
Main.c: In function 'main':
Main.c:97:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:99:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | scanf("%s", cc + i * m);
| ^~~~~~~~~~~~~~~~~~~~~~~
Main.c:100:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | scanf("%d%d", &k1, &k2);
| ^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
292 KB |
Output is correct |
3 |
Correct |
1 ms |
448 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
27 ms |
3000 KB |
Output is correct |
6 |
Correct |
9 ms |
3156 KB |
Output is correct |
7 |
Correct |
18 ms |
3424 KB |
Output is correct |
8 |
Correct |
18 ms |
3108 KB |
Output is correct |
9 |
Correct |
26 ms |
3668 KB |
Output is correct |
10 |
Correct |
20 ms |
3540 KB |
Output is correct |
11 |
Correct |
29 ms |
3856 KB |
Output is correct |
12 |
Correct |
23 ms |
3676 KB |
Output is correct |
13 |
Correct |
20 ms |
3848 KB |
Output is correct |
14 |
Correct |
19 ms |
3536 KB |
Output is correct |
15 |
Correct |
23 ms |
3540 KB |
Output is correct |
16 |
Correct |
7 ms |
3156 KB |
Output is correct |
17 |
Correct |
11 ms |
3880 KB |
Output is correct |
18 |
Correct |
8 ms |
2516 KB |
Output is correct |
19 |
Correct |
7 ms |
4180 KB |
Output is correct |
20 |
Correct |
9 ms |
2976 KB |
Output is correct |
21 |
Correct |
10 ms |
4056 KB |
Output is correct |
22 |
Correct |
11 ms |
3412 KB |
Output is correct |
23 |
Correct |
14 ms |
2704 KB |
Output is correct |
24 |
Correct |
19 ms |
3080 KB |
Output is correct |
25 |
Correct |
26 ms |
3292 KB |
Output is correct |
26 |
Correct |
18 ms |
2840 KB |
Output is correct |
27 |
Correct |
27 ms |
3108 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
8 ms |
3284 KB |
Output is correct |
30 |
Correct |
6 ms |
2772 KB |
Output is correct |
31 |
Correct |
6 ms |
2208 KB |
Output is correct |
32 |
Correct |
6 ms |
2120 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
420 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
416 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
420 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
15 ms |
2672 KB |
Output is correct |
44 |
Correct |
17 ms |
2840 KB |
Output is correct |
45 |
Correct |
10 ms |
1992 KB |
Output is correct |
46 |
Correct |
14 ms |
2604 KB |
Output is correct |
47 |
Correct |
12 ms |
2132 KB |
Output is correct |
48 |
Correct |
11 ms |
2416 KB |
Output is correct |
49 |
Correct |
12 ms |
2516 KB |
Output is correct |
50 |
Correct |
0 ms |
292 KB |
Output is correct |