/
C.cpp
executable file
·110 lines (92 loc) · 1.49 KB
/
C.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
// Problem: Doubly-sorted Grid
// Language: C++
// Author: KirarinSnow
// Usage: g++ thisfile.cpp -o executable && ./executable <input.in >output.out
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
const int MOD = 10007;
int d[1<<20][26][11];
bool b[1<<20][26][11];
char g[10][10];
int row, col;
int dp(int p, int c, int k)
{
if (b[p][c][k] == false)
{
int t = 0;
if (p == (((1<<row)-1)<<col))
{
t = 1;
}
else
{
if (k < 0)
{
if (c != 0)
{
t += dp(p, c-1, col-1);
}
}
else
{
t += dp(p, c, k-1);
int pp = p;
int x = col;
int y = 0;
int z = 0;
while (x > k)
{
if (pp%2 == 0)
{
x--;
}
else
{
z++;
}
y++;
pp /= 2;
}
if ((y >= 2) && (((p%(1<<y))>>(y-2)) == 1))
{
char ch = g[z-1][k];
if (ch == '.' || ch == (char)((int)'a'+c))
{
int flip = (pp << y) + (2 << (y-2)) + (p%(1<<(y-2)));
t += dp(flip, c, k);
}
}
}
}
d[p][c][k] = t%MOD;
b[p][c][k] = true;
}
return d[p][c][k];
}
int solve()
{
memset(d, 0, sizeof d);
memset(b, false, sizeof b);
memset(g, 0, sizeof g);
cin >> row >> col;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cin >> g[i][j];
}
}
return dp((1<<row)-1, 25, col-1);
}
int main()
{
int cases;
cin >> cases;
for (int i = 1; i <= cases; i++)
{
printf("Case #%d: %d\n", i, solve());
}
return 0;
}