-1

我有一个我整天都在做的程序。该程序冻结更多次然后它不冻结....当它不冻结时它工作正常,看起来像这样...... http://picpaste.com/Multidimensional_Array-KfdKSmoE.bmp

当它在打开时挂断时,它看起来像这样...... http://picpaste.com/Multidimensional_Array_hung-Jcmz0rQP.bmp

// Chapter 8 Programming Project #9

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

#define SIZE 10
#define PATH_SIZE 25
#define ROW_SIZE ((int) (sizeof(board) / sizeof(board[0])))

int main(void)
{
    char board[SIZE][SIZE] = {};
    // 0 = Up, 1 = Down, 2 = Left, 3 = Right
    unsigned short i = 0, x, y;
    // Generate a random number
    srand((unsigned) time(NULL));
    int dir = rand() % 4;
    // Set all positions of board to '.'
    for (x = 0; x < ROW_SIZE; x++) {
        for (y = 0; y < ROW_SIZE; y++)
            board[x][y] = '.';
    }
    x = 0;
    y = 0;
    board[0][0] = 'A';
    // Generate the path
    while (i != PATH_SIZE) {
        for (i = 0; i < PATH_SIZE;) {
            // Check the direction and replace that char
            switch (dir) {
                case 0: if ((y - 1) >= 0 && (y - 1) < ROW_SIZE
                            && board[x][y - 1] == '.') {
                    board[x][--y] = i + 'B';
                    ++i;
                } break;
                case 1: if ((y + 1) >= 0 &&(y + 1) < ROW_SIZE
                            && board[x][y + 1] == '.') {
                    board[x][++y] = i + 'B';
                    ++i;
                } break;
                case 2: if ((x - 1) >= 0 && (x - 1) < ROW_SIZE
                            && board[x - 1][y] == '.') {
                    board[--x][y] = i + 'B';
                    ++i;
                } break;
                case 3: if ((x + 1) >= 0 && (x + 1) < ROW_SIZE
                            && board[x + 1][y] == '.') {
                    board[++x][y] = i + 'B';
                    ++i;
                } break;
            }
        // Reset the random direction
        dir = rand() % 4;
        }
    }
    // Print the board
    for (x = 0; x < ROW_SIZE; x++) {
        for (y = 0; y < ROW_SIZE; y++)
            printf("%c ", board[x][y]);
        printf("\n");
    }

    return 0;
}
4

2 回答 2

2

当没有可能的移动方向时,这将挂起。以 4 x 4 的板子为例。最初板子就像

....
....
....
....

初始位置标记

A...
....
....
....

我迈出第一步,说我向右移动

AB..
....
....
....

我采取下一步行动,说我向下移动

AB..
.C..
....
....

我采取下一步行动,说我向下移动

AB..
.C..
.D..
....

我进行下一步,说我向左移动

AB..
.C..
ED..
....

我采取下一步行动,说我向上移动

AB..
FC..
ED..
....

而且我被卡住了,我没有任何可能的移动位置。现在再添加一些内容可以解决您的问题。

  1. 添加一个方法来计算可能的移动方向列表
  2. 如果不能再移动,退出程序

暗示

void calc_poss_dirs()
{
        int dir;

        poss_dir.count = 0;

        for (dir = UP; dir <= RIGHT; dir++) {
                if (can_move(dir)) {
                        poss_dir.dirs[poss_dir.count] = dir;
                        poss_dir.count++;
                }
        }
}
int can_move(int dir)
{
        int x = cur_x;
        int y = cur_y;

        if (dir == UP)
                x--;
        else if (dir == DOWN)
                x++;
        else if (dir == LEFT)
                y--;
        else
                y++;

        if (!in_area(x, y))
                return 0;

        if (visited(x, y))
                return 0;

        return 1;
}
于 2014-03-11T09:37:22.023 回答
0

谢谢 Sakthi Kumar,您的意见非常宝贵,其他所有帮助我的人都对此表示赞赏,工作代码如下:)

    // Chapter 8 Programming Project #9

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

    #define SIZE 10
    #define PATH_SIZE 25
    #define ROW_SIZE ((int) (sizeof(board) / sizeof(board[0])))

    int main(void)
    {
        char board[SIZE][SIZE];
        // 0 = Up, 1 = Down, 2 = Left, 3 = Right
        unsigned short i = 0, x, y;
        // Generate a random number
        srand((unsigned) time(NULL));
        int dir = rand() % 4;
        // Set all positions of board to '.'
        for (x = 0; x < ROW_SIZE; x++) {
            for (y = 0; y < ROW_SIZE; y++)
                board[x][y] = '.';
        }
        x = 0;
        y = 0;
        board[0][0] = 'A';
        // Generate the path
        for (i = 0; i < PATH_SIZE;) {
                // Check that the last character has not been cornered
            if ((board[x][y - 1] != '.' || y - 1 < 0) &&
                (board[x][y + 1] != '.' || y + 1 > ROW_SIZE) &&
                (board[x - 1][y] != '.' || x - 1 < 0) &&
                (board[x + 1][y] != '.' || x + 1 > ROW_SIZE))
                break;
            // Check the direction and replace that char
            switch (dir) {
                case 0: if ((y - 1) >= 0
                            && board[x][y - 1] == '.') {
                            board[x][--y] = i + 'B';
                            ++i;
                        } break;
                case 1: if ((y + 1) < ROW_SIZE
                            && board[x][y + 1] == '.') {
                            board[x][++y] = i + 'B';
                            ++i;
                        } break;
                case 2: if ((x - 1) >= 0
                            && board[x - 1][y] == '.') {
                            board[--x][y] = i + 'B';
                            ++i;
                        } break;
                case 3: if ((x + 1) < ROW_SIZE
                            && board[x + 1][y] == '.') {
                            board[++x][y] = i + 'B';
                            ++i;
                        } break;
                default: if (board[x][y] == '.')
                             board[x][y] = i + 'B';
                             break;
            }
        // Reset the random directions
        dir = rand() % 4;
        }
        // Print the board
        for (x = 0; x < ROW_SIZE; x++) {
            for (y = 0; y < ROW_SIZE; y++)
                printf("%4c ", board[x][y]);
            printf("\n");
        }

        return 0;
    }
于 2014-03-13T06:11:56.930 回答