0

到目前为止,我得到的是一个 .in 文件,它将创建 100 个数组,然后是板上有多少个“地雷”,然后是每个“地雷”的 2 个数字,表示它们将放置在数组上的位置。这是为我的初学者 C 课准备的,老实说,我们没有被正确地教过这种高级的东西(我说高级使用这个词)。我知道如何读取文件,也知道如何创建数组,但我不确定如何读取那么多行,一遍又一遍地从地雷切换到放置。我还发现自己对如何根据地雷的放置位置将数组编号从 0 更改为另一个编号感到困惑。

示例输入文件:

1
4
1 3
7 5
7 3
3 3

1第一行中的 表示我们有一个板。下一行的4表示它将有 4 个炸弹。以下 4 行将炸弹在阵列中的位置描述为row column

有没有人可以为我指出正确的方向?

4

1 回答 1

0

下面是一个部分解决方案,它留下了一些部分作为 OP 的练习。

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

#define BOARD_SIZE 8

int main(void) {

    FILE *fp;

    fp = fopen("mines.in","r");
    if ( fp == NULL ) {
            fprintf(stderr,"Could not open file\n");
            return 1;
    }

    int nBoards = 0;
    int nMines  = 0;
    int col;
    int row;

    int currentBoard = 0;

    /* We know the first row is going to be the number of boards */
    fscanf(fp,"%d",&nBoards);

    printf("We have %d boards\n",nBoards);

    while ( fscanf(fp,"%d",&nMines) > 0 ) {
            int i,j;
            /* initialize board as all zeros */
            int board[BOARD_SIZE][BOARD_SIZE] = { {0} };

            currentBoard++;
            printf("Board %d:\n",currentBoard);

            /* Read in and set the mines */
            for (i=0; i<nMines; i++) {
                    fscanf(fp,"%d %d",&col,&row);
                    board[col-1][row-1] = 9;
            }

            /* Add mine proximity */
            for (i=0; i<BOARD_SIZE; i++) {
                    for (j=0; j<BOARD_SIZE; j++) {
                            if ( board[i][j] == 9 ) { /* we have a mine */
                                    /* Square to the left */
                                    if (j > 0 && board[i][j-1] != 9) {
                                            board[i][j-1]++;
                                    }
                                    /* Square to the right */
                                    /* Left as exercise for the OP*/

                                    /* Square above */
                                    /* Left as exercise for the OP*/

                                    /* Square below */
                                    /* Left as exercise for the OP*/
                    }
            }

            /* Print out the board */
            for (i=0; i<BOARD_SIZE; i++) {
                    for (j=0; j<BOARD_SIZE; j++) {
                            printf("%d ",board[i][j]);
                    }
                    printf("\n");
            }
            printf("\n");
    }
    fclose(fp);

    if (currentBoard != nBoards) {
            fprintf(stderr,"Expected %d boards, read in %d  boards\n",nBoards,currentBoard);
            return 1;
    }

    return 0;
}

代码读取第一行以记录板的数量,然后循环遍历包含地雷数量和地雷位置的数据块。while循环将在包含地雷数量的行上执行,fscanf并且在 while 循环的主体中,不同的地雷位置将被读入为棋盘定义的数字。

一旦我们有了所有的地雷位置,我们就可以计算板上其他方格中的数字,我在代码中只显示了其中一个(其他方格类似)。

请注意,上面的代码几乎没有错误处理,也几乎没有对输入文件进行验证——如果输入文件错误,您可能会收到错误,即对数组的“超出范围”访问。我省略了这些检查以使程序的底层逻辑更清晰。

另请注意,我假设输入索引是“1”索引(即在 [1,8] 范围内,而不是 C 期望的“0”索引(即在 [0,7] 范围内),因此行中 1 的替换board[col-1][row-1] = 9;

于 2012-11-06T11:27:02.947 回答