UVA Minesweeper在C

时间:2015-09-24 21:13:22

标签: c minesweeper

我一直致力于UVA挑战10189:Minesweeper,虽然我的代码完全适用于所提出的测试用例(以及我提出的其他测试用例),但我仍然会得到错误的答案作为判决。我在这里找到了很多帖子来调试这个问题的代码,但是没有一个适合我的情况。

如果你们中的任何人能帮我看看我的代码有什么问题,我将不胜感激。

问题:

  

你玩过扫雷吗?这是一个可爱的小游戏,属于某个特定的操作   我们无法记住的名称系统。那么,游戏的目标是找到所有的地方   M×N场内的地雷。为了帮助你,游戏在一个方块中显示一个数字,告诉你如何   那个广场附近有许多地雷。例如,将以下4×4字段用2表示   地雷(由'*'字符表示):

*...
....
.*..
....
  

如果我们代表放置上述提示号码的相同字段,我们最终会结束   用:

*100
2210
1*10
1110
  

您可能已经注意到,每个方格最多可能有8个相邻的方块。

输入

  

输入将包含任意数量的字段。每个字段的第一行包含两个整数   n和m(0

输出

  

对于每个字段,您必须单独打印以下消息:   字段#x:   其中x代表字段的编号(从1开始)。接下来的n行应包含   带有'。'字符的字段替换为该方块的相邻地雷数。必须有   字段输出之间的空行。

示例输入:

4 4
*...
....
.*..
....
3 5
**...
.....
.*...
0 0

示例输出:

Field #1:
*100
2210
1*10
1110

Field #2:
**100
33200
1*100

源代码:

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

//the two boundaries functions check whether 
//the corner indices of the "sub-grid" are within the boundaries of the field

int BoundariesTop(int i){   
    if (0 <= i-1)
        return (i-1);
    else 
        return 0;
}

int BoundariesBottom(int i, int n) {
    if (i+1 < n)
        return (i+1);   
    else 
        return (n-1);   
}

//CheckBomb is used to check every "sub-grid" (one cell + 8 adjacents)
//and count the number of bombs it contains.

void checkBomb(int spotI, int spotJ, int topI, int topJ, int bottomI, int bottomJ, int field[101][101]){
    int i, cnt = 0,j;

    for(i = topI; i <= bottomI; i++){
        for(j = topJ; j <= bottomJ; j++){
            if(field[i][j] == -1)
                cnt++;
        }
    }
    if(field[spotI][spotJ] != -1)
        field[spotI][spotJ]= cnt;
}

int main (void){

    int r, c, i, j, field[101][101], c1, z1, c2, z2, cnt = 0;
    char cell, temp;

    while (scanf("%d %d",&r,&c) && (r != 0 && c != 0)) {
        cnt++;
        //get the field
        //replace symbols with integer for computation purposes.
        for(i = 0; i<r; i++) {
            scanf("%c",&temp);
            for (j = 0; j<c; j++) {
                scanf("%c",&cell);
                if (cell == '*')
                    field[i][j] = -1;
                else if (cell == '.')
                    field[i][j] = 0;
            }
        }

        // Select and send sub-grid to be checked in function : checkBomb   
        for(i = 0; i<r; i++){
            for(j = 0; j<c; j++){
                //(c1,z1) are coordinates of top left of sub-grid
                c1 = BoundariesTop(i);
                z1 = BoundariesTop(j);
                //(c2,z2) are coordinates of bottom right of sub-grid
                c2 = BoundariesBottom(i,r);
                z2 = BoundariesBottom(j,c);
                checkBomb(i,j,c1,z1,c2,z2,field);
            }
        }

        // Print Result

        printf("Field #%d:",cnt);
        for(i = 0; i<r; i++) {
            printf("\n");
            for (j = 0; j<c; j++) {
                if (field[i][j] == -1)
                    printf("*");
                else
                    printf("%d",field[i][j]);
            }

        }
        printf("\n");
    }

    return 0;
}

0 个答案:

没有答案
相关问题