atcoder#AGC059E. [AGC059E] Grid 3-coloring
[AGC059E] Grid 3-coloring
Score : points
Problem Statement
You have an grid board. You want to color all cells in colors so that no two adjacent (edge-sharing) cells have the same color.
You have already painted the border of the board. Determine if you can color the rest of the board properly.
More precisely, you are given a string of length consisting of characters 1
, 2
, and 3
. The string denotes the colors of the cells on the border, starting from the cell , in clockwise order.
Strictly speaking, the -th character of denotes the color of the cell:
- for
- for
- for
- for .
Here, denotes the cell in the -th row and the -th column.
It is guaranteed that no two adjacent cells on the border have the same color.
Solve test cases for each input file.
Constraints
- is a string of length consisting of characters
1
,2
, and3
. - for , and .
- The sum of in one input file doesn't exceed .
- All numbers in the input are integers.
Input
Input is given from Standard Input in the following format:
Each case is in the following format:
Output
For each test case, output YES
if there is a way to color the rest of the board in colors properly, and NO
otherwise. You can output each letter in any case (upper or lower).
4
3
12312312
4
121212121212
7
321312312312121212121321
7
321312312312121312121321
NO
YES
NO
YES
It can be shown that there is no way to properly color the rest of the board in the first and the third test cases. The proper colorings for the second and fourth test cases are shown below.