99define ("N " , 9 );
1010define ("UNASSIGNED " , 0 );
1111
12- function  SolveSudoku (array  &$ grid ): bool  {
12+ function  solveSudoku (array  &$ grid ): bool  {
1313    $ row  = $ col  = 0 ;
1414
15-     if  (!FindUnassignedLocation ($ grid , $ row , $ col ))
15+     if  (!findUnassignedLocation ($ grid , $ row , $ col ))
1616	return  true ; // success! no empty space 
1717
1818    for  ($ num  = 1 ; $ num  <= N; $ num ++) {
1919	if  (isSafe ($ grid , $ row , $ col , $ num )) {
2020	    $ grid [$ row ][$ col ] = $ num ; // make tentative assignment 
2121
22- 	    if  (SolveSudoku ($ grid ))
22+ 	    if  (solveSudoku ($ grid ))
2323		return  true ;  // return, if success// return, if success 
2424
2525	    $ grid [$ row ][$ col ] = UNASSIGNED ;  // failure, unmake & try again 
@@ -28,23 +28,23 @@ function SolveSudoku(array &$grid): bool {
2828    return  false ; // triggers backtracking 
2929}
3030
31- function  FindUnassignedLocation (array  &$ grid , int  &$ row , int  &$ col ): bool  {
31+ function  findUnassignedLocation (array  &$ grid , int  &$ row , int  &$ col ): bool  {
3232    for  ($ row  = 0 ; $ row  < N; $ row ++)
3333	for  ($ col  = 0 ; $ col  < N; $ col ++)
3434	    if  ($ grid [$ row ][$ col ] == UNASSIGNED )
3535		return  true ;
3636    return  false ;
3737}
3838
39- function  UsedInRow (array  &$ grid , int  $ row , int  $ num ): bool  {
39+ function  usedInRow (array  &$ grid , int  $ row , int  $ num ): bool  {
4040    return  in_array ($ num , $ grid [$ row ]);
4141}
4242
43- function  UsedInColumn (array  &$ grid , int  $ col , int  $ num ): bool  {
43+ function  usedInColumn (array  &$ grid , int  $ col , int  $ num ): bool  {
4444    return  in_array ($ num , array_column ($ grid , $ col ));
4545}
4646
47- function  UsedInBox (array  &$ grid , int  $ boxStartRow , int  $ boxStartCol , int  $ num ):bool  {
47+ function  usedInBox (array  &$ grid , int  $ boxStartRow , int  $ boxStartCol , int  $ num ):bool  {
4848    for  ($ row  = 0 ; $ row  < 3 ; $ row ++)
4949	for  ($ col  = 0 ; $ col  < 3 ; $ col ++)
5050	    if  ($ grid [$ row  + $ boxStartRow ][$ col  + $ boxStartCol ] == $ num )
@@ -54,9 +54,9 @@ function UsedInBox(array &$grid, int $boxStartRow, int $boxStartCol, int $num):b
5454
5555function  isSafe (array  $ grid , int  $ row , int  $ col , int  $ num ): bool  {
5656
57-     return  !UsedInRow ($ grid , $ row , $ num ) &&
58- 	    !UsedInColumn ($ grid , $ col , $ num ) &&
59- 	    !UsedInBox ($ grid , $ row  - $ row  % 3 , $ col  - $ col  % 3 , $ num );
57+     return  !usedInRow ($ grid , $ row , $ num ) &&
58+ 	    !usedInColumn ($ grid , $ col , $ num ) &&
59+ 	    !usedInBox ($ grid , $ row  - $ row  % 3 , $ col  - $ col  % 3 , $ num );
6060}
6161
6262/* A utility function to print grid  */ 
@@ -80,7 +80,7 @@ function printGrid(array $grid) {
8080    [0 , 0 , 2 , 0 , 9 , 0 , 5 , 0 , 0 ]
8181];
8282
83- if  (SolveSudoku ($ grid ) == true )
83+ if  (solveSudoku ($ grid ) == true )
8484    printGrid ($ grid );
8585else 
8686    echo  "No solution exists " ;
0 commit comments