私は、矩形配列を0と1から構成しています。私はそれらのインデックスで構成されるパスを取得する必要があります。出発点からお互いに接続できるものの数の点で最も長い最善の経路を得る必要があります。パスは、垂直、水平または斜め方向に互いに接続できるグループのグループで、これらのすべての方向を使用して移動できます。Cのセルの特定の値に矩形配列をソートする方法
答えて
単純な再帰アルゴリズムを使用して配列全体をトラバースすることができます。関数を呼び出すたびに、すでに訪れたセルのリスト(「パス」)と、入力したばかりのセルが表示されます。
現在のセルをリストに追加し、パスリストにない「1」のセルを探しています。
パスリストにない「1」セルがない場合は、パスリストを返します。
まだアクセスしていないセルが1つある場合は、これまでのパスを持つ各セルで再帰的に関数を使用し、戻り値のパスを長さと比較し、最も長いパスを返します。
コード例が追加さ:考える
using System;
using System.IO;
using System.Text;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using AUV_Topology;
using System.Collections.Generic;
using System.Media;
using System.Linq;
namespace AUVtopology
{
public partial class Form1 : Form
{
static int[,] array;
static List<int[]> path;
//This method is used to make sure the coordinate array
//is contained in the list. List.contains(new int[] {val1,val2}) was not enough.
static Boolean containsArray(List<int[]> list, int[] array)
{
if (array == null || array.Length == 0)
{
return false;
}
foreach (var listArray in list)
{
if (listArray != null && listArray.Length == array.Length)
{
for (int i = 0; i < listArray.Length; i++)
{
if (array[i] != listArray[i])
{
continue;
}
return true;
}
return false;
}
}
return false;
}
//This is the recursive method of the algorithm. It finds the
//maximum path of 1 cells in a matrix of 0/1 cells
static List<int[]> getMaxPath(int[,] array, List<int[]> maxPath, int rowIndex, int colIndex)
{
//End case in which we started (or ended up) in a 0 cell
if (array[rowIndex,colIndex] != 1) {
return maxPath;
}
//if we back-tracked and this cell was visited
if (containsArray(maxPath, new int[]{rowIndex,colIndex})) {
return maxPath;
}
//Add the current cell to the path.
maxPath.Add(new int[]{rowIndex,colIndex});
//Get the array limits.
int rowLength = array.GetLength(0);
int colLength = array.GetLength(1);
//If the path contains all the cells in the matrix, stop
if (maxPath.Count >= rowLength * colLength) {
return maxPath;
}
//remove one from lengths to make it the maximum index
colLength = colLength - 1;
rowLength = rowLength - 1;
//We'll use this variables to see which of the
//potential 7 paths is the longest.
List<int[]> futurePath;
//Go over all 8 possible adjoining cells:
//If we can go one down, one right
if (colIndex < colLength && rowIndex < rowLength) {
//We use maxPath first, since this is the first
//direction and by default is the longest
maxPath = getMaxPath (array, maxPath, rowIndex+1, colIndex+1);
}
//If we can go one down
if (colIndex < colLength) {
//We use futurePath now, since this is a second
//direction and a potential contender
futurePath = getMaxPath (array, maxPath, rowIndex, colIndex+1);
//We only need the maximum path.
if (futurePath.Count > maxPath.Count) {
maxPath = futurePath;
}
}
//If we can go one down and one left
if (rowIndex>0 && colIndex < colLength) {
futurePath = getMaxPath (array, maxPath, rowIndex-1, colIndex+1);
if (futurePath.Count > maxPath.Count) {
maxPath = futurePath;
}
}
//If we can go one left
if (rowIndex>0) {
futurePath = getMaxPath (array, maxPath, rowIndex-1, colIndex);
if (futurePath.Count > maxPath.Count) {
maxPath = futurePath;
}
}
//If we can go one left and one up
if (rowIndex>0 && colIndex>0) {
futurePath = getMaxPath (array, maxPath, rowIndex-1, colIndex-1);
if (futurePath.Count > maxPath.Count) {
maxPath = futurePath;
}
}
//If we can go one up
if (colIndex>0) {
futurePath = getMaxPath (array, maxPath, rowIndex, colIndex-1);
if (futurePath.Count > maxPath.Count) {
maxPath = futurePath;
}
}
//If we can go one up and one right
if (colIndex>0 && rowIndex < rowLength) {
futurePath = getMaxPath (array, maxPath, rowIndex+1, colIndex-1);
if (futurePath.Count > maxPath.Count) {
maxPath = futurePath;
}
}
//If we can go one right
if (rowIndex < rowLength) {
futurePath = getMaxPath (array, maxPath, rowIndex+1, colIndex);
if (futurePath.Count > maxPath.Count) {
maxPath = futurePath;
}
}
//We return the max path. Note: If none of the directions around
//us was applicable, we simply return the path we started
//with with our cell included.
return maxPath;
}
コメントは議論の延長ではありません。この会話は[チャットに移動]されています(http://chat.stackoverflow.com/rooms/148128/discussion-on-answer-by-assafs-how-to-analyze-a- rectangular-array-and-obtain- )。 –
私はもうここではできません、申し訳ありません。コードを実行してデバッグするのに役立つC#プログラマーを募集することをお勧めします。ブラインドを3日間飛行してさまざまなアプローチを試してみたら、コードを実行してリアルタイムでデバッグすることはできません。頑張って。 – Assafs
あなたは関数に入り口でそれをチェックしてください。 静的リスト
- 1. C#矩形アレイがソート
- 2. バイト配列をC#で特定の配列値で分割する方法は?
- 3. 配列を含むセル配列をソートする方法は?
- 4. 値の配列の配列をソートする方法は?
- 5. PHP配列の特定のキーの特定の値にアクセスする方法
- 6. Svg特定の矩形にズーム
- 7. listview C#特定の列でソートする
- 8. セル配列をソートする?
- 9. openCVで不完全な矩形を特定する方法
- 10. 配列を特定の値に分割する方法
- 11. 配列の配列の要素に特定の値を入れる方法
- 12. HowTo:特定の列に特定の値があるセル配列のすべての行を選択します。
- 13. UIView内のテーブルにグラフィック矩形を配置する方法
- 14. jqueryデータテーブルの特定の列にソートを適用する方法
- 15. 2D配列の特定の値をチェックする方法
- 16. 特定のインデックスでnumpyの配列値をコピーする方法
- 17. C#WPF Datagrid特定の列の選択セルを無効にする方法
- 18. 複数の要素の境界矩形を特定する方法は?
- 19. 配列からの矩形の交差
- 20. C++のパフォーマンス:特定のセルに特定の値を持つメモリブロックをチェックする
- 21. 配列内の日付を特定の形式に変換する方法
- 22. C#の塗りつぶし矩形配列の拡張メソッド
- 23. 矩形の検索矩形の配列からのポイントを含む
- 24. 配列内の次の値に循環する方法特定の値のみ
- 25. 配列で特定の値を取得する方法PHP
- 26. 配列から特定の値を出力する方法は?
- 27. 配列内の特定の値に値を追加する方法
- 28. matlabの3D矩形内の特定の点に値を割り当てる方法
- 29. データベースの特定のセルの値を保存する方法
- 30. シーングラフの矩形ノードに角を丸く設定する方法
は、他の多くの選択肢があります - どのように人間としてあなたが他のものより1つを選ぶのですか? – BugFinder
そのパスを "ベスト"パスにするには?あなたは "最良の"パスが最も長い "1"の値のチェーンを持っていると言っているようですが、繰り返しセルなしで描画したパスに '1'セルを追加できるように見えます。あなたの「最良の」道を理解していれば、あなたが描いた道はそれではありません。問題に迷路解決アルゴリズムを適用できますか?バックトラッキングアルゴリズムを試しましたか?ソリューションが処理できるアレイの大きさはどれくらいですか?あなたの質問は非常に曖昧です。 –
@Isra 'しかしあなたは質問に答えています。あなたが人間の言葉で私達に説明することができない場合、それは何が間違っている..あなたは英語で説明することはできませんどのように別の言語に翻訳することを期待していますか? – BugFinder