Home > Archive > Fortran > December 2005 > Re: Write a Fortran program to solve Sudoku puzzles :-)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Re: Write a Fortran program to solve Sudoku puzzles :-)
|
|
| AN O'Nymous 2005-12-05, 7:04 pm |
|
Rich Townsend wrote:
> and this being c.l.f, will we ever see a working program?
Oh, ye of little faith :-)
In the spirit of open sourcing, I'll start the ball rolling with the
"validity checker" below, which checks if the row/column/region
requirements are met once you give it the element's co-ordinates to
check for.
It assumes that all relevant entries are known - either given or
guessed.
! This section assigns the regions for the entry
x_region = int(real(x)/3.)+1
y_region = int(real(y)/3.)+1
! This section checks the validity of the region
count = 0
x_offset1 = 3*x_region-1
x_offset2 = 3*x_region
y_offset1 = 3*y_region-1
y_offset2 = 3*y_region
do i = 1,9
count(i) = i
enddo
do i = x_offset1, x_offset2
do j = y_offset1, y_offset2
do k = 1, 9
if (count(k) == sudoku_array(i,j)) then
count(k) = 0
endif
enddo
enddo
enddo
test_integer = 0
do i = 1,9
test_integer = test_integer + count(i)
enddo
if (test_integer == 0) then
Print *,'Valid region'
endif
if (test_integer > 0) then
Print *,'Invalid region'
endif
! This section checks the validity of the rows/columns
do i = 1,9
count(i) = i
enddo
do i = 1, 9
do k = 1, 9
if (count(k) == sudoku_array(i,y)) then
count(k) = 0
endif
enddo
enddo
test_integer = 0
do i = 1,9
test_integer = test_integer + count(i)
enddo
if (test_integer == 0) then
Print *,'Rows OK'
endif
if (test_integer > 0) then
Print *,'Rows not OK'
endif
do i = 1,9
count(i) = i
enddo
do j = 1, 9
do k = 1, 9
if (count(k) == sudoku_array(x,j)) then
count(k) = 0
endif
enddo
enddo
test_integer = 0
do i = 1,9
test_integer = test_integer + count(i)
enddo
if (test_integer == 0) then
Print *,'Columns OK'
endif
if (test_integer > 0) then
Print *,'Columns not OK'
endif
| |
| Richard Maine 2005-12-05, 7:04 pm |
| AN O'Nymous <a_n_onymous80@yahoo.co.uk> wrote:
> Rich Townsend wrote:
>
>
> Oh, ye of little faith :-)
I think you missed the important part before the "and" in Rich's post.
:-) Or perhaps you intentionally choose to do so,which would be fine.
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
| |
| Michael Metcalf 2005-12-05, 7:04 pm |
|
"AN O'Nymous" <a_n_onymous80@yahoo.co.uk> wrote in message
news:1133795510.830786.177900@g44g2000cwa.googlegroups.com...
> ! This section checks the validity of the rows/columns
> do i = 1,9
> count(i) = i
> enddo
>
> do i = 1, 9
> do k = 1, 9
> if (count(k) == sudoku_array(i,y)) then
> count(k) = 0
> endif
> enddo
> enddo
Might I suggest that once count(k) has been set 0 you can exit the 2 loops.
But the whole section is, I think, equivalent to (untested):
kount = ( (/ i, i = 1, 9 /) ) ! count is an intrinsic, so a name to be
avoided
do k = 1, 9
if(any(kount(k) == sudoku_array(:,y) ) ) kount(k) = 0 ! is y correct? do
you mean j?
end do
or even
where (kount == sudoku_array(:,y)) kount = 0
In my own code, I have found count, any and where invaluable.
Regards,
Mike Metcalf
| |
| Michael Metcalf 2005-12-05, 7:04 pm |
|
"AN O'Nymous" <a_n_onymous80@yahoo.co.uk> wrote in message
news:1133798587.314883.226280@o13g2000cwo.googlegroups.com...
>
> Michael Metcalf wrote:
>
>
> Do you mean that there is no point testing the legality of an element
> if it fails one test? In general, unless an element fails, you'd have
> to test region + row + column to be certain if it is correct.
>
Yes. Once count(k) is set to 0 it cannot get changed to any other value
(only to 0 again), so there's no point in continuing the double loop.
Regards,
Mike Metcalf
|
|
|
|
|