随机游走算法的条件
我是编程的初学者,我已经被分配了一个小型项目。教练对我应该做什么非常不清楚。我所知道的是这是一个“随机游走”,我不应该使用一种方法,我只需要使用循环,分支和随机数生成器(甚至不知道这个)。它也应该从用户那里收到一些步骤,并根据给定的数字进行步行。无论如何,我是一个初学者,对C#的知识非常有限。我在这里和其他许多地方进行了很多研究,但都没有成功。代码要么非常复杂,要么用其他语言。 我写了一些代码。我不知道这是否正确。 请给我一些指点。 有没有错误,但程序不起作用,有条件的问题。随机游走算法的条件
static void Main(string[] args)
{
int row = 100;
int col = 100;
int[,] matrix;
matrix = new int[row, col];
int n;
Console.WriteLine("Enter the number of steps:");
n = int.Parse(Console.ReadLine());
const int up = 1;
const int down = 2;
const int left = 3;
const int right = 4;
int number=0;
for (int i=0;i<100;i++)
{
for (int j = 0; j < 100; j++)
matrix[i, j] = number;
}
Random randomdirection = new Random();
for (int counter = 0; counter < n; counter++)
{
int direction = randomdirection.Next(1, 5);
while (direction == 1)
{
if ((++col) < 100 && matrix[row, ++col] == 0)
{
matrix[row, ++col] = ++number;
}
else
break;
}
while (direction == 2)
{
if ((--col) < 100 && matrix[row, --col] == 0)
{
matrix[row, --col] = ++number;
}
else
break;
}
while (direction == 3)
{
if ((--row) < 100 && matrix[--row, col] == 0)
{
matrix[--row, col] = ++number;
}
else
break;
}
while (direction == 4)
{
if ((++row) < 100 && matrix[++row,col] == 0)
{
matrix[++row,col] = ++number;
}
else
break;
}
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
Console.WriteLine(matrix[i, j]);
}
Console.ReadKey();
}
这是分配: 酒鬼开始行走漫无目的,起始于灯柱。在每一个时间步,酒鬼都会忘记他或她在哪里,并随机一步,无论是北部,东部,南部还是西部,概率为25%。 N级后,酒鬼离开灯柱多远? 这是非常不完整的。 我们没有教过类和方法,所以我们不应该使用它们。我认为这一点是使用数组和循环和分支。 我自己写了代码。这就是为什么它很混乱。
改成了这样: 好像我没有看到这个问题:((
static void Main(string[] args)
{
Random randomObject = new Random();
int randomDirection;
int[,] mainarray;
int row=10;
int col=10;
mainarray=new int[row,col];
int number=0;
for (int b=0;b<row;b++)
{
for (int c=0;c<col;c++)
mainarray[b,c]=number;
}
while (true)
{
int n;
Console.WriteLine("Enter the number of steps: ");
n = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < n; i++)
{
randomDirection = randomObject.Next(1, 5);
if (randomDirection == 1 && row < 0 && row < 10)
mainarray[++row, col] = ++number;
if (randomDirection == 2 && row < 0 && row < 10)
mainarray[--row, col] = ++number;
if (randomDirection == 3 && col < 0 && col < 10)
mainarray[row, ++col] = ++number;
if (randomDirection == 4 && col < 0 && col < 10)
mainarray[row, --col] = ++number;
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
Console.Write(mainarray[i,j]);
}
}
Console.ReadKey();
}
我认为你的方法作为一个整体是错误的。
假设你有一个100×100的网格。并且你想走几步的随机方向。你会想是这样的:
如果你走在墙上做randomDirection==1 && row<100
像你说的,你可以检查:你的问题的评论和编辑后
static void Main(string[] args)
{
Random randomObject = new Random();
int randomDirection;
while (true) { //because your program needs to run infinitely
Console.WriteLine("Enter the number of steps:");
n = int.Parse(Console.ReadLine());
//You walk a random way each step
for (int i = 0; i < n; i++)
{
randomDirection = randomObject.Next(1,5);
if (randomDirection == 1)
{
//walk right
}
if (randomDirection == 2)
{
//walk left
}
if (randomDirection == 3)
{
//walk up
}
if (randomDirection == 4)
{
//walk down
}
//Also check if you aren't walking against walls!
}
}
}
编辑。你必须编写一些代码来采取另一个方向,因为如果你不能这样做,但i
会增加,这将是错误的。那么你不会动,但一步将被计算在内。
你可以像你说的那样检查你的代码行(和所有其他方向),以检查你是否移出界限。我会将所有if都更改为else if
声明。 else if (randomDirection == 4 && col < 100) {}
后,请写else { i--; }
。至于检查酒鬼走了多远,你可以使用毕达哥拉斯定理。
编辑2:您使用:randomDirection == 1 && row < 0 && row < 10
。你总是检查行是否低于0.它应该是row > 0
。
EDIT3:实际上,您对mainArray
的全部使用是没有意义的。为什么不把row
和col
设置为0,如果你向右走,做row++
?
static void Main(string[] args)
{
Random randomObject = new Random();
int randomDirection;
int row = 1;
int col = 1;
while (true)
{
int n;
row = 1;
col = 1;
Console.WriteLine("Enter the number of steps: ");
n = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < n; i++)
{
randomDirection = randomObject.Next(1, 4);
if (randomDirection == 1 && row > 0 && row <= 10)
row++;
else if (randomDirection == 2 && row > 0 && row <= 10)
row--;
else if (randomDirection == 3 && col > 0 && col <= 10)
col--;
else if (randomDirection == 4 && col > 0 && col <= 10)
col++;
else
i--;
}
Console.WriteLine("You have walked in row: " + row);
Console.WriteLine("You have walked in col: " + col);
Console.WriteLine("");
}
}
看这个片段:
while (direction == 1)
{
if ((++col) < 100 && matrix[row, ++col] == 0)
{
matrix[row, ++col] = ++number;
}
else
break;
}
它增加col
3倍所以会发生什么当col == 98
就在这个循环之前?
我们得到:
if ((99) < 100
&& matrix[row, 100] == 0) // index out of range
{
matrix[row, 101] = ++number; // index out of range
}
我能想到的最好的建议是:在没有++
操作符的情况下重写它。你会更清楚地看到问题。
好的,我做到了。现在它只是在一行中打印0:( – rapture 2013-05-13 15:14:43
你会很好地解释你想达到什么 – 2013-05-13 13:55:34
你从哪里得到这段代码?因为我100%肯定你没有写这段代码,所以你真的更好。多么混乱...... – Joetjah 2013-05-13 13:56:06
'我不应该使用一种方法'是你的解释还是要求教师? “条件有问题”什么是条件,以及你的“随机游走”逻辑如何流动? – Fendy 2013-05-13 13:56:11