我正在尝试在python中实现N-Queens问题。我需要一些帮助来设计算法,以检查是否给出了Queen的位置,检查板上是否有其他任何女王在其对角线上。
我正在尝试设计一个函数diagonal_check(board, row, col)
,其中board是数组的N * N矩阵,其中' 1'表示女王的存在和' 0' 0代表缺席。
我会将函数的数组和位置(行,列)传递给函数。如果对角线上有任何其他女王,则我的函数必须返回false,否则返回true。
如果有人可以帮助我使用diagonal_check
函数的算法。不寻找任何特定的语言代码。
答案 0 :(得分:0)
让左上角为(0,0)
正方形(行,列)的右下方对角线为internal class SerializerSurrogate : IDataContractSurrogate
{
/// <summary>
/// Identifies a type transformation.
/// </summary>
/// <param name="type">The type being inspected.</param>
/// <returns>The type to transform to. If the same type is used, transform does not happen.</returns>
public Type GetDataContractType(Type type)
{
if (type == typeof(sourceType))
return typeof(targetType);
return type;
}
/// <summary>
/// During serialization, transfrom from sourceType to targetType.
/// </summary>
/// <param name="obj">The object instance.</param>
/// <param name="targetType">The source type.</param>
/// <returns>A new object instance based on targetType.</returns>
public object GetObjectToSerialize(object obj, Type targetType)
{
if (obj is sourceType)
{
// Create instance of targetType from the obj variable which is sourceType
// Set the properties of the targetType, based on the (sourceType)obj variable.
return new targetType();
}
return obj;
}
/// <summary>
/// During deserialization, transform from targetType to sourceType.
/// </summary>
/// <param name="obj">The object instance.</param>
/// <param name="targetType">The target type.</param>
/// <returns>A new object instance based on sourceType.</returns>
public object GetDeserializedObject(object obj, Type targetType)
{
if (obj is targetType)
{
// Create instance of sourceType from the obj variable which is targetType
// Set the properties of the sourceType, based on the (targetType)obj variable.
return new sourceType();
}
return obj;
}
public object GetCustomDataToExport(Type clrType, Type dataContractType)
{
throw new NotImplementedException();
}
public object GetCustomDataToExport(MemberInfo memberInfo, Type dataContractType)
{
throw new NotImplementedException();
}
public void GetKnownCustomDataTypes(Collection<Type> customDataTypes)
{
throw new NotImplementedException();
}
public Type GetReferencedTypeOnImport(string typeName, string typeNamespace, object customData)
{
return null;
}
public CodeTypeDeclaration ProcessImportedType(CodeTypeDeclaration typeDeclaration, CodeCompileUnit compileUnit)
{
return null;
}
}
正方形(行,列)的右上角度为col-row+7
只需检查2个皇后是否有相同的row+col
或col-row+7
应该告诉您2个皇后是否在同一对角线上。如果您仍然有点困惑,请在Google上查找棋盘图片。
答案 1 :(得分:0)
boolean diagonalCheck(board, row, col) {
int tempRow ;
int tempCol ;
//algorithm to check left diagonal
if (row >= col) {
tempRow = row-col;
tempCol = 0;
} else {
tempRow = 0;
tempCol = col-row;
}
while (tempRow != N-1 && tempCol != N-1) {
if (tempRow == row && tempCol ==col ){
//no need to check
} else if(queen(tempRow,tempCol) == 1 ) {
return true;
}
tempRow++;
tempCol++;
}
//algorithm to check right diagonal
if (row + col >= N-1) {
tempCol = N-1;
tempRow = (row + col) -(N-1)
} else {
tempRow = 0;
tempCol = row + col;
}
while (tempRow != N-1 && tempCol != 0) {
if (tempRow == row && tempCol ==col ) {
//no need to check
} else if(queen(tempRow,tempCol) == 1 ) {
return true;
}
tempRow++;
tempCol--;
}
return false;
}