Solve island hunting (equivalent to paiza rank S) in Python

at first

I was solving a collection of paiza level-up questions, but I didn't have a model answer, so I made it myself. The language is Python3.

problem

Paiza's skill check sample problem "Island search (equivalent to paiza rank S)" https://paiza.jp/works/mondai/skillcheck_sample/search-island?language_uid=python3 I couldn't see the problem statement without logging in. Registration is free and can be done immediately, so I recommend you to register for the time being.

Answer code

find_lands.py


col, row = map(int, input().split())
map_list = [[0]* (col+2)]
for _ in range(row):
    map_list.append([0] + list(map(int, input().split())) + [0])
map_list.append(map_list[0])

def check(x, y):
    lands = [[x,y]]
    
    while lands:
        x, y = lands.pop()
        map_list[y][x] = 0
        # down 
        if map_list[y+1][x] == 1:
            lands.append([x, y+1])
        # right
        if map_list[y][x+1] == 1:
            lands.append([x+1, y])
        # up
        if map_list[y-1][x] == 1:
            lands.append([x, y-1])
        # left
        if map_list[y][x-1] == 1:
            lands.append([x-1, y])

count = 0
for r in range(1, row+1):
    for c in range(1, col+1):
        if map_list[r][c] == 1:
            check(c, r)
            count += 1

print(count)

reference

https://maro28.com/paiza-s-rank-mod7

Summary

I wrote an article about Qiita at the beginning, but it should be simple and easy to use! I hope I can use it from now on.

Recommended Posts

Solve island hunting (equivalent to paiza rank S) in Python
Solve mod7 fortune-telling (equivalent to paiza rank S) in Python
Solve addition (equivalent to paiza rank D) in Python
Solve multiplication (equivalent to paiza rank D) in Python
Solve number sorting (equivalent to paiza rank D) in Python
Solve word counts (equivalent to paiza rank C) in Python
Solve character matches (equivalent to paiza rank D) in Python
Solve Fizz Buzz (equivalent to paiza rank C) in Python
Solve the smallest value in Python (equivalent to paiza rank D)
[With commentary] Solve Fizz Buzz (equivalent to paiza rank C) in Python
PUT gzip directly to S3 in Python
I wanted to solve ABC159 in Python
Solve ABC168D in Python
Solve ABC167-D in Python
Solve ABC146-C in Python
Solve ABC098-C in Python
Solve ABC159-D in Python
Solve ABC169 in Python
Solve ABC160-E in Python
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
To flush stdout in Python
Login to website in Python
Processing of python3 that seems to be usable in paiza
Solve ABC176 E in Python
Solve Wooldridge exercises in Python
Solve ABC175 D in Python
Speech to speech in python [text to speech]
13th Offline Real-time How to Solve Writing Problems in Python
Solve optimization problems in Python
How to develop in Python
Post to Slack in Python
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
The 17th Offline Real-time How to Solve Writing Problems in Python
How to write offline real time Solve E04 problems in Python
[Python] How to do PCA in Python
Convert markdown to PDF in Python
How to collect images in Python
How to use SQLite in Python
Solve Atcoder ABC169 A-D in Python
In the python command python points to python3.8
Try to calculate Trace in Python
Solve ABC036 A ~ C in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
6 ways to string objects in Python
How to use PubChem in Python
Solve ABC037 A ~ C in Python
Solve ordinary differential equations in Python
How to handle Japanese in Python
An alternative to `pause` in Python
I tried to solve AtCoder's depth-first search (DFS) in Python (result: TLE ...)
Upload what you got in request to S3 with AWS Lambda Python
[At Coder] What I did to reach the green rank in Python
I tried to implement PLSA in Python
Solve ABC175 A, B, C in Python
Try logging in to qiita with Python
How to access environment variables in Python
I tried to implement permutation in Python
Method to build Python environment in Xcode 6
How to dynamically define variables in Python