随机漫步10x10?

时间:2013-12-22 15:27:36

标签: c arrays

这是我试图在C中解决的问题: http://i.stack.imgur.com/X70nX.png

我写了这段代码,但我不知道出了什么问题。它没有给我任何输出,只是一个空屏幕!1

这是我的代码:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#define ROW 10 
#define COL 10

#define RIGHT 0
#define UP  1
#define LEFT 2 
#define DOWN 3

int main(void)
{
    char mat[ROW][COL];
    bool try;
    int move, co, ro,letter;

//Filling the grid with "."
    for (ro = 0; ro < ROW; ro++)
    {
        for (co = 0; co < COL; co++)
            mat[ro][co] = '.';
    }

//Initial Values
    co = 0; ro = 0; mat[0][0] = 'A';


    srand((unsigned)time(NULL));

    for (letter = 1; letter < 26; letter++)
    {
        try = true;

        while (try)
        {
            move = rand() % 4;

            if ((move == RIGHT) && (co + 1 < COL) && (mat[ro][co+1]=='.'))
            {
                mat[ro][co + 1] = mat[ro][co] + 1;
                co++; try=false;
            }

            if ((move == UP) && (ro - 1 >= 0) && (mat[ro-1][co]=='.') )
            {
                mat[ro - 1][co] = mat[ro][co] + 1;
                ro--; try = false;
            }

            if ((move == LEFT) && (co - 1 >= 0) && (mat[ro][co-1]=='.'))
            {
                mat[ro][co - 1] = mat[ro][co] + 1;
                co--; try = false;
            }

            if ((move == DOWN) && (ro + 1 < ROW) && (mat[ro+1][co]=='.'))
            {
                mat[ro + 1][co] = mat[ro][co] + 1;
                ro++;
            }
        }
    }


//Printing The GRID
    for (ro = 0; ro < ROW; ro++)
    {
        for (co = 0; co < COL; co++)
            printf(" %c", mat[ro][co]);
        printf("\n");
    }

    return 0;
}

现在有关于同一问题的类似讨论:Random walk on 10x10 Array

但我仍然不知道我的代码有什么问题!! ...请认为我是初学者。

修改: 考虑到这些意见后,这是新的一个:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#define ROW 10 
#define COL 10

#define RIGHT 0
#define UP  1
#define LEFT 2 
#define DOWN 3

int main(void)
{
    char mat[ROW][COL];
    bool try;
    int move, co, ro,letter,blocked;

//Filling the grid with "."
    for (ro = 0; ro < ROW; ro++)
    {
        for (co = 0; co < COL; co++)
            mat[ro][co] = '.';
    }

//Initial Values
    co = 0; ro = 0; mat[0][0] = 'A';


    srand((unsigned)time(NULL));

    for (letter = 1; letter < 26; letter++)
    {
        try = true;
        blocked = 0;
        while (try)
        {
            move = rand() % 4;
            switch (move)
            {

            case RIGHT:
            {   if ((co + 1 < COL) && (mat[ro][co + 1] == '.'))
            {
                mat[ro][co + 1] = mat[ro][co] + 1;
                co++; try = false; break;
            }
            else { blocked++; break; }}

            case UP:
            {   if ((move == UP) && (ro - 1 >= 0) && (mat[ro - 1][co] == '.'))
            {
                mat[ro - 1][co] = mat[ro][co] + 1;
                ro--; try = false; break;
            }
            else { blocked++; break; }}

            case LEFT:
            {   if ((move == LEFT) && (co - 1 >= 0) && (mat[ro][co - 1] == '.'))
            {
                mat[ro][co - 1] = mat[ro][co] + 1;
                co--; try = false; break;
            }
            else { blocked++; break; }}

            case DOWN:
            {if ((move == DOWN) && (ro + 1 < ROW) && (mat[ro + 1][co] == '.'))
            {
                mat[ro + 1][co] = mat[ro][co] + 1;
                ro++; try = false; break;
            }
            else { blocked++; break; }}
            }
            if (blocked == 4)
            {try = false; letter=26;}
        }  

    }


//Printing The GRID
    for (ro = 0; ro < ROW; ro++)
    {
        for (co = 0; co < COL; co++)
            printf(" %c", mat[ro][co]);
        printf("\n");
    }

    return 0;
}

代码有时只工作,但它不会在'Z'之后停止(它应该是字母= 26),

1 个答案:

答案 0 :(得分:6)

代码有时。我认为你经常进入一个无限循环,因为你产生一个无法继续的随机游走。像这样:

A B C .
H I D .
G F E .
. . . .