偵測炸彈

 avatar
user_6817964
c_cpp
2 years ago
1.5 kB
3
Indexable
Never
#include <stdbool.h> // bool
#include <stdio.h> // printf
int main()
{
    int x[15][15];
    int R, C;
    scanf_s("%d%d", &R, &C);
    for (int i = 1; i <= R; i++) {
        for (int j = 1; j <= C; j++) {
            scanf_s("%d", &x[i][j]);
        }
    }
    int bump = 0, not_bump = 0;
    for (int i = 1; i <= R; i++) {
        for (int j = 1; j <= C; j++) {
            if (x[i][j] == 5) {
                bool annoy = false;
                /*偵測有沒有被干擾*/
                for (int k = i - 1; k <= i + 1; k++) {
                    for (int m = j - 1; m <= j + 1; m++) {
                        if (x[k][m] == 5 && (k != i || m != j)) {
                            annoy = true;
                        }
                    }
                }
                if (!annoy) {
                    /*偵測炸彈數*/
                    for (int k = i - 1; k <= i + 1; k++) {
                        for (int m = j - 1; m <= j + 1; m++) {
                            if (x[k][m] == 1) {
                                bump++;
                                x[k][m] = 0;
                            }
                        }
                    }
                }
            }
        }
    }
    /*不在偵測器範圍的炸彈*/
    for (int i = 1; i <= R; i++) {
        for (int j = 1; j <= C; j++){
            if (x[i][j] == 1)
                not_bump++;
    }
}
    printf("%d %d", bump, not_bump);

}