![]() |
#2
wjshan08082014-06-23 17:46
是这样的吧?
![]() int[,] path ={ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /*(A)*/{ 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0}, { 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0}, { 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0}, { 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0}, { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, { 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1},//(B) { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}; List<string> arrayPath = new List<string>(30); //A to B for (int i = 1; i < path.GetLength(0) - 2; i++) { for (int j = 0; j < path.GetLength(1); j++) { if (path[i, j] == 1) { arrayPath.Add(i + "," + j); } } } foreach (string item in arrayPath) { Console.WriteLine(item); } arrayPath.Clear(); //B to A for (int i = path.GetLength(0) - 2; i > 1; i--) { for (int j = path.GetLength(1) - 1; j >= 0; j--) { if (path[i, j] == 1) { arrayPath.Add(i + "," + j); } } } foreach (string item in arrayPath) { Console.WriteLine(item); } |
int[,] path ={
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
(A){ 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0},
{ 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0},
{ 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0},
{ 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0},
{ 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0},
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1},(B)
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
以上是一个二维数组,用C#语言从path数组中按顺序得到元素值为1的各项下标,然后按顺序存到List中。说白了就是得到元素值为1的路径,有两条路径A到B,和B到A。得到两条路径的方法应该都一样吧。请写一下代码,谢谢!