Problem Statement
The game of Inc is played by 3 players on a rectangular board, made up of an MxN grid of squares. Each square is either empty or contains a digit between 1 and 3, inclusive. The players take turns making moves on the board, in order, starting with player 1. Play continues for a pre-determined number of moves, after which the players' scores are totaled. Each player receives one point for each square containing a digit equal to their player number. The player with the highest score wins.
A move consists of a player dividing the squares on the board into two non-empty groups, with a single straight horizontal or vertical line. The player then selects one of the two groups on either side of the line, and either adds 1 to or subtracts 1 from all the digits in that group. Whenever 1 is added to 3, the result is wrapped around to 1. Similarly, whenever 1 is subtracted from 1, the result is wrapped around to 3.
Each player makes his or her moves according to the following set of priorities. A higher-numbered priority is only used to select among multiple moves that satisfy all lower-numbered priorities.
- Prefer moves that will result in you having more points than the other 2 players at the end of the game.
- Prefer moves that will result in neither of the other 2 players having more points than you at the end of the game.
- Prefer moves that will result in you having as many points as possible at the end of the game.
- Prefer dividing the board with a horizontal line over dividing the board with a vertical line.
- Prefer the group to the left or the top of the dividing line over the group to the right or the bottom.
- Prefer lines to the left or the top over lines to the right or the bottom.
Assume that all players play optimally, and that they all expect each other to play optimally as well.
The initial state of the board is given as a
Return the final state of the board as a
Definition
- Class:
- Inc
- Method:
- finalBoard
- Parameters:
- String[], int, int
- Returns:
- String[]
- Method signature:
- String[] finalBoard(String[] initialBoard, int inc, int turns)
- (be sure your method is public)
Notes
- turns will not necessarily be divisible by 3.
Constraints
- initial will contain between 2 and 5 elements, inclusive.
- Each element of initial will be between 2 and 5 characters long, inclusive.
- Each element of initial will have the same length.
- Each element of initial will be either a '.' (period), '1', '2', or '3'.
- inc will be -1 or 1.
- turns will be between 1 and 10, inclusive.
Examples
{ "333", "3.3", "333" }
1
1
Returns: {"111", "1.1", "333" }
Only player 1 makes a move, and there are 4 ways he can get 5 points. Following the given priorities, he chooses to divide the board with a horizontal line one row above the bottom, and increment the 5 digits in the 6 squares above that line.
{ "....", "3333", "3333", ".33." }
-1
3
Returns: {"....", "2222", "2222", ".22." }
Player 1 cannot score any points and selects only the top row, which contains no digits. This leaves player 2 with the optimal move of selecting all rows but the top. Player 3 cannot score any points either and selects the top row only, leaving player 2 with all 10 available points.
{ "111", "111" }
1
9
Returns: {"313", "232" }
{ "2222", "2222", "2222" }
-1
2
Returns: {"3112", "3112", "3112" }
{ "231.3", "3.233", "12.12", "3.231", "132.3" }
1
10
Returns: {"123.1", "1.333", "12.31", "1.331", "132.2" }
{ "11111", "12221", "12321", "12221", "11111" }
-1
10
Returns: {"33331", "12222", "31211", "31111", "33331" }
{ "11111", "12221", "12321", "12221", "11111" }
1
10
Returns: {"22222", "31113", "12321", "31113", "11111" }
{ "11111", "12221", "12321", "12221", "11111" }
1
9
Returns: {"33212", "23211", "23311", "23211", "33212" }
{ "12121", "23232", "31313", "12121", "32323" }
1
10
Returns: {"11312", "33231", "11312", "22123", "31211" }
{ "21111", "12111", "11211", "11121", "11112" }
-1
9
Returns: {"13123", "12231", "11331", "22322", "22313" }
{ "123", "123" }
1
1
Returns: {"131", "131" }
{ "31122", "31122" }
1
8
Returns: {"11111", "33333" }
{ "31122", "31122" }
1
10
Returns: {"31312", "12123" }
{ "31122", "31122" }
-1
7
Returns: {"33333", "11111" }
{ "12231", ".23.1", "..211", "...1.", "222.3" }
-1
9
Returns: {"11131", ".23.2", "..222", "...3.", "211.3" }
{ ".1", ".2", "33" }
1
6
Returns: {".3", ".3", "12" }
{ "212.2", ".33.1", "213..", "21.2." }
1
6
Returns: {"131.1", ".22.3", "132..", "32.3." }
{ "2.222", "21212", ".3..3", "2.31.", "33..1" }
1
10
Returns: {"1.133", "21231", ".2..1", "1.22.", "33..3" }
{ "2..", ".31" }
-1
7
Returns: {"3..", ".12" }
{ "2...1", "31131", "..223", ".3123", "32.23" }
-1
10
Returns: {"3...2", "23213", "..332", ".1121", "13.21" }
{ "..332", "11.1.", "32.33", "31.2.", "213.3" }
-1
9
Returns: {"..221", "32.2.", "12.33", "22.3.", "313.3" }
{ "...", "331", "13.", "1..", "3.." }
-1
6
Returns: {"...", "331", "32.", "2..", "1.." }
{ ".1211", "22.13", "233.." }
-1
10
Returns: {".3133", "23.21", "211.." }
{ ".3323", "3.1..", "22.3." }
1
9
Returns: {".1323", "3.3..", "22.2." }
{ "121", "1.1", ".11", "12.", "1.2" }
-1
6
Returns: {"122", "3.1", ".23", "31.", "3.2" }
{ "3.222", "13..3", "33111", ".2333", ".2232" }
1
10
Returns: {"2.111", "12..3", "21333", ".1333", ".3121" }
{ "1.22.", "31331", "2122.", ".213." }
-1
9
Returns: {"2.31.", "23231", "3231.", ".322." }
{ "2.321", ".112.", "3.1.1", "31211", "1.113" }
1
2
Returns: {"3.132", ".223.", "1.2.2", "12322", "2.221" }
{ "33332", ".3..1", ".12.3" }
-1
7
Returns: {"33321", ".1..1", ".12.2" }
{ "12.", "312", "121", ".32" }
1
10
Returns: {"31.", "122", "312", ".31" }
{ "212", "123", "331", ".2.", "332" }
1
3
Returns: {"321", "232", "332", ".2.", "111" }
{ "1123", "3311", "1323", "1222", "2112" }
-1
9
Returns: {"2232", "3312", "3213", "2331", "3221" }
{ "...33", "112.2", "12123", "22.3.", "13112" }
-1
9
Returns: {"...22", "131.2", "11323", "13.2.", "23123" }
{ "1222", "1321", "2...", "121.", "2122" }
1
9
Returns: {"2222", "1213", "3...", "113.", "2311" }
{ "33..2", "131.3", "33..2", "1112.", "3..23" }
1
10
Returns: {"33..2", "212.1", "33..2", "1112.", "1..31" }
{ "1.231", "231.3", "1..33", "212.2", "2312." }
1
5
Returns: {"2.223", "112.3", "1..11", "231.3", "2233." }
{ "....1", "1222.", "..112", ".1232" }
1
10
Returns: {"....2", "3133.", "..331", ".1121" }
{ ".2112", "21.12", ".12.2" }
-1
10
Returns: {".2131", "21.31", ".12.1" }
{ "32323", ".31.1", "23.12", "...33", "3.232" }
-1
3
Returns: {"21231", ".23.2", "31.12", "...33", "2.113" }
{ "1223.", ".1..2", "1.1.2" }
1
9
Returns: {"1111.", ".2..2", "3.2.2" }
{ "...2.", ".2.11", ".1.13", ".2213", "..311" }
1
10
Returns: {"...2.", ".1.13", ".2.31", ".3131", "..313" }
{ ".3312", "1..22", "1321.", "13.32" }
1
7
Returns: {".1121", "1..31", "2213.", "33.33" }
{ "2..32", "322.2", ".3...", "..1..", "...21" }
1
6
Returns: {"1..32", "131.1", ".2...", "..2..", "...32" }
{ "31132", "33.31", "32322", "..22.", "223.1" }
-1
10
Returns: {"31122", "33.21", "21231", "..13.", "112.3" }
{ "122", "3..", "113", "2.." }
-1
10
Returns: {"111", "3..", "132", "2.." }
{ "33.", ".13", "..2", "1.." }
1
10
Returns: {"33.", ".11", "..3", "1.." }
{ "22", "12", "21", ".3" }
-1
1
Returns: {"11", "12", "21", ".3" }
{ "22", "13", "2.", "2.", "3." }
-1
10
Returns: {"12", "11", "3.", "3.", "2." }
{ "3222", "..1.", ".32.", "..22" }
1
10
Returns: {"2122", "..3.", ".33.", "..11" }
{ "312..", ".232.", "..2..", "3.332", "333.." }
1
10
Returns: {"113..", ".132.", "..3..", "1.113", "323.." }
{ "22111", "223..", ".2211", "31...", "13323" }
-1
8
Returns: {"33233", "112..", ".1111", "12...", "32223" }