UVA - 10189 (Minesweeper )/Direction Array problem

*We can sove it by using (Direction Array) algorithm. Direction Array is very useful to solve DFS or BFS problem and (Minesweeper)  is a very good problem for practicing Direction Array.
>Problem Link: UVA - 10189  




 * You can see shafayet's blog to know the how Directon Array works.
                                                        

Solution:        
                                  
#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll,ll> pii;
#define pi acos(-1.0)
char f[1000][1000];
bool v[1000][1000];
char DA[10000][10000];
ll fx[] = {1, 1, 0, -1, -1, -1, 0, 1};
ll fy[] = {0, 1, 1, 1, 0, -1, -1, -1};

int main()
{
    int R,C,t=0;
    while(cin>>R>>C)
    {
        memset(DA,0,sizeof(DA));
        if(R==0&&C==0)
            break;
        for(ll i=0; i<R; i++)
        {
            for(ll j=0; j<C; j++)
            {
                cin>>DA[i][j];
            }
        }

        ll x0,y0,cnt=0;
        for(ll i=0; i<R; i++)
        {
            for(ll j=0; j<C; j++)
            {
                if(DA[i][j]=='.')
                {
                    ll x=i;
                    ll y=j;

                    for(ll i=0; i<8; i++)
                    {
                        x0=x+fx[i];
                        y0=y+fy[i];
                        if(DA[x0][y0]=='*')
                        {
                            cnt++;
                        }
                    }
                    DA[i][j]=cnt+'0';
                    cnt=0;
                }
            }
        }
        if(t!=0)
            cout<<endl;

        cout<<"Field #"<<++t<<":"<<endl;

        for(ll i=0; i<R; i++)
        {
            for(ll j=0; j<C; j++)
            {
                cout<<DA[i][j];
            }
            cout<<endl;
        }
    }
}

                                                        
                      
    



                                              

Comments

Popular Posts