[PYTHON] Programming Improvement Course 4: Pixel Logic

This is the 4th programming improvement course.

Using pixel logic as a subject Consider a program.

Click here for the explanation video

I'm sorry if it doesn't appear

What is Pixel Logic?

Do you know Pixel Logic?

It ’s a game that has been around for a long time. I think there are many people who have done it.

The tentative rule is A game in which the squares are painted black by the number of vertical and horizontal numbers.

It is painted continuously by the number Where the numbers are divided There are at least one white square.

If you can paint all the squares correctly The picture is completed.

For example, in a 5 x 5 square problem, the numbers in one row If it is 2 2, it will be like ■■ □ ■■.

If you paint the squares according to the numbers That's good.

Beginner: Create a check function for filled squares

Count how many squares are filled in one line Let's create a function check_row that outputs a number.

If white squares come in between like illustration logic, divide the numbers.

The output is a list type of numbers

check_row(Row data):
return List of numbers

Example:

check_row('□■□■□■□')

1 1 1

Intermediate:

Consider a program that creates an illustration logic problem from an image.

The input data of the image is a list type with black and white cells as character string elements. The input data is a square of N squares x N squares.

In the case of the following image data

data = [
'□□■■■■□□',
'□■■□□■■□',
'■■□□□□■■',
'■■■■■■■■',
'■■□□□□□□',
'■■□□□□■■',
'□■■□□■■□',
'□□■■■■□□']

N = 8

Output example is

pix.png

Answer

Beginner's answer

Count the number of black squares one by one from the beginning.

+1 if black squares continue When the white square comes, if the front is the black square, record the number If the last square is a black square, record the number.

□■■■■□□□ 4 □■■■■□□■ 4 1

#Check how many black squares there are
def check_row(row):
    res,checked,count = [],0,0
    while True:
        if row[checked]=='■':
            count+=1
        elif count!=0:
            res.append(count)
            count=0
        checked+=1
        if checked==len(row):
            if count!=0:
                res.append(count)
            break
    return res
​
check_row('□□□■□■■■□□■■■■■■■□□□')

[1, 3, 7]

Intermediate answer

The result of counting the black squares is calculated separately for the row direction and the column direction.

When thinking about what to output Write the numbers in the column direction first, and write the numbers in the row direction later.

Output the concatenated one as a character string (tab delimited)

data = [
'□□■■■■□□',
'□■■□□■■□',
'■■□□□□■■',
'■■■■■■■■',
'■■□□□□□□',
'■■□□□□■■',
'□■■□□■■□',
'□□■■■■□□']

N = len(data)

#Check how many black squares there are
def check_row(row):
    res,checked,count = [],0,0
    while True:
        if row[checked]=='■':
            count+=1
        elif count!=0:
            res.append(count)
            count=0
        checked+=1
        if checked==len(row):
            if count!=0:
                res.append(count)
            break
    return res

#Store the result of the matrix
result1,result2 = [],[]

#row direction
for row in data:
    tmp = check_row(row)
    result1.append(tmp)

#Column direction
for x in range(N):
    col = []
    for y in range(N):
        col.append(data[y][x])
    tmp = check_row(col)
    result2.append(tmp)

#Create a function for output
def make_num_data(res1,res2,num):
    row_len = max([len(r) for r in res1])
    col_len = max([len(c) for c in res2])
    row_num = row_len + num
    res_data = []
    
    #Create output data in the column direction first
    for c in range(col_len):
        tmp = [' '] * row_len
        for x in range(num):
            if len(res2[x])-1>=c:
                tmp+=[str(res2[x][c])]
            else:
                tmp+= [' '] 
        res_data.append(tmp)

    #Connect data in the row direction
    for n in range(num):
        tmp = []
        for r in range(row_num):
            if len(res1[n])-1>=r:
                tmp+=[str(res1[n][r])]
            else:
                tmp+= [' ']
        res_data.append(tmp)
    return res_data

#Create data for output
d = make_num_data(result1,result2,N)

#output
for y in d:
    print('\t'.join(y))
pix.png               ## Advanced edition

Create a program that solves the nonogram.

I don't have time, so I'm sorry for the inconvenience.

If you are free, please try it.

Summary

Nonogram creates a problem Solving the problem is the real thrill of both.

You have to write an efficient search program to solve the problem Because it becomes impossible to solve when the squares become large I thought it would take some time to implement.

I want to challenge when I have time.

Well then.

Author information

Otsu py's HP: http://www.otupy.net/

Youtube: https://www.youtube.com/channel/UCaT7xpeq8n1G_HcJKKSOXMw

Twitter: https://twitter.com/otupython

Recommended Posts

Programming Improvement Course 4: Pixel Logic
Programming improvement course 1
Programming improvement course 2