Advertisement
2002ASP OLE/ COM/ DCOM/ Active-X #8571

ASP 8 Queens solution

To find the solutions to the 8 queens puzzle. The puzzle asks "How can you place 8 Queens on a chess board so that no Queen can attack any other queen?"

AI

AI சுருக்கம்: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

மூலக் குறியீடு
original-source
<%@ LANGUAGE=VBScript %>
<HTML><BODY>
<% Server.ScriptTimeOut=200 %>
<% If Request.Form("NumSolutions") = "" Then %>
<FORM method=Post>
This web page can show you all the solutions to the 8-Queens puzzle. There are 92 
possible solutions. Please enter the number of solutions you would like to see then 
click the submit button.<BR><BR>
This demonstrates the use of functions in an ASP page. A recursive algorithim 
is used to solve the problem. If you are familiar with VB and want to do a compairison
you can look at my 8 Queens solution in the VB section of www.planetsourcecode.com. 
This is almost a pure cut and paste. It only took about 10 minutes to make it into 
an ASP application. The only real changes are in the Sub UpdateDisplay() other than 
that all I did was delete all the data typing since in VBScript everything is a Variant.<BR><BR>
<INPUT name=NumSolutions>
<INPUT type=submit value="Submit">
<% Else %>
<%
Dim NumToFind
Dim mblnBoard(7, 7)
Dim solutions
NumToFind = Request.Form ("NumSolutions")
bln8Queens(0)
Function blnCheckUp(ByVal intRow, ByVal intColumn) 
blnCheckUp = True
Do Until intRow = 0 Or intColumn = 0
  intRow = intRow - 1
  intColumn = intColumn - 1
  If mblnBoard(intRow, intColumn) Then
    blnCheckUp = False
    Exit Function
  End If
Loop
End Function
Function blnCheckDown(ByVal intRow, ByVal intColumn) 
blnCheckDown = True
Do Until intRow = 7 Or intColumn = 0
  intRow = intRow + 1
  intColumn = intColumn - 1
  If mblnBoard(intRow, intColumn) Then
    blnCheckDown = False
    Exit Function
  End If
Loop
End Function
Function blnCheckAcross(ByVal intRow, ByVal intColumn) 
blnCheckAcross = True
Do Until intColumn = 0
  intColumn = intColumn - 1
  If mblnBoard(intRow, intColumn) Then
    blnCheckAcross = False
    Exit Function
  End If
Loop
End Function
Function blnIsSafe(ByVal intRow, ByVal intColumn) 
  blnIsSafe = blnCheckUp(intRow, intColumn) And blnCheckDown(intRow, intColumn) And blnCheckAcross(intRow, intColumn)
End Function
Sub UpdateDisplay()
	Dim intRow
	Dim intColumn 
	Response.Write "<HR>Solution #" & solutions & "<BR>"
	Response.Write "<table border=1 align=Left width=190 cellborder=1 cellspacing=0>"
	For intRow = 0 to 7
		Response.Write "<TR>"
		For intColumn = 0 to 7
			If (intColumn + intRow) MOD 2 = 1 Then
				Response.Write "<td bgcolor =""#000000"""
			Else
				Response.Write "<td" 
			End If
			If mblnBoard(intRow,intColumn) = True Then
				Response.Write " align=center><font color =""#FF0066""> Q </font></td>"
			Else
				Response.Write "> &nbsp </td>"
			End If
		Next
		Response.Write "</tr>"
	Next
	Response.Write "</table><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR>"
End Sub
Function bln8Queens(ByVal intColumn) 
  Dim intRow
  Do
    If intColumn = 7 And blnIsSafe(intRow, intColumn) Then
      mblnBoard(intRow, intColumn) = True
	  solutions = solutions + 1
      UpdateDisplay 
      If CInt(solutions) >= CInt(Request.Form ("NumSolutions")) Then 
		bln8Queens = True
        mblnBoard(intRow, intColumn) = False
        Exit Function 
      End If
      mblnBoard(intRow, intColumn) = False
      intRow = intRow + 1
    Else 
      If blnIsSafe(intRow, intColumn) Then 
        mblnBoard(intRow, intColumn) = True 
        bln8Queens = bln8Queens(intColumn + 1)
        mblnBoard(intRow, intColumn) = False
      End If 
      intRow = intRow + 1 
    End If
  Loop Until bln8Queens = True Or intRow = 8 
End Function
%>
<% End If %>
</BODY></HTML>
அசல் கருத்துகள் (3)
வேபேக் மெஷினிலிருந்து (Wayback Machine) மீட்டெடுக்கப்பட்டது