#P1764. 翻转游戏 (加强版)

翻转游戏 (加强版)

Description

kkke is playing a flip game on an n×nn \times n board. Each cell of the board contains a piece, and each piece has 22 faces: one black and one white. Initially, some pieces face up black and others face up white. Now kkke wants to make all pieces face the same color (either all black up or all white up) with the minimum number of flips. Each time a flip is performed, kkke may choose any piece to flip; at the same time, the 44 pieces that are respectively adjacent to it up, down, left, and right must also be flipped.

Input Format

The first line contains an integer nn, indicating the board size is n×nn \times n.

Then follow nn lines, each containing nn letters, representing the initial state of the board. If a letter is w\tt w, the corresponding piece is currently white side up; if it is b\tt b, the piece is currently black side up.

Output Format

Output one line. If it is impossible to reach the target state, output Impossible; otherwise, output a single integer, the minimum number of flips kkke needs.

4
bwwb
bbwb
bwwb
bwww

4

Hint

  • Constraints
    • For 30%30\% of the testdata, 1n41 \le n \le 4.
    • For 100%100\% of the testdata, 1n161 \le n \le 16.

Translated by ChatGPT 5