#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 + 1];
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 |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
29 ms |
2968 KB |
Output is correct |
6 |
Correct |
9 ms |
3216 KB |
Output is correct |
7 |
Correct |
18 ms |
3408 KB |
Output is correct |
8 |
Correct |
18 ms |
3140 KB |
Output is correct |
9 |
Correct |
27 ms |
3740 KB |
Output is correct |
10 |
Correct |
23 ms |
3600 KB |
Output is correct |
11 |
Correct |
26 ms |
3916 KB |
Output is correct |
12 |
Correct |
22 ms |
3676 KB |
Output is correct |
13 |
Correct |
21 ms |
3948 KB |
Output is correct |
14 |
Correct |
18 ms |
3432 KB |
Output is correct |
15 |
Correct |
24 ms |
3544 KB |
Output is correct |
16 |
Correct |
7 ms |
3156 KB |
Output is correct |
17 |
Correct |
12 ms |
3816 KB |
Output is correct |
18 |
Correct |
9 ms |
2580 KB |
Output is correct |
19 |
Correct |
6 ms |
4180 KB |
Output is correct |
20 |
Correct |
9 ms |
3000 KB |
Output is correct |
21 |
Correct |
9 ms |
4052 KB |
Output is correct |
22 |
Correct |
12 ms |
3412 KB |
Output is correct |
23 |
Correct |
12 ms |
2688 KB |
Output is correct |
24 |
Correct |
17 ms |
2976 KB |
Output is correct |
25 |
Correct |
26 ms |
3284 KB |
Output is correct |
26 |
Correct |
17 ms |
2912 KB |
Output is correct |
27 |
Correct |
25 ms |
3144 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
7 ms |
3284 KB |
Output is correct |
30 |
Correct |
6 ms |
2772 KB |
Output is correct |
31 |
Correct |
7 ms |
2200 KB |
Output is correct |
32 |
Correct |
8 ms |
2108 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
2 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
340 KB |
Output is correct |
42 |
Correct |
2 ms |
340 KB |
Output is correct |
43 |
Correct |
14 ms |
2648 KB |
Output is correct |
44 |
Correct |
16 ms |
2772 KB |
Output is correct |
45 |
Correct |
10 ms |
1952 KB |
Output is correct |
46 |
Correct |
13 ms |
2644 KB |
Output is correct |
47 |
Correct |
12 ms |
2188 KB |
Output is correct |
48 |
Correct |
10 ms |
2444 KB |
Output is correct |
49 |
Correct |
12 ms |
2492 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |