How to resolve the algorithm Flipping bits game step by step in the J programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Flipping bits game step by step in the J programming language
Table of Contents
Problem Statement
Given an N×N square array of zeroes or ones in an initial configuration, and a target configuration of zeroes and ones.
The game is to transform one to the other in as few moves as possible by inverting whole numbered rows or whole lettered columns at once (as one move). In an inversion. any 1 becomes 0, and any 0 becomes 1 for that whole row or column.
Create a program to score for the Flipping bits game.
Show an example of a short game here, on this page, for a 3×3 array of bits.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Flipping bits game step by step in the J programming language
Source code in the j programming language
start=:3 :0
Moves=:0
N=:i.y
Board=: ?2$~,~y
'fr fc'=. (2,y)$}.#:(+?&.<:@<:)2x^2*y
End=: fr~:fc~:"1 Board
Board;End
)
abc=:'abcdefghij'
move=:3 :0
fc=. N e.abc i. y ([-.-.)abc
fr=. N e._-.~_ "."0 abc-.~":y
Board=: fr~:fc~:"1 Board
smoutput (":Moves=:Moves++/fr,fc),' moves'
if. Board-:End do.
'yes'
else.
Board;End
end.
)
start 3
┌─────┬─────┐
│1 1 1│1 0 1│
│1 1 0│0 1 1│
│1 0 0│0 0 1│
└─────┴─────┘
move 'b2'
2 moves
┌─────┬─────┐
│1 0 1│1 0 1│
│1 0 0│0 1 1│
│0 0 1│0 0 1│
└─────┴─────┘
move '1'
3 moves
yes
You may also check:How to resolve the algorithm Compiler/code generator step by step in the C programming language
You may also check:How to resolve the algorithm Caesar cipher step by step in the AWK programming language
You may also check:How to resolve the algorithm Sorting algorithms/Gnome sort step by step in the Rascal programming language
You may also check:How to resolve the algorithm Send an unknown method call step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm MD5/Implementation step by step in the Sidef programming language