uva online judge self describing sequence problem solution python | uva 10049

The self describing sequence problem  (uva 10049) is also very straight forward and easy programming challenge in competitive programming once understood well. Which can be stated as (from online judge) Solomon Golomb’s self–describing sequence ⟨f(1),f(2),f(3),...⟩ is the only nondecreasing sequence of positive integers with the property that it contains exactly f(k) occurrences of k for each k. A few moments thought reveals that the sequence must begin as follows: n 1 2 3 4 5 6 7 8 9 10 11   12 f(n) 1 2 2 3 3 4 4 4 5 5 5 6 In this problem you are expected to write a program that calculates the value of f(n) given the value of n. Sample Input 100  9999  123456  1000000000  0 Sample Output 21  356  1684  438744 Programming Explanation This is very simple problem states that for given range of n as input (as shown in above image), we have

uva online judge minesweeper problem solution python | uva 10189

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

Popular posts from this blog

uva online judge vito's family problem solution python | uva 10041

uva online judge australian voting problem solution python | uva 10142

uva online judge common permutation problem solution python | uva 10252