题目收集
题目列表题单
返回列表

CS:GO

CodeforcesP0051· 4000ms· 1024 MB· ZJU Summer 2026 Contest 5原题链接
轮廓线 dp状压 dp

创建于 2026/07/18 · 更新于 2026/07/18

题目描述

Rifleman is a student who majors in Computer Science , and he is playing GO now.

Consider an n×mn\times mn×m chessboard where each cell can be in one of three states: a black piece, a white piece, or empty. Consider a maximal four-connected component consisting only of pieces of the same color. If there is no empty cell among the cells directly adjacent to this connected component, the configuration is considered invalid(This rule is quite similar to Go).

We call a component SSS maximal if there is no component TTT such that SSS is a proper subset of TTT.

Now, given an incomplete chessboard, you need to fill each ? with one of the three states so that the resulting configuration is valid. Count the number of ways to do this. Since the number may be large, output it modulo 998244353998244353998244353.

输入格式

The first line contains two integers n(1≤n≤20)n(1\leq n\leq 20)n(1≤n≤20) and m(1≤m≤7)m(1\leq m\leq 7)m(1≤m≤7), the dimensions of the chessboard.

The next nnn lines each contain mmm characters, describing the chessboard. The characters mean:

B: a black piece

W: a white piece

.: an empty cell

?: a cell you need to fill

输出格式

Print a integer — the answer modulo 998244353998244353998244353.

样例输入 #1

2 2
B?
?W

样例输出 #1

5

样例输入 #2

3 3
?W.
W?W
???

样例输出 #2

108