984B - Minesweeper - CodeForces Solution


implementation *1100

Please click on ads to support us..

Python Code:

line = input().split()
n, m = int(line[0]), int(line[-1])
 
grid = []
 
for i in range(n):
    grid.append(input())
    
def k_neighbors(k, arr, x, y):
    total = 0
 
    for i in range(x - 1, x + 2):
        for j in range(y - 1, y + 2):
            if (0 <= i < len(arr)) and (0 <= j < len(arr[i])):
                if (not (i == x and j == y)) and arr[i][j] == '*':
                    total += 1
 
    return total == k
 
 
def minesweep(n, m, grid):
    for i in range(n):
        for j in range(m):
            if grid[i][j] != '*':
                num = int(grid[i][j]) if grid[i][j] != '.' else 0
                if not k_neighbors(num, grid, i, j):
                    return False
    return True
 
res = minesweep(n, m, grid)
 
if res:
    print("YES")
else:
    print("NO")

C++ Code:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n,m;
    cin>>n>>m;
    string a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    
    int x=-1;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(a[i][j]== '*')
                continue;
            else if(a[i][j] == '.' )
            {
                if(a[i][j+1]=='*' or a[i][j-1]=='*' or a[i-1][j]=='*' or a[i+1][j]=='*' or a[i-1][j-1]=='*' or a[i-1][j+1]=='*' or a[i+1][j+1]=='*' or a[i+1][j-1]=='*')
                {
                    cout<<"NO"<<endl;
                    x=1;
                    break;

                }
                else    
                    continue;
            }
            else
            {
                char k= a[i][j];
                int b=0;
                int p=(i==0)?i:i-1;
                while(p<=i+1)
                {
                    int q=(j==0)?j:j-1;
                    while(q<=j+1)
                    {
                        if(a[p][q]=='*')
                        {
                            b++;
                        }
                        q++;
                    }
                    p++;
                }
                if((char)(b+48)!=k)
                {
                    cout<<"NO"<<endl;
                    x=1;
                    break;
                }
                else
                    continue;
            }
        }
        if(x==1)
            break;
    }
    if(x==-1)
    cout<<"YES"<<endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

376A - Lever
1305A - Kuroni and the Gifts
1609A - Divide and Multiply
149B - Martian Clock
205A - Little Elephant and Rozdil
1609B - William the Vigilant
978B - File Name
1426B - Symmetric Matrix
732B - Cormen --- The Best Friend Of a Man
1369A - FashionabLee
1474B - Different Divisors
1632B - Roof Construction
388A - Fox and Box Accumulation
451A - Game With Sticks
768A - Oath of the Night's Watch
156C - Cipher
545D - Queue
459B - Pashmak and Flowers
1538A - Stone Game
1454C - Sequence Transformation
165B - Burning Midnight Oil
17A - Noldbach problem
1350A - Orac and Factors
1373A - Donut Shops
26A - Almost Prime
1656E - Equal Tree Sums
1656B - Subtract Operation
1656A - Good Pairs
1367A - Short Substrings
87A - Trains