C#中的填字游戏算法
我正在为我的单一任务做填字游戏.几乎完成了,但这是一个我无法解决的问题.
我们需要加载包含已完成的填字游戏的csv文件,但我们需要进行一些验证以确保填字游戏文件有效. 限制是: >言语无法重复. 我已经完成了上面的所有限制,但我仍然坚持到这里 >您只能拥有一组连接的单词,即一组连接的单词 填字游戏文件的一部分是这样的:(由于缺乏声誉,我无法上传图片) R O B E R T I J I L L E L J O H N S A S M A R Y R I A O R C R G A R Y ……. 到目前为止,我所做的与此有关: >名为Crozzle的类表示此填字游戏文件.其中一个属性是 这是我的解决方案: public bool ContainsOneGroup() { bool flag = true; // a temp crozzle word list List<WordInCrozzle> tempWords = _crozzle.CrozzleWords; // start from the first item in wordlist,whatever which word is using WordInCrozzle word = tempWords[0]; if (word.IntersectionPosition.Count > 0) { // step1. get a word randomly 'Word' OK -- WordInCrozzle word = tempWords[0]; // step2. get wordInCrozzle List OK -- List<WordInCrozzle> tempWords = _crozzle.CrozzleWords; // step3. find the intersection position(s) of the word 'Word' and store it to a temp list 'positionOfIntersection' OK -- List<int[]> positionOfIntersection = word.IntersectionPosition; List<int[]> positionOfIntersection = word.IntersectionPosition; // remove the first word tempWords.Remove(word); //crozzleBackup.CrozzleWords.Remove(word); // step4. if can grab an intersection position from 'positionOfIntersection' (means any) while (positionOfIntersection.Any()) { foreach (WordInCrozzle w in tempWords) { for (int i = 0; i < w.IntersectionPosition.Count; i++) { if (ArraysEqual(w.IntersectionPosition[i],positionOfIntersection[0])) { w.IntersectionPosition.Remove(positionOfIntersection[0]); positionOfIntersection.Remove(positionOfIntersection[0]); //tempWords.CrozzleWords[i].IntersectionPosition.Remove(w.IntersectionPosition[i]); if (w.IntersectionPosition.Count > 0) { // store the positionOfIntersections,if this is null,and still have word in tempWords,means there are more than one group of words positionOfIntersection.AddRange(w.IntersectionPosition); } // after get the position,remove the word tempWords.Remove(w); } } } } // step9. if there is no more intersection position left,and no word in wordInCrozzle List,means only one group in the crozzle // Otherwise,more than one group of word if (tempWords.Any()) { _errors.Add(new Error(ErrorType.CrozzleError,"More than one group of connected words found")); flag = false; } } else { _errors.Add(new Error(ErrorType.CrozzleError,"More than one group of connected words found")); // if there is no intersection in a word,means there must more than one group of words flag = false; } return flag; } 但是当我运行它时,我有一个’System.InvalidOperationException’,它告诉我当我正在做foreach时我无法修改tempWords. 谁能告诉我怎么做?或者是否有任何算法可以找出填字游戏文件中只有一组单词? 解决方法
枚举时不允许从数组中删除元素.我通常做的只是制作数组的副本并枚举一下:
foreach (WordInCrozzle w in tempWords.ToArray()) { ... tempWords.Remove(w); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- objective-c – Grand Central Dispatch(GCD)Key-Value Obs
- 【AJAX】学习总结
- 正则表达式30分钟入门教程
- 如何在PostgreSQL中修复双重编码?
- ruby-on-rails – Puma输出像铁杆一样薄
- ruby-on-rails – acceptable_nested_attributes_for忽略空
- vb.net 教程 3-7 窗体编程 菜单和工具栏 2 ContextMenuStri
- Oracle GoldenGate 系列:如何计算 Extract 或 Replicat 进
- ruby-on-rails-3 – Rails 3:访问??twitter bootstrap变量
- applicationContext.xml 配置说明