2012年10月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

2 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Dim K(9) As Integer
    Dim x(10, 10) As Integer
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    n = 0
    For i = 1 To 9
    Line Input #1, X1
    a = Split(X1, " ")
    n = n + 1
    For i1 = 0 To 8
    x(n, i1 + 1) = a(i1)
    Next
    Next
    Do While Not EOF(1)

    For i = 1 To 9
    K(i) = 0
    Next
    Input #1, X2, Y2
    If X2 = 0 And Y2 = 0 Then Exit Do
    Select Case Y2
    Case 1 To 3
    x3 = 2
    Case 4 To 6
    x3 = 5
    Case 7 To 9
    x3 = 8
    End Select
    Select Case X2
    Case 1 To 3
    y3 = 2
    Case 4 To 6
    y3 = 5
    Case 7 To 9
    y3 = 8
    End Select
    For i = 1 To 9
    If x(i, X2) <> 0 Then K(x(i, X2)) = 1
    If x(Y2, i) <> 0 Then K(x(Y2, i)) = 1
    Next
    If x(x3 + 1, y3 + 1) <> 0 Then K(x(x3 + 1, y3 + 1)) = 1
    If x(x3 + 1, y3 - 1) <> 0 Then K(x(x3 + 1, y3 - 1)) = 1
    If x(x3 + 1, y3) <> 0 Then K(x(x3 + 1, y3)) = 1
    If x(x3 - 1, y3 + 1) <> 0 Then K(x(x3 - 1, y3 + 1)) = 1
    If x(x3 - 1, y3 - 1) <> 0 Then K(x(x3 - 1, y3 - 1)) = 1
    If x(x3 - 1, y3) <> 0 Then K(x(x3 - 1, y3)) = 1
    If x(x3, y3 + 1) <> 0 Then K(x(x3, y3 + 1)) = 1
    If x(x3, y3 - 1) <> 0 Then K(x(x3, y3 - 1)) = 1
    If x(x3, y3) <> 0 Then K(x(x3, y3)) = 1
    ch = False
    If x(Y2, X2) = 0 Then
    For i = 1 To 9
    If K(i) = 0 Then
    Print #2, i,
    ch = True
    End If
    Next
    End If
    If ch = False Then Print #2, 0
    Print #2,

    Loop
    Close
    Close
    End
    End Sub

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\test3.txt" For Input As #1
    Open App.Path & "\result3.txt" For Output As #2
    For i = 1 To 9
    Line Input #1, sudo(i)
    Next i
    c = 1
    For i = 1 To 9
    a = Split(sudo(i), " ")
    For m = 0 To 8
    num(c) = a(m)
    c = c + 1
    Next m
    Next i

    'For i = 1 To 81
    'Print #2, num(i);
    'If i Mod 9 = 0 Then Print #2,
    'Next i

    c = 1
    For i = 1 To 9
    For j = 1 To 9
    b(i, j) = num(c)
    c = c + 1
    'Print #2, b(i, j);
    Next j
    'Print #2,
    Next i

    Do
    Input #1, y, x
    If y = 0 And x = 0 Then Exit Do
    If b(x, y) = 0 Then
    Call check(x, y)
    For j = 1 To 9
    For i = 0 To List1.ListCount - 1
    If j = List1.List(i) Then t = False: Exit For Else t = True
    Next i
    If t = True Then Print #2, j;
    Next j
    Print #2,
    Else
    Print #2, 0
    End If
    List1.Clear
    Loop


    Close #2
    Close #1
    End
    End Sub

    Sub check(ByVal x, ByVal y)
    For i = 1 To 9
    If i <> x Then
    If b(i, y) <> 0 Then List1.AddItem Val(b(i, y))
    End If
    Next i

    For i = 1 To 9
    If i <> y Then
    If b(x, i) <> 0 Then List1.AddItem Val(b(x, i))
    End If
    Next i

    Select Case x
    Case 1, 2, 3
    Select Case y
    Case 1, 2, 3
    For i = 1 To 3
    For j = 1 To 3
    If i <> x And j <> y Then
    If b(i, j) <> 0 Then List1.AddItem b(i, j)
    End If
    Next j
    Next i

    Case 4, 5, 6
    For i = 1 To 3
    For j = 4 To 6
    If i <> x And j <> y Then
    If b(i, j) <> 0 Then List1.AddItem b(i, j)
    End If
    Next j
    Next i

    Case 7, 8, 9
    For i = 1 To 3
    For j = 7 To 9
    If i <> x And j <> y Then
    If b(i, j) <> 0 Then List1.AddItem b(i, j)
    End If
    Next j
    Next i
    End Select
    Case 4, 5, 6
    Select Case y
    Case 1, 2, 3
    For i = 4 To 6
    For j = 1 To 3
    If i <> x And j <> y Then
    If b(i, j) <> 0 Then List1.AddItem b(i, j)
    End If
    Next j
    Next i

    Case 4, 5, 6
    For i = 4 To 6
    For j = 4 To 6
    If i <> x And j <> y Then
    If b(i, j) <> 0 Then List1.AddItem b(i, j)
    End If
    Next j
    Next i

    Case 7, 8, 9
    For i = 4 To 6
    For j = 7 To 9
    If i <> x And j <> y Then
    If b(i, j) <> 0 Then List1.AddItem b(i, j)
    End If
    Next j
    Next i
    End Select
    Case 7, 8, 9
    Select Case y
    Case 1, 2, 3
    For i = 7 To 9
    For j = 1 To 3
    If i <> x And j <> y Then
    If b(i, j) <> 0 Then List1.AddItem b(i, j)
    End If
    Next j
    Next i
    Case 4, 5, 6
    For i = 7 To 9
    For j = 4 To 6
    If i <> x And j <> y Then
    If b(i, j) <> 0 Then List1.AddItem b(i, j)
    End If
    Next j
    Next i
    Case 7, 8, 9
    For i = 7 To 9
    For j = 7 To 9
    If i <> x And j <> y Then
    If b(i, j) <> 0 Then List1.AddItem b(i, j)
    End If
    Next j
    Next i
    End Select
    End Select
    End Sub

    回覆刪除