UVA online 10189_Minesweeper

coding cpp uvaonline

Since I am not going to have anymore time soon, I decided to try one more problem just to check that I still have my skills intact for school start. I tried this minesweeper problem. I passed through the sample test case the realized that I had made a terrible mistake, which took me 3 wrong submission and 9 unique test case to realize. But eventually I managed to solve it.

#include <cstdio>

int main(){
    int f=1;
    for(char line[1024]; fgets(line, 1024, stdin); ){
        int n, m; sscanf(line, "%d %d", &n, &m);
        if(!n && !m) break;

        char g[n][m];
        for(int i=0; i<n; i++) fgets(g[i], m+10, stdin);

        if(f>1) printf("\n");
        printf("Field #%d:\n", f++);
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                if(g[i][j] == '.'){
                    int s=0;
                    if(i-1>=0){
                        if(j-1>=0 && g[i-1][j-1]=='*') s++;
                        if(g[i-1][j]=='*') s++;
                        if(j+1<m && g[i-1][j+1]=='*') s++;
                    }
                    if(1){
                        if(j-1>=0 && g[i][j-1]=='*') s++;
                        if(j+1<m && g[i][j+1]=='*') s++;
                    }
                    if(i+1<n){
                        if(j-1>=0 && g[i+1][j-1]=='*') s++;
                        if(g[i+1][j]=='*') s++;
                        if(j+1<m && g[i+1][j+1]=='*') s++;
                    }
                    printf("%d", s);
                }else{ printf("*"); }
            }
            printf("\n");
        }
    }
    return 0;
}