一个青蛙过河程序 总是调试错误 请牛人帮忙
题目:一条小溪上7块石头,如图所示:(图就是下面的链接)
http://album.hi.csdn.net/app_uploads/jinder22/20081220/235450716.p.jpg?d=20081220235510403
分别有六只青蛙:A,B,C,D,E,F。A,B,C三只蛙想去右岸,它们只会从左向右跳;D,E,F三只蛙想去左岸,它们只会从右向左跳。青蛙每次最多跳到自己前方第2块石头上。请问最少要跳几次所有青蛙上岸。写出步骤。 代码是
using System;
using System.Collections.Generic;
using System.Text;
class Program
{
public bool jump(int[] begintArray, int[] endArray, List<int> jumpStep)
{ if (compareArray(begintArray, endArray))
{
return true;
} int zeroIndex = Array.IndexOf(begintArray, 0);
int[] moveTo = zeroCanMoveTo(begintArray,zeroIndex); //穷举所有可以移动的位置
foreach (int moveStep in moveTo)
{
int[] newBegin = (int[])begintArray.Clone();
newBegin[zeroIndex] = newBegin[moveStep];
newBegin[moveStep] = 0;
if (jump(newBegin, endArray, jumpStep))
{
jumpStep.Add(moveStep);
return true;
}
} return false;
} //判断0可以移动到那些格
public int[] zeroCanMoveTo(int[] begintArray, int zeroIndex)
{
List<int> moveTo = new List<int>();
if (zeroIndex >= 1 && begintArray[zeroIndex - 1] == 1)
moveTo.Add(zeroIndex - 1);
if (zeroIndex >= 2 && begintArray[zeroIndex - 2] == 1)
moveTo.Add(zeroIndex - 2);
if (zeroIndex < begintArray.Length - 1 && begintArray[zeroIndex + 1] == 2)
moveTo.Add(zeroIndex + 1);
if (zeroIndex < begintArray.Length - 2 && begintArray[zeroIndex + 2] == 2)
moveTo.Add(zeroIndex + 2); return moveTo.ToArray();
} //比较两个数组是否相等
public bool compareArray(int[] begintArray, int[] endArray)
{
for (int i = 0; i < begintArray.Length; i++)
{
if (begintArray[i] != endArray[i])
{
return false;
}
}
return true;
}
static void Main(string[] args)
{
int[] begintArray = new int []{ 1, 1, 1, 0, 2, 2, 2 };
int[] endArray = new int[] { 2, 2, 2, 0, 1, 1, 1 }; //最后的解,最先移动的排在最后
List<int> jumpStep = new List<int>(); jump(begintArray, endArray, jumpStep); //反转一下得到正序的解 jumpStep.Reverse();
}
}
报错如下:
G:\csdev\k2\k2>csc class1.cs
Microsoft (R) Visual C# 2008 Compiler version 3.5.21022.8
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.class1.cs(74,13): error CS0120: An object reference is required for the
non-static field, method, or property 'Program.jump(int[], int[],
System.Collections.Generic.List<int>)'
class1.cs(9,22): (Location of symbol related to previous error)请问该怎么修改 正确代码怎么写 谢谢!
题目:一条小溪上7块石头,如图所示:(图就是下面的链接)
http://album.hi.csdn.net/app_uploads/jinder22/20081220/235450716.p.jpg?d=20081220235510403
分别有六只青蛙:A,B,C,D,E,F。A,B,C三只蛙想去右岸,它们只会从左向右跳;D,E,F三只蛙想去左岸,它们只会从右向左跳。青蛙每次最多跳到自己前方第2块石头上。请问最少要跳几次所有青蛙上岸。写出步骤。 代码是
using System;
using System.Collections.Generic;
using System.Text;
class Program
{
public bool jump(int[] begintArray, int[] endArray, List<int> jumpStep)
{ if (compareArray(begintArray, endArray))
{
return true;
} int zeroIndex = Array.IndexOf(begintArray, 0);
int[] moveTo = zeroCanMoveTo(begintArray,zeroIndex); //穷举所有可以移动的位置
foreach (int moveStep in moveTo)
{
int[] newBegin = (int[])begintArray.Clone();
newBegin[zeroIndex] = newBegin[moveStep];
newBegin[moveStep] = 0;
if (jump(newBegin, endArray, jumpStep))
{
jumpStep.Add(moveStep);
return true;
}
} return false;
} //判断0可以移动到那些格
public int[] zeroCanMoveTo(int[] begintArray, int zeroIndex)
{
List<int> moveTo = new List<int>();
if (zeroIndex >= 1 && begintArray[zeroIndex - 1] == 1)
moveTo.Add(zeroIndex - 1);
if (zeroIndex >= 2 && begintArray[zeroIndex - 2] == 1)
moveTo.Add(zeroIndex - 2);
if (zeroIndex < begintArray.Length - 1 && begintArray[zeroIndex + 1] == 2)
moveTo.Add(zeroIndex + 1);
if (zeroIndex < begintArray.Length - 2 && begintArray[zeroIndex + 2] == 2)
moveTo.Add(zeroIndex + 2); return moveTo.ToArray();
} //比较两个数组是否相等
public bool compareArray(int[] begintArray, int[] endArray)
{
for (int i = 0; i < begintArray.Length; i++)
{
if (begintArray[i] != endArray[i])
{
return false;
}
}
return true;
}
static void Main(string[] args)
{
int[] begintArray = new int []{ 1, 1, 1, 0, 2, 2, 2 };
int[] endArray = new int[] { 2, 2, 2, 0, 1, 1, 1 }; //最后的解,最先移动的排在最后
List<int> jumpStep = new List<int>(); jump(begintArray, endArray, jumpStep); //反转一下得到正序的解 jumpStep.Reverse();
}
}
报错如下:
G:\csdev\k2\k2>csc class1.cs
Microsoft (R) Visual C# 2008 Compiler version 3.5.21022.8
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.class1.cs(74,13): error CS0120: An object reference is required for the
non-static field, method, or property 'Program.jump(int[], int[],
System.Collections.Generic.List<int>)'
class1.cs(9,22): (Location of symbol related to previous error)请问该怎么修改 正确代码怎么写 谢谢!
{
...
}
其他地方也加。
{
int[] begintArray = new int []{ 1, 1, 1, 0, 2, 2, 2 };
int[] endArray = new int[] { 2, 2, 2, 0, 1, 1, 1 }; //最后的解,最先移动的排在最后
List<int> jumpStep = new List<int>(); jump(begintArray, endArray, jumpStep); //反转一下得到正序的解 jumpStep.Reverse();
}Main被声明成了静态的,因此不能调用非静态的,可以把其它的改为静态的或把Main改为非静态的。我原来写的是button_click,非静态的。
static void Main(string[] args)
{
int[] begintArray = new int []{ 1, 1, 1, 0, 2, 2, 2 };
int[] endArray = new int[] { 2, 2, 2, 0, 1, 1, 1 }; //最后的解,最先移动的排在最后
List <int> jumpStep = new List <int>(); jump(begintArray, endArray, jumpStep); //反转一下得到正序的解 jumpStep.Reverse();
foreach (int i in jumpStep)
{
Console.WriteLine(i);
}
}
得到1个解就返回了,如果要得到全部解(总共2个解),还需要修改。对于这道题,深度优先可能更好一些。
private void button1_Click(object sender, EventArgs e)
{
int[] begintArray = new int[] { 1, 1, 1, 0, 2, 2, 2 };
int[] endArray = new int[] { 2, 2, 2, 0, 1, 1, 1 }; //最后的解,最先移动的排在最后
List<string> jumpStep = new List<string>();
jump(begintArray, endArray, jumpStep); //反转一下得到正序的解
jumpStep.Reverse();
foreach (string step in jumpStep)
Console.WriteLine(step);
} private bool jump(int[] begintArray, int[] endArray, List<string> jumpStep)
{
if (compareArray(begintArray, endArray))
return true; int zeroIndex = Array.IndexOf(begintArray, 0);
int[] moveTo = zeroCanMoveTo(begintArray, zeroIndex); //穷举所有可以移动的位置
foreach (int moveStep in moveTo)
{
int[] newBegin = (int[])begintArray.Clone();
newBegin[zeroIndex] = newBegin[moveStep];
newBegin[moveStep] = 0;
if (jump(newBegin, endArray, jumpStep))
{
string steplog = "";
for (int i = 0; i < newBegin.Length; i++)
steplog += newBegin[i].ToString();
jumpStep.Add(steplog);
return true;
}
}
return false;
} //判断0可以移动到那些格
private int[] zeroCanMoveTo(int[] begintArray, int zeroIndex)
{
List<int> moveTo = new List<int>(); if (zeroIndex >= 1 && begintArray[zeroIndex - 1] == 1)
moveTo.Add(zeroIndex - 1);
if (zeroIndex >= 2 && begintArray[zeroIndex - 2] == 1)
moveTo.Add(zeroIndex - 2);
if (zeroIndex < begintArray.Length - 1 && begintArray[zeroIndex + 1] == 2)
moveTo.Add(zeroIndex + 1);
if (zeroIndex < begintArray.Length - 2 && begintArray[zeroIndex + 2] == 2)
moveTo.Add(zeroIndex + 2); return moveTo.ToArray();
} //比较两个数组是否相等
private bool compareArray(int[] begintArray, int[] endArray)
{
for (int i = 0; i < begintArray.Length; i++)
{
if (begintArray[i] != endArray[i])
return false;
}
return true;
}