Surrounding Chess Pieces
Time limit2sMemory limit2048 MB
Count the ways to place white pieces on an 8x8 board so that no two black pieces are connected through empty squares.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Bit manipulation, Matrix
- Solved
- No attempts yet
Problem
Chess is a game of strategy and intellect that has been played for centuries. It offers endless possibilities, and players can stay absorbed in a single game for hours. Suppose you have no opponent. Here is a puzzle instead.
You have an chessboard. You have up to 64 white pieces and up to 64 black pieces. Some black pieces are already on the board. Place white pieces so that no two black pieces are "connected".
Two black pieces are connected if you can move from one to the other through empty squares, one square at a time, moving horizontally or vertically. In other words, no path of empty squares may join two black pieces without passing through a white piece.
Write a program that counts the ways to place white pieces so that all black pieces are separated. All white pieces are indistinguishable.
Input
The input consists of 8 lines, each with 8 characters. A "." marks an empty square, and a "B" marks a square with a black piece.
Output
Print a single integer: the number of ways to place white pieces so that no two black pieces can reach each other.