The minesweeper problem (uva 10189) is a loop programming problem states that (from onlinejudge)
The goal of the game minesweeper is to find where are all the mines within a m * n field. The game shows a number in a square which tells you how many mines there are adjacent to that square.
In this problem, given a line indicating '*' as bomb and '.' as empty box we have to calculate the number of bombs placed adjecent to each box and replace '.' with number of bombs.
In above game example, we have to find number of bombs in adjecent boxes marked by black lines. Once number of bombs founded, replace value of center box with number of bombs. That's it.
Sample Input
4 4
*...
....
.*..
....
3 5
**...
.....
.*...
0 0
Sample Output
Field #1:
*100
2210
1*10
1110
Field #2:
**100
33200
1*100
Explanation
As the problem states, we have to loop for every box over its adjecent 3 * 3 area boxes. For each itteration check value at given box, if it is '*' i.e. bomb then skip else calculate number of bombs adjecent to it.
Code
rows = int(input("Enter number of rows : "))
cols = int(input("Enter number of cols : "))
a = list()
print("Enter patter : ")
for i in range(0,rows):
a.append(list(input()))
for i in range(0,rows):
for j in range(0,cols):
count=0
if(a[i][j]=='*'):
continue
else:
for n in range(i-1,i+2):
for m in range(j-1,j+2):
if(n<0 or m<0 or n>=rows or m>=cols):
continue
else:
if(a[n][m]=='*'):
count+=1
a[i][j]=str(count)
for i in range(0,rows):
print(a[i][:])
Here, inside main loop(i and j) we itterate with n and m loops with condition i-1, i+2. This is because for every box its adjecent boxes number will remain same i.e 3 * 3. Now, inside if condition we check if n and m is invalid or valid (side boxes i.e for i=0, j=0 its adjecent boxes are only three, and so on). If box is not exist then skip else increase count by 1.
Related keywords: uva problem 10189 solution, uva minesweeper problem, minesweeper programming challenges problem, minesweeper online judge, uva 10189 problem explanation, uva source code, uva online judge minesweeper problem solution python.
Comments
Post a Comment