多维数组随机悬挂

时间:2014-03-11 09:23:45

标签: c arrays multidimensional-array hung

我有一个我一直在工作的程序。该程序冻结更多次然后它不冻结....当它没有冻结它工作正常,看起来像这样...... 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;
}

2 个答案:

答案 0 :(得分:2)

当没有可能的移动方向时,这将挂起。以4乘4的方式举一个简单的电路板示例。最初,电路板就像

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

标记的初始位置

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

我做了第一步,说我向右移动

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

我做了下一步,说我向下移动

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

我做了下一步,说我向下移动

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

我做了下一步,说我向左移动

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

我做了下一步,说我向上移动

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

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

  1. 添加一种方法来计算可能的移动方向列表
  2. 如果无法再移动,请退出程序
  3. <强>提示

    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;
    }
    

答案 1 :(得分: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;
    }