2011年9月25日 星期日

數獨

 數獨說明(略)。
 請設計一個程式,可以幫助玩家提示空格中尚可填入的數字。
 例如,(如輸入範例test3.txt),第5行第3列的位置尚可填入1這個數字;第6行第7列的位置尚可填入7與8這兩個數字;而第5行第5列的位置已有數字填入則輸出0。
 輸入說明:
 輸 入的第一列到第九列為數獨資料,每一列由九個數字所組成。數字1到9為已填入的數字,0代表尚未填入數值的空格,每個數字與數字間的區隔為一個空白符號。 第十列及以後的資料為要提示位置的座標(座標的定義方式為右上角為9 1,左下角為1 9,第一個數字代表行,第二個數字代表列),當為0 0 時表示結束。
 輸入範例:in.txt
4 0 0 8 0 5 0 1 0 
2 0 0 0 0 9 0 8 0
6 0 8 0 0 3 4 9 0
0 0 5 0 2 0 6 0 0 
0 0 1 0 7 0 9 0 0
0 0 7 0 8 0 3 0 0
0 9 0 1 0 0 0 0 2
0 8 0 4 0 0 0 0 9
0 3 0 6 0 2 0 0 7
5 3
6 7
5 5
0 0 
 輸出說明:每個提示位置有一列輸出結果,表示尚可填入的數字,請由小到大排列。每個數字與數字間的區隔為一個空白符號。若該位置已有填入數字,或找不到提示的數字則輸出0。
 輸出範例:out.txt
1
7 8
0

額外說明:(從學長那邊貼過來的,參考)
1.這題雖然前面已經做過數獨遊戲了,但是一來每個人還沒做解題的部份;二來用以往的競賽題目,來了解題目可以怎麼出。
2.還是要再提醒每個人,不可以只用這樣的一個例子去解而已。要自己再加輸入例子,再將輸出的結果也一併貼出來。
3.尤其要注意「邊界值」,像這題的1 1和9 9或是9 1或是1 9。像前一題總和檢查時,要注意的邊界值就是用100個數字的數列及用2個數字的數列來當成是輸入例子。

2 則留言:

  1. 先做類似的題目:P

    Dim X(9, 9) As Byte
    Dim Y(9) As Byte
    Private Sub Form_Load()

    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    For i = 1 To 9
    For j = 1 To 9
    Input #1, X(j, i)
    Next j
    Next i

    Do
    Input #1, A, B
    ans = ""
    Dim OK(9) As Boolean
    If A = 0 Then Exit Do
    If X(A, B) <> 0 Then Print #2, 0: Exit Do

    For i = 1 To 9
    OK(i) = True
    Next i

    For i = 1 To 9
    Y(i) = X(A, i)
    Next i

    For i = 1 To 9
    If check(i) = False Then OK(i) = False
    Next i

    For i = 1 To 9
    Y(i) = X(i, B)
    Next i

    For i = 1 To 9
    If check(i) = False Then OK(i) = False
    Next i

    Select Case A
    Case 1 To 3: AI = 1: AJ = 3
    Case 4 To 6: AI = 4: AJ = 6
    Case 7 To 9: AI = 7: AJ = 9
    End Select

    Select Case B
    Case 1 To 3: BI = 1: BJ = 3
    Case 4 To 6: BI = 4: BJ = 6
    Case 7 To 9: BI = 7: BJ = 9
    End Select

    N = 1
    For i = AI To AJ
    For j = BI To BJ
    Y(N) = X(i, j)
    N = N + 1
    Next j
    Next i

    For i = 1 To 9
    If check(i) = False Then OK(i) = False
    Next i

    For i = 1 To 9
    If OK(i) = True Then ans = ans & " " & i
    Next i
    If ans <> "" Then Print #2, ans Else Print #2, 0

    Loop Until A = 0

    Close #2
    Close #1
    End
    End Sub

    Function check(C) As Boolean
    P = True
    For i = 1 To 9
    If Y(i) = C Then P = False: Exit For
    Next i
    check = P
    End Function


    in.txt-----------------
    2 5 8 4 0 7 1 3 0
    0 4 0 1 8 9 2 5 0
    9 0 0 2 3 5 6 8 4
    6 1 3 5 4 0 9 0 0
    5 2 0 3 7 0 4 1 8
    4 8 7 0 0 2 5 0 0
    1 0 2 8 5 4 0 9 6
    0 9 5 6 2 0 0 0 1
    8 0 4 7 9 1 3 2 0
    3 3
    8 4
    5 6
    9 9
    2 9
    0 0

    out.txt--------------------------
    1
    7
    1 6
    5
    6

    回覆刪除
  2. Dim n(9, 9), X As String, Y, ban
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2

    For i = 1 To 9
    For j = 1 To 9
    Input #1, n(j, i)
    Next
    Next





    Do While Not EOF(1)
    ban = 0: ans = ""
    Input #1, X
    If X <> "0 0" Then

    Y = Split(X)
    If n(Y(0), Y(1)) <> 0 Then ans = "0": GoTo Pr:
    For i = 1 To 9
    ban = ban & n(Y(0), i):
    ban = ban & n(i, Y(1))

    Next



    Call Area((Y(0)), (Y(1)))
    '
    For i = 1 To 9
    If InStr(ban, i) = 0 Then ans = ans & i
    Next

    Pr:
    Print #2, ans

    End If

    Loop



    Close
    Close
    End
    End Sub

    Sub Area(a, b)
    Do Until a = 3 Or a = 6 Or a = 9
    a = a + 1
    Loop
    Do Until b = 3 Or b = 6 Or b = 9
    b = b + 1
    Loop

    For i = a - 2 To a
    For j = b - 2 To b
    ban = ban & n(i, j)
    Next
    Next
    End Sub


    用你的輸入,輸出跟你一樣

    回覆刪除