Practice on Toph

Participate in exhilarating programming contests, solve unique algorithm and data structure challenges and be a part of an awesome community.


By pinanzo · Limits 1s, 512 MB

Who doesn’t love board games? During this COVID lockdown, we all are bored at home and need to entertain ourselves. Watching movies, Netflix is not as exciting as it sounds nowadays. Learning and playing board games is something worthy to pass our leisure time. Such a game is Othello. The motto of the game is, “A minute to learn, a lifetime to master”. It is a 2 player game that is played on a 8 x 8 2D grid like a chessboard. But unlike chess, the grids are not color marked here. There are 64 disks (or discs). Each disk has 2 sides (white and black). Each player chooses their color and plays accordingly.

The rules of the game are as follows:

  1. The object of this 2 player game is to have the majority of your colored disks face up on the board at the end of the game. To set up the board place 4 disks, 2 white sides up and 2 black sides up in the center of the board as shown. 1 player plays as white and the other as black. The player playing black goes first, then turns alternate.

  2. On your turn, you must place 1 disk on any empty space on the board and outflank your opponent. To outflank means to have your disks on either side of a continuous straight line of your opponent’s disks. This line can be any number of disks long and can be horizontal, vertical, or diagonal. When you place a disk so that you have outflanked your opponent’s disks, you then flip the outflanked disks over to your color. You are allowed to outflank multiple lines(horizontal, vertical, or diagonal) in a single turn. So, Black’s first possible move should be any of the possible 4 positions (lightly shaded disk positions in Image 2). Let’s say he places a disk on (2, 3) grid.

  3. If you are unable to outflank your opponent on your turn, then you are not allowed to play and you must skip your turn. If you can play, then you must play. In this example, white has no valid move here because it can’t outflank its opponent. So, it must skip its turn.

  4. Disks may only be outflanked as a direct result of a move and must be in a direct line of the placed disk. In this example, if black plays its move at (3, 7), the only white disks that are going to be flipped are (4,7) and (5, 7). Though after flipping these two white disks, (4, 5) and (4, 6) are getting outflanked but these are not going to be flipped because these are not outflanked as the direct result of black’s move at (3, 7). An outflank may not skip over your own colored disk to outflank more disks. Only the disks in the immediate outflank are captured.

  5. All outflanked disks must be flipped. You may not choose to only flip some of them. Once a disk is placed on a square it may never be removed or moved to another square. When it is no longer possible for either player to move, the game is over. Disks are counted and the player with the majority of their color showing is the winner.

In this problem, you will be given some move. You’ll have to decide which move is whose. Remember black moves first always. Your job is to simulate the board according to the given moves. You’ll have to print the state of the board after the final move.


There will be a number N on the first line of input that denotes the number of moves on the following line. Then N line(s) follow. There will be two numbers R & C in each line separated by a space character, which denotes the row and column index of the move. Remember, every move will be valid for either black or white. You’ll have to decide which move belongs to whom.

0 <= N <= 60

0 <= R, C <= 7


After completing the move, just print the state of the board where a ‘.’ (dot) without quotes represents the empty grids, ‘B’ without quotes represents the black-faced disks and ‘W’ without quotes represents the white-faced disks. See the sample test cases for a better understanding.


2 3



    100% Solution Ratio

    rfpermenEarliest, 3w ago

    edge555Fastest, 0.0s

    rfpermenLightest, 131 kB

    NirjhorShortest, 1695B


    Login to submit

    Toph uses cookies. By continuing you agree to our Cookie Policy.