在10×10阵列的随机游走 [英] Random walk on 10x10 Array

查看:187
本文介绍了在10×10阵列的随机游走的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有搞清楚这个问题的算法有问题,一直努力没有成功的几天,这里是什么我尝试获得图:

http://i.stack.imgur.com/X70nX.png

下面是我的code尝试了许多解决方案型动物,但总是会卡在相同点:(对不起,混合语言的重要组成部分,是英文)

PS
林不应该使用的功能来解决这个问题唯一的循环和数组。

修改
很多固定后,它的散步,但罕崩溃
任何想法?

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&time.h中GT;诠释主要(无效){炭箱[10] [10];
INT I,J;
INT动,行,列;
字符字母='A';
函数srand(时间(NULL));的printf(\\ n \\ T开始\\ n \\ n);对于(I = 0; I&小于10;我++)/ * righe * /
{
 为(J = 0; J&小于10; J ++)/ * Colonne的* /
 {
  盒[I] [J] =''; / * assegno。一个合奏GLI ELEMENTI戴尔数组* /
  如果(J == 9)
   的printf(%C%C \\ N,包装盒[I] [J]); / * giustifico ogni 10 ELEMENTI戴尔阵列Ĵ(0-9)* /
   其他
   的printf(%C%C中的[I] [J]);
  }
}
/* 开始吧 */的printf(\\ n \\ n Inizia IL gioco \\ n \\ n);/ *随机的地方开始* /行=兰特()%9;
COL =兰特()%9;
盒[行] [山口] ='A';
而(字母LT ='Z')
{
 如果(盒[排+ 1] [COL] ==''||盒[行1] [COL] ==''||盒[行] [COL + 1] ==''||盒[行] [COL-1] =='。')
 {
 此举=兰特()%4;
 开关(移动){
              案件0:/ *往上走* /
                    如果((行= 0)及!及(盒[行1] [COL] ==)'。')
                    {
                            盒[行1] [COL] = ++信;
                            盒[行 - ] [COL];
                    }其他{
                          此举=兰特()%4;
                          }
              案例1:/ *正在下降* /
                   如果((行= 9)及!及(盒[排+ 1] [COL] ==)'。')
                   {
                           盒[排+ 1] [COL] = ++信;
                           盒[行++] [COL];
                   }其他{
                         此举=兰特()%4;
                         }
              案例2:/ *外出时左* /
                   如果((COL = 0)及!&放大器;'。'(盒[行] [COL-1] ==))
                   {
                           盒[行] [COL-1] = ++信;
                           盒[行] [col--];
                   }其他{
                         此举=兰特()%4;
                         }
              案例3:/ *在朝好的方向* /
                   如果((COL = 9)及!及(盒[行] [COL + 1] ==)'。')
                   {
                           盒[行] [COL + 1] = ++信;
                           盒[行] [COL ++];
                   }其他{
                         此举=兰特()%4;
                         }
              }
 }其他{
        的printf(\\ n \\ nBloccato一个%C \\ n \\ n,字母);
        打破;
 }
}
 / * * FINE /对于(I = 0; I&小于10;我++)/ * righe * /
{
 为(J = 0; J&小于10; J ++)/ * Colonne的* /
 {
   如果(J == 9)
     的printf(%C%C \\ N,包装盒[I] [J]); / * giustifico ogni 10 ELEMENTI戴尔阵列Ĵ(0-9)* /
    其他
     的printf(%C%C中的[I] [J]);
 }
}
返回0;
}


解决方案

您需要更新山坳内循环。
否则,你将永远尝试从'A'的位置走。

...一旦所有的4个方向都填满,你被困在一个无限循环


。 。 。 。 。
。 。 B点。 。
。 êA C。
。 。 ð。 。


甚至当你更新山坳内环路(和纠正 = = 错误),你必须处理的一个问题:假设第一个点(在'A')是左上角的下一个随机方向是东,南,南,西,北。 ... 怎么办? :)


A B。
F c的。
E D。
。 。 。

i am having a problem figuring out an algorithm for this problem,been trying for few days without success,here is a pic of what im trying to obtain:

http://i.stack.imgur.com/X70nX.png

Here is my code tried many differents solutions but always get stuck at the same point:(Sorry for mixed language the important part is in english)

ps im not supposed to use functions to solve this problem only loops and array.

EDIT after much fixing it does the walk but seldomly crashes any idea?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void){

char box[10][10];
int i,j;
int move,row,col;
char letter='A';
srand(time(NULL)); 

printf("\n\tSTART\n\n");

for(i=0;i < 10 ;i++)/* righe */
{
 for(j=0;j < 10;j++) /* colonne */
 {
  box[i][j] = '.'; /* assegno . a tutti gli elementi dell array */
  if(j == 9)
   printf("%c%c\n", box[i][j]); /* giustifico ogni 10 elementi dell array j(0-9) */
   else 
   printf("%c%c", box[i][j]);
  }
}


/* LETS START */ 

printf("\n\n    Inizia il gioco\n\n");

/* random place to start */

row = rand() % 9;
col = rand() % 9;
box[row][col]= 'A';


while(letter <= 'Z')
{
 if(box[row+1][col] == '.' || box[row-1][col] == '.' || box[row][col+1] == '.' || box[row][col-1] == '.' )
 {
 move=rand() % 4;
 switch(move){
              case 0: /* Going UP */
                    if((row != 0) && (box[row-1][col] == '.'))
                    {
                            box[row-1][col]=++letter;
                            box[row--][col];
                    }else{
                          move=rand() % 4;
                          }
              case 1:/* Going Down */
                   if((row != 9) && (box[row+1][col] == '.'))
                   {
                           box[row+1][col]=++letter;
                           box[row++][col];
                   }else{
                         move=rand() % 4;
                         }
              case 2: /*Going Left */
                   if((col != 0) && (box[row][col-1] == '.'))
                   {
                           box[row][col-1]=++letter;
                           box[row][col--];
                   }else{
                         move=rand() % 4;
                         }
              case 3: /* Going Right */
                   if((col != 9) && (box[row][col+1] == '.') )
                   {
                           box[row][col+1]=++letter;
                           box[row][col++];
                   }else{
                         move=rand() % 4;
                         }
              }
 }else{
        printf("\n\nBloccato a %c\n\n", letter);
        break;
 }
}


 /* FINE */

for(i=0;i<10;i++)/* righe */
{
 for(j=0;j<10;j++) /* colonne */
 {
   if(j == 9)
     printf("%c%c\n", box[i][j]); /* giustifico ogni 10 elementi dell array j(0-9) */
    else 
     printf("%c%c", box[i][j]);
 }  
}
return 0;
}

解决方案

You need to update row and col inside the loop. Otherwise you'll always attempt to walk from the position of the 'A'.

... and once all 4 directions are filled, you're stuck in a infinite loop

. . . . .
. . B . .
. E A C .
. . D . .


Even when you update row and col inside the loop (and correct the == mistake), you have to handle a problem: suppose the first spot (the 'A') is the top left corner and the next random directions are East, South, South, West, and North. ... now what? :)

A B .
F C .
E D .
. . .

这篇关于在10×10阵列的随机游走的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆