Tuesday, October 17, 2017

UVa problem solution 10189 - Minesweeper

problem link:

Discuss: in this problem you just have to simply check the adjacent square.if there is mine then just count it.for simplicity you can use function.then in loop you can print the sum.no need to store it string. use size_t to avoid presentation error.

try your self before see the code



#include<bits/stdc++.h>
using namespace std;
 char str[101][101];
 int n,m;
 int check(int x,int y)
 {
     return (x>0&&x<=n&&y>0&&y<=m&&str[x][y]=='*');
 }
int main()
{
    int cnt=0;
    size_t T=1;
    while(cin >>n>>m&&n&&m)
    {
        cnt++;
        int i,j,flag=0,edge=0,sum;
        for(i=1;i<=n;i++)
        {
            scanf("\n");
            for(j=1;j<=m;j++)
            {
                cin >>str[i][j];
            }
        }
         if(T++>1)
        cout <<endl;
        printf("Field #%d:\n",cnt);
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                if(str[i][j]!='*')
                {
                sum=check(i+1,j)+check(i-1,j)+check(i,j+1)+check(i,j-1)+check(i-1,j-1)+check(i-1,j+1)+check(i+1,j-1)+check(i+1,j+1);
                cout <<sum;
                }
                else
                    cout <<'*';
            }
            cout <<endl;
        }
    }
    return 0;
}

No comments:

Post a Comment