1 8 15 22 29 36 43
2 9 16 23 30 37 44
3 10 17 24 31 38 45
4 11 18 25 32 39 46
5 12 19 26 33 40 47
6 13 20 27 34 41 48
7 14 21 28 35 42 49
In the 7x7 array given above, select six numbers from six cells simultaneously satisfying the following conditions:
- No two numbers chosen should belong to the same row;
- No two numbers chosen should belong to the same column;
- In the set of numbers chosen, each of the ten digits appears once and only once.
For example, if the numbers selected are 7,9,18,26,30 and 45 then this violates the given conditions as 9 and 30 belong to the same row.
DECLARE SUB doCol (col!)
DECLARE FUNCTION gridNo! (row!, col!)
CLEAR , , 25000
DIM SHARED rowused(7), colnotused, nums(7), numstr$
CLS
FOR colnotused = 1 TO 7
doCol 1
NEXT colnotused
SUB doCol (col)
IF col <> colnotused THEN
FOR row = 1 TO 7
IF rowused(row) = 0 THEN
s$ = LTRIM$(STR$(gridNo(row, col)))
IF LEFT$(s$, 1) <> RIGHT$(s$, 1) OR LEN(s$) = 1 THEN good = 1: ELSE good = 0
FOR i = 1 TO LEN(s$)
IF INSTR(numstr$, MID$(s$, i, 1)) > 0 THEN good = 0: EXIT FOR
NEXT
IF good THEN
nums(col) = gridNo(row, col)
savenumstr$ = numstr$
numstr$ = numstr$ + "," + s$
rowused(row) = 1
IF col = 7 THEN
PRINT numstr$
ELSE
doCol col + 1
END IF
numstr$ = savenumstr$
nums(col) = 0
rowused(row) = 0
END IF
END IF
NEXT
ELSE
IF col = 7 THEN
PRINT numstr$
ELSE
doCol col + 1
END IF
END IF
END SUB
FUNCTION gridNo (row, col)
gridNo = (row - 1) MOD 7 + 1 + 7 * (col - 1)
END FUNCTION
finds
6,8,17,25,30,49
|
Posted by Charlie
on 2013-03-05 15:42:23 |