我正在用C#编写一个单词搜索拼图,我希望能够以优雅的方式搜索二维字符数组.
从左到右,从上到下等基本搜索并不难写,但是当在对角线上搜索时,事情开始变得有点冗长.我已经开始工作,但我确信那里有更好的解决方案.
这是我想要解决的一个难题的例子,任何想法将不胜感激.
BXXD
AXEX
TRXX
FXXX
BAT FRED
编辑:感谢史蒂夫给我一个搜索罗盘点的想法
编辑:搜索结果需要返回数组中单词的x1,y1和x2,y2坐标.
编辑:感谢Antti为搜索数组提供了一个很好的算法.
这是我想出的最终结果.我基于Antti的答案中的算法,修改它以返回任何单词的开头和结尾的数组偏移量.这个算法将用于我在WPF中为我的孩子写的Word Search游戏.感谢大家帮助我.当它受到尊重时,我会在这里发布一个链接到应用程序.
public class Range
{
public Range(Coordinate start, Coordinate end)
{
Start = start;
End = end;
}
public Coordinate Start { get; set; }
public Coordinate End { get; set; }
}
public class Coordinate
{
public Coordinate(int x, int y)
{
X = x;
Y = y;
}
public int X { get; set; }
public int Y { get; set; } …Run Code Online (Sandbox Code Playgroud) 我有一些代码用于在linq中强类型Includes(),就像这样......
public static ObjectQuery<T> Include<T>(this ObjectQuery<T> mainQuery, Expression<Func<T, object>> subSelector)
{
return mainQuery.Include(((subSelector.Body as MemberExpression).Member as System.Reflection.PropertyInfo).Name);
}
/// <summary>
/// Old way: (from dbUser in DataSource.DataContext.Users.Include("UserSubscriptions.ChurchSubscriptions") select dbUser);
/// New way: (from dbUser in DataSource.DataContext.Users.Include<Users, UserSubscriptions>(u => u.UserSubscriptions, s => s.ChurchSubscriptions) select dbUser);
/// </summary>
public static ObjectQuery<T> Include<T, Q>(this ObjectQuery<T> mainQuery, Expression<Func<T, object>> tSubSelector, Expression<Func<Q, object>> qSubSelector)
{
string tProperty = ((tSubSelector.Body as MemberExpression).Member as System.Reflection.PropertyInfo).Name;
string qProperty = ((qSubSelector.Body as MemberExpression).Member as System.Reflection.PropertyInfo).Name;
string path = string.Format("{0}.{1}", …Run Code Online (Sandbox Code Playgroud) 我目前正在开发一个自定义的HttpHandler(用于压缩/组合CSS,但这对于这个问题无关紧要).
我从一个简单的reusable = true同步HttpHandler开始,就像我们都知道的那样.
现在我正在尝试将其改进为异步处理程序(因为它使用IO功能并且它在非常繁忙的网站上使用).
我的第一次尝试(这似乎工作正常):
Action<HttpContext> asyncProcessRequest;
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
{
asyncProcessRequest = new Action<HttpContext>(ProcessRequest);
return asyncProcessRequest.BeginInvoke(context, cb, extraData);
}
public void EndProcessRequest(IAsyncResult result)
{
asyncProcessRequest.EndInvoke(result);
}
public virtual void ProcessRequest(HttpContext context)
{
// real work
}
Run Code Online (Sandbox Code Playgroud)
这是一个不可重用的httphandler(从我读到的,IsReusable应该是false,因为这个处理程序有状态(asyncProcessRequest字段).
现在我想让它重复使用.所以我的第一个想法是创建一个IAsyncResult/Action字典,如下所示:
IDictionary<IAsyncResult, Action<HttpContext>> asyncProcessRequests;
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
{
if (asyncProcessRequests == null)
{
asyncProcessRequests = new Dictionary<IAsyncResult, Action<HttpContext>>();
}
var request = new Action<HttpContext>(ProcessRequest);
var result = request.BeginInvoke(context, cb, extraData);
asyncProcessRequests.Add(result, …Run Code Online (Sandbox Code Playgroud) 我正在编写一个命令行程序,它将有一个状态栏,就像wget一样.
我面临的主要问题是:如何删除我已经发送到stdout/stderr的内容?
我有想法:使用退格字符'\ b'并删除我发送的输出.这是最好的方式吗?这是唯一的方法吗?有没有更好的办法?
PS:我不想使用像ncurses这样的东西.平原老C请.
谢谢
编辑:
我也可以往上走/走吗?示例:我有10行输出,我想将第3行更改Doing ABC为ABC: Done.我怎样才能做到这一点?
此外,任何人都可以发布有关VT102字符的更多详细信息吗?它的功能是什么?如果您有任何好的链接,请在此发布.
谢谢
我已经做了很长一段时间的Java并且大约6个月前开始使用Scala.我喜欢这门语言.我发现的一件事是有多种方法可以做.我不知道这是因为语言的性质还是因为它还很年轻而且不断发展,习惯用法和最佳实践还没有出现.
让我感到惊讶的是,我一直是perl人的蟒蛇,其中:
"应该有一个 - 最好只有一个 - 明显的做法."
对我来说比对我更有意义
"有不止一种方法可以做到这一点".
我很想知道你认为Scala适合这种规模的原因以及为什么?
如果我有以下内容:
typedef struct _MY_STRUCT
{
int a;
float b;
} MY_STRUCT, *PMYSTRUCT
Run Code Online (Sandbox Code Playgroud)
怎么*PMYSTRUCT办?它现在是我需要声明的指针类型还是只能指向_MY_STRUCT我可以使用的指针?
我知道这MY_STRUCT是一种需要按如下方式使用的新类型:
MY_STRUCT str;
str.a = 2;
Run Code Online (Sandbox Code Playgroud)
那怎么样*PMYSTRUCT?
我有很多关系:客户端和代理表(只是一个例子).显然,每个客户端可以有多个代理,每个代理可以有多个客户端.什么被认为是交叉表的正确命名约定....是ClientBroker ...?
我正在进行LEFT OUTER JOIN,但我只能在第一个表上应用Restrictions.有没有办法在第二张桌子上申请?
这是我的代码:
Criteria criteria = this.crudService
.initializeCriteria(Applicant.class).setFetchMode("products",
FetchMode.JOIN);.
Run Code Online (Sandbox Code Playgroud)
这工作(申请人有applicantName属性):
criteria.add(Restrictions.eq("applicantName", "Markos")
Run Code Online (Sandbox Code Playgroud)
这些都不起作用(产品具有productName属性)
criteria.add(Restrictions.eq("productName", "product1")
Run Code Online (Sandbox Code Playgroud)
criteria.add(Restrictions.eq("products.productName","product1")// products:属性的名称criteria.add(Restrictions.eq("Product.productName","product1")//产品:数据库表的名称
这是我收到的例外(如果我理解正确),申请人中不存在productName属性:
EJB Exception: ; nested exception is: org.hibernate.QueryException: could not resolve property: products.inventedName of: org.myCompany.applicant.entity.Applicant; nested exception is: org.hibernate.QueryException: could not resolve property: products.inventedName of: org.myCompany.applicant.entity.Applicant
Run Code Online (Sandbox Code Playgroud)
我尝试使用别名,但这会生成一个INNER JOIN,而不是我想要的LEFT OUTER JOIN.
如何对两个表格施加限制?
更新:
问题可能与此相同:https: //forum.hibernate.org/viewtopic.php?p = 2393694
我想定义一个简单的模板函数,它接受运行时值并确定它是否是某些可能值的成员.
用法:
int x; // <- pretend this came from elsewhere...
if (isoneof(x, {5,3,9,25}) ...
Run Code Online (Sandbox Code Playgroud)
就像是:
template <typename T, size_t size>
bool isoneof(T value, T (&arr)[size])
{
for (size_t i = 0; i < size; ++i)
if (value == arr[i])
return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
我认为这注定要失败,因为我没有看到如何创建内联静态数组.
我可以用:
int kPossibilities[] = {5,3,9,25};
if (isoneodf(6, kPossibilities)) ...
Run Code Online (Sandbox Code Playgroud)
稍微改变isoneof:
template <typename T1, typename T2, size_t size>
bool isoneof(T1 value, const T2 (&arr)[size])
{
for (size_t i = 0; i < size; …Run Code Online (Sandbox Code Playgroud) 我有一个模型,它有一个函数来计算两个字段之间的差异示例:
Class MyModel(models.Model):
fieldA = models.FloatField()
fieldB = models.FloatField()
def delta(self):
return self.fieldA - self.fieldB
Run Code Online (Sandbox Code Playgroud)
我想在 GenericView 中使用这个模型。我可以使用函数 delta 作为 extraContext 但我也喜欢在模板中包含所有 Delta 结果的总和,在这种情况下,我必须进行聚合,但再次因为 delta 既不是数据库字段也不是模型字段我不能使用它在一个聚合函数中。
怎样才能做到这一点?