Problem Statement
You are given a
In a single move, a king can move to any of its 8 neighboring cells. If you move a king into a cell occupied by a pawn, the king will capture that pawn. You can never move a king outside the board or into a cell already occupied by another king.
Return the minimal number of moves required for the kings to capture all the pawns.
Definition
- Class:
- PawnsAndKings
- Method:
- minNumberOfMoves
- Parameters:
- String[]
- Returns:
- int
- Method signature:
- int minNumberOfMoves(String[] board)
- (be sure your method is public)
Constraints
- board will contain exactly 8 elements.
- Each element of board will contain exactly 8 characters.
- Each character of each element of board will be '.', or an uppercase 'P', or an uppercase 'K'.
- The number of 'P' characters will be between 1 and 10, inclusive.
- The number of 'K' characters will be between 1 and 10, inclusive.
Examples
{".PPPPKP.", "........", "........", "........", "........", "........", "........", "........"}
Returns: 6
The only king will make the minimal number of moves by first capturing the only pawn at its right side and then capturing the other pawns.
{"P......P", "........", "........", "........", "...KK...", "........", "........", "P......P"}
Returns: 20
If we mark the kings with A and B and the pawns with 1, 2, 3 and 4, then one possible solution is that A captures the pawns 1 and 2, and B captures the pawns 3 and 4. 1......4 ........ ........ ........ ...AB... ........ ........ 2......3
{"PK....KP", "K......K", "........", "........", "...KK...", "........", "K......K", "PK....KP"}
Returns: 4
{"........", "..P.KK..", ".P..KPK.", "..P.K...", "K...K.PK", ".P..P...", ".K...P.K", "..P...P."}
Returns: 11
{".PP.K..P", "..P.KK..", ".P..KPK.", "..P.K...", "K.....P.", ".P......", ".K...P..", "...KK..."}
Returns: 13
{".P....K.", "K.PK..P.", "K.K.....", "KP......", "...P...K", "......P.", "........", ".K.PP..."}
Returns: 10
{"....K...", ".......P", "........", "K.K.....", "....P...", "...K....", ".....K..", "..K..K.."}
Returns: 4
{".P.P....", "K...PPK.", "........", ".....P.P", "...P.P..", ".....K..", "....K...", ".P...P.."}
Returns: 15
{"........", ".......P", "........", "K..P.P.K", ".K....K.", "........", "....P...", ".....P.."}
Returns: 9
{"........", ".P......", "........", "....P..K", ".....K.P", "........", ".K......", "......P."}
Returns: 8
{"P.P..K..", ".......K", "P.P..KK.", "....PP..", "......P.", "...P.P..", "....P...", "...K...."}
Returns: 14
{".P......", "........", ".......K", "....K...", "........", "P.P...P.", "..K.....", "......K."}
Returns: 8
{"K..P...P", "......P.", "PP......", ".......K", ".K......", ".P..KK.P", "KP...P..", "......P."}
Returns: 14
{"K......K", "........", "K....P..", "......P.", "......P.", "P..P.P.P", "....P..K", "..PK.P.."}
Returns: 13
{"....K...", "..K.....", ".....KKK", "....K..P", "........", ".K......", "........", "......K."}
Returns: 1
{"...P....", "..P...P.", "........", "......P.", "....PP..", "....K...", ".P......", ".....K.."}
Returns: 13
{"P.K.....", "....P...", "........", "......K.", "........", ".......K", "........", "...P...."}
Returns: 8
{".P...P..", "PK.P....", "K......K", "........", "........", "..P.....", "K....P..", ".K....P."}
Returns: 12
{"........", "P..K....", ".......K", "P....K..", "...PP...", "K.K...PK", "....P...", ".K.....P"}
Returns: 11
{"........", "P..K....", "........", "P.......", "...P....", "........", ".....K..", "K......."}
Returns: 7
{"..P.....", "........", ".P......", "P.......", "........", "..P..KP.", "....P...", "........"}
Returns: 10
{"P.KK.K..", "...P.P.P", "........", "......K.", ".K..P...", "...P..P.", ".PP.....", "........"}
Returns: 13
{".....P.P", "..K....P", "....K...", "..PP...P", "...K..KK", "........", "K.......", "KP.K...."}
Returns: 9
{"K..P....", ".....P..", ".......P", "........", "........", "........", "........", ".P......"}
Returns: 13
{"........", "P.......", "........", "........", "........", "..P.....", "....K...", "....P..."}
Returns: 7
{"P..P.K..", "K......P", "....K...", "PPK.KK..", "...K....", "P......K", "..K.P.PK", "...P.P.."}
Returns: 13
{"K...P...", "KK.PPP.P", "K.K.....", "..KK....", "K.P.....", "........", "KPK.....", "..PPP..."}
Returns: 11
{"P.....K.", ".P...P..", "......P.", "PPK..K..", "K.P..K..", ".PK.....", "KPP...K.", ".K...K.."}
Returns: 11
{".P......", "..P..P.K", "...P...K", "....PKK.", "P....K.K", "..P....K", ".....KP.", "...PPKK."}
Returns: 13
{"....K..P", ".K.P..KP", "PK..K..K", "P....P..", "...KK...", "..K.....", "..K.....", "PPP...P."}
Returns: 12
{"PK...KPK", "......K.", "...K....", "..KPK...", "...K....", "........", "........", "K..P.K.P"}
Returns: 8
{"P.P.P.P.", ".K.K.K.K", "........", "P.....P.", "..K..K..", "........", "P.P.P.P.", ".K.K.K.K"}
Returns: 11
{"K.K..K.K", "........", "K.......", "....P...", "...P....", "K.......", "........", "K.K..K.K"}
Returns: 4
{"KKKKKKKK", "...KK...", "........", "........", "........", "........", "...PP...", "PPPPPPPP"}
Returns: 15
{"K......P", ".K....P.", "..K..P..", "...KP...", "...PK...", "..P..K..", ".P....K.", "P.PKPK.K"}
Returns: 10
{"..K....K", "P...K...", "K......P", ".P......", "..P..K..", "P...PPKK", ".P.P.KP.", ".K...K.."}
Returns: 12
{"..P..K.K", "K.P.P.P.", ".......P", "..K.K...", "PK.KP...", "PP.KK...", "......K.", "..P....."}
Returns: 12
{"........", "..P.K...", ".KP..K..", "K....K..", "P....PKP", ".PPKPKK.", "........", "..PK...P"}
Returns: 11
{"..P...K.", "...P....", "..K.K...", "......K.", "PPP..PP.", "..KK....", "P.KK.PKK", "..P....."}
Returns: 11
{"...K....", "KP.P....", ".PPPP..K", "PKPK..K.", ".P......", "..K.....", "...KK..K", "..P....."}
Returns: 10
{"........", ".P..P...", ".K..P...", "........", "........", ".....K..", ".K......", "........"}
Returns: 5
{"..K.PK..", "K.K.K...", ".K..K...", "........", ".....P..", "........", "........", "........"}
Returns: 3
{"...P..K.", ".K..PP..", ".P....P.", "...P....", "..PKKPK.", "P...K...", "....KK.K", "....P..."}
Returns: 12
{"......KK", "........", ".K....K.", "......P.", "...P...P", "......K.", "..K..P.K", "P.....P."}
Returns: 8
{".......K", ".K.P....", "K..K....", "......K.", "........", ".....P..", "........", "P.....KK"}
Returns: 8
{"P......P", "........", "........", "........", "....K...", "........", "........", "P......P"}
Returns: 24
{ ".P.K..K.", "..K.K...", "..K.....", "........", "........", "..K..K..", "..P....P", "........" }
Returns: 4
{ "K..K....", "..K....K", ".....P.K", "......PP", ".....PP.", "..P..P..", "....PP..", "....K..." }
Returns: 10
{ "..P.....", ".P......", "...P...P", "K..P....", "...PPP..", "........", "........", "...P...." }
Returns: 16
{ "......K.", "P.......", "....P..P", "...P..K.", ".P..PP..", "....P...", "........", "P......." }
Returns: 17
{ "..P.P...", "........", ".......P", "PP......", ".P.KP...", "....P...", "........", "......P." }
Returns: 18
{ ".....P.P", "P.P.....", "..P.....", "........", "...P....", "..K..PP.", ".P......", "........" }
Returns: 19
{ "P....P..", "....PP.K", "....P..P", "........", ".....P..", "........", ".....P..", ".P......" }
Returns: 21
{ "...P...P", ".....P..", "...P....", "K.......", "....P...", ".......P", "...P....", "P.P..P.." }
Returns: 22
{ "P....P.P", "P.......", "......P.", "PPP.....", "........", "........", "........", "K...P.P." }
Returns: 23
{ "P...P..P", "........", "......PP", ".P..P...", "........", "......P.", "....P...", "..P....K" }
Returns: 24
{ ".P..P..P", "........", "....P...", "........", "........", "K..P....", "P......P", "P..P...P" }
Returns: 25
{ "P..P...P", "........", "P.......", "...P....", ".......P", "..K.....", ".P...P..", "..P....P" }
Returns: 26
{ "P...P..P", ".......P", ".....P..", "...P....", "........", "P..P....", "........", "P...K..P" }
Returns: 27
{ "......P.", "P...PP..", ".......P", ".....K.K", "P.P.....", "........", "P......P", "...P...." }
Returns: 22
{ ".P..P.P.", "........", "........", "P...P...", ".......P", "K...PP..", "..P.....", "P......K" }
Returns: 23
{ "P......P", "........", "....P.P.", "P.......", "..KK....", "P.......", "P.K.P..P", ".......P" }
Returns: 20
{ "......K.", "P..KP...", "....K...", "..P....P", ".PK.....", "PP......", "...P....", ".P.....P" }
Returns: 19
{ ".P...P.K", "...K.P..", "P..K.K..", "K.......", "........", "...PP..P", "P.......", "......PP" }
Returns: 18
{ "P..P...P", "PK......", "........", "..K.....", "P...K.P.", ".PP.....", "....K...", ".KK.P..P" }
Returns: 19
{ "P.P.....", ".K......", "P.P.K...", "....KK..", ".KK....P", "P....P..", "..P.....", "P..KP..." }
Returns: 17
{ "..P.PK..", "....K..K", "K....K.K", ".P.K.P..", "...P....", "...P....", "...K.P.P", "P..P...." }
Returns: 15
{ ".P...K..", ".....P.P", "....KP..", "..PKKP..", "........", ".KK.K...", "K.......", "P.PP...P" }
Returns: 16
{ "K.PK...P", ".....P..", "...P...P", "KP...K..", "...P....", "........", "KP.KKP..", ".K..PK.." }
Returns: 15
{ "K..PP.P.", "...K....", ".KKP..KP", "....P...", "...K..P.", "P.......", "......KK", "P..K..PK" }
Returns: 15
{".....P..","........","P....K..","..PPP...",".P..K.K.","P.....K.","P...P.P.","......K."}
Returns: 14
{"....P...","..P..P.P","P....P..",".PKP....","........","........","........",".......P"}
Returns: 17
{".......P","..P.....","........","....KP..",".P.P....","..P.P...","....P...",".....PP."}
Returns: 18
{".P......","..P.P...","........",".....P..",".KP.....","....P...","........","P.PP..P."}
Returns: 19
{"..P....P","P...P...",".....P..","........","..P.....",".P...KPK","........","P...P..."}
Returns: 20
{".P....P.","....PK..","..KP....","........","..P..P.P","P.....P.","........",".PK....."}
Returns: 15
{"P.K...P.","...P....",".K......","P....K..",".PP..P..","........","...PP.P.","......K."}
Returns: 15
{"....P.P.", "........", ".PK.....", "P.......", "P.K....K", "....KP.K", "P......P", "..P...P."}
Returns: 15
{".....K..","........","..P..PK.","....P..P","P.P.K...","..K...P.","..PK...P","......PK"}
Returns: 12
{"..K...P.","..P.....",".P...KK.",".P...PP.",".KP..K..","..P.....",".P....P.","K......."}
Returns: 12
{"KK.....P","...P...K",".PP...K.","..KP....","...K....","....PK..","P..P..PP","........"}
Returns: 13
{"K.P..P..","..K.KP.K","..K..K..","P.......","P..P....","KP.P...P","....K...",".......P"}
Returns: 13
{"K...P...",".K...K..","PKK...PP",".......P",".......P","P..K..K.",".PK.P...","..P...K."}
Returns: 10
{"P.P.KK.K",".P..P...",".KP.PK..","P.P...K.","....KK.K","K...P...","P.......","........"}
Returns: 12
{".P...PP.","........","...PP.KP","K...K...","..KP..K.","P.K.P.KP",".......K","....KK.."}
Returns: 13
{".......P",".PP.P...","P.....K.","...K.P..","...P.KPP","....K...","..KK.K.K","..P.KK.."}
Returns: 13
{".P.K..K.","K.K....K","....PP..","........","..P...P.","...PP...",".P......","P.....P."}
Returns: 16
{".P...K.K",".KK.K..K","...P...P","........","...PP...","..PP....",".....PP.",".P......"}
Returns: 14
{".KKK...K","K.PKP.K.","..P..PP.","........","....P...",".....P.P","...P..P.","........"}
Returns: 13
{"KP.K.K.K",".KPKKK.P","........","........","...PP...",".......P","P...P...","..P..P.."}
Returns: 17
{"P.K.KKK.",".KKKKK..","PPP.P.P.","........","....P...",".....PP.","..P.....","........"}
Returns: 13
{"KKK.KKP.",".KPKK.KK","P.......","........","..P.....","......P.","..PP.P..","PP......"}
Returns: 14