- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在编写一个国际象棋引擎,我需要一种有效的方法来将多个列表合并为一个按最小值排序的单个列表。
每个列表本质上都是一组已经按照完美攻击顺序排列的棋子。例如,我在 a2 上有一个白皇后,在 b3 上有一个白主教,在 f1 上有一个白车,在 f2 上有一个白车。现在假设我在 f7 上有一个黑棋子,然后所有四个白棋子从两个不同的离散方向——东北(皇后和象)和北(车)汇聚在 f7 方格上。
这两个组的顺序如下:
A 组 1st - Bishop (b3);第二名 - 女王 (a2)
B组) 第一 - 新车 (f2);第二 - 车 (f1)
现在使用下面的积分系统,我希望两个列表按以下顺序(从最低值到最高值)合并为一个列表:Bishop (b3)、Rook (f2)、Rook (f1) 最后是 Queen (a2).
皇后 = 900 分
车 = 500 分
主教 = 375 分
骑士 = 350 点
兵 = 100 分
部分代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SO_22015528
{
public enum eRayDirection
{
North,
NorthEast,
East,
SouthEast,
South,
SouthWest,
West,
NorthWest
}
public enum ePieceType
{
Empty = 0,
Pawn = 1,
Knight = 2,
Bishop = 3,
Rook = 4,
Queen = 5,
King = 6
}
public struct BoardPosition : IComparable<BoardPosition>
{
public int File;
public int Rank;
public int Square;
public BoardPosition( int square )
{
Square = square;
File = square % 7;
Rank = 8 - ( square >> 3 );
}
public static Boolean operator >( BoardPosition b1, BoardPosition b2 )
{
return ( b1.Rank > b2.Rank ) || ( b1.Rank == b2.Rank && b1.File < b2.File );
}
public static Boolean operator <( BoardPosition b1, BoardPosition b2 )
{
return ( b1.Rank < b2.Rank ) || ( b1.Rank == b2.Rank && b1.File > b2.File );
}
public int CompareTo( BoardPosition obj )
{
if ( this < obj ) return 1;
else if ( this > obj ) return -1;
else return 0;
}
}
public class ChessPiece
{
public int Value { get; set; }
public ePieceType Type { get; set; }
public int Square { get; set; }
public BoardPosition XY
{
get
{
return new BoardPosition( this.Square );
}
}
public ChessPiece( ePieceType type, int value, int square )
{
Value = value;
Type = type;
Square = square;
}
}
public class Constraint
{
public ChessPiece Piece { get; set; }
public eRayDirection Ray { get; set; }
public Constraint( ChessPiece piece, eRayDirection ray )
{
Piece = piece;
Ray = ray;
}
public override string ToString()
{
return String.Format( "{0} {1} {2}", Piece.Square, Piece.Type, Ray );
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SO_22015528
{
class Program
{
static void Main( string[] args )
{
// test code
ChessPiece a2 = new ChessPiece( ePieceType.Queen, 900, 48 );
ChessPiece b3 = new ChessPiece( ePieceType.Bishop, 375, 41 );
ChessPiece f1 = new ChessPiece( ePieceType.Rook, 500, 61 );
ChessPiece f2 = new ChessPiece( ePieceType.Rook, 500, 53 );
// This just simulates pieces that attack on the f7 square.
List<Constraint> f7 = new List<Constraint>();
f7.Add( new Constraint( b3, eRayDirection.NorthEast ) );
f7.Add( new Constraint( a2, eRayDirection.NorthEast ) );
f7.Add( new Constraint( f1, eRayDirection.North ) );
f7.Add( new Constraint( f2, eRayDirection.North ) );
// Get all positive ray directions ( use to simplify LINQ orderby )
List<eRayDirection> positiveRayDirections = new List<eRayDirection>();
positiveRayDirections.Add( eRayDirection.North );
positiveRayDirections.Add( eRayDirection.NorthEast );
positiveRayDirections.Add( eRayDirection.NorthWest );
positiveRayDirections.Add( eRayDirection.West );
var groups = f7
.GroupBy( g => g.Ray )
.Select( a =>
new
{
Key = a.Key,
Results = positiveRayDirections.Contains( a.Key ) ? a.OrderBy( x => x.Piece.XY ).ToList() : a.OrderByDescending( x => x.Piece.XY ).ToList()
} ).ToList();
// The groups object returns two discrete groups here;
// NorthEast containing 2 entries (Bishop & Queen) and North
// also containing to entries (Rook x 2).
List<Constraint> attackOrder = new List<Constraint>();
List<Int32> groupIndicies = new List<Int32>( groups.Count() );
for ( int n = 0; n < groups.Count(); n++ )
groupIndicies.Add( 0 );
while ( true )
{
Int32 value = Int32.MaxValue;
Int32 groupIndex = -1;
for ( int n = 0; n < groups.Count(); n++ )
{
var g = groups[ n ];
int gIndex = groupIndicies[ n ];
if ( gIndex < g.Results.Count && g.Results[ gIndex ].Piece.Value < value )
{
value = g.Results[ gIndex ].Piece.Value;
groupIndex = n;
}
}
if ( groupIndex < 0 )
break;
attackOrder.Add( groups[ groupIndex ].Results[ groupIndicies[ groupIndex ] ] );
groupIndicies[ groupIndex ]++;
}
foreach ( var ao in attackOrder )
Console.WriteLine( ao.ToString() );
Console.ReadKey();
}
}
}
我认为最后一点效率不高,如果有人能找到更简单的方法,我将不胜感激。
最佳答案
使用快速排序对每个列表单独排序,然后通过插入到最终列表中进行排序。
Order the lists individually.
Create the empty Final list.
do
{
consider the first item in each of the sorted lists and find the highest ranking candidate.
remove the item from its sorted list and add it to the final list.
}
until all of the sorted lists are empty.
关于c# - 基于项目权重合并多个列表的高效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22015528/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!