Share a pen question from a tall Internet company

Source: Internet
Author: User
Statement:

First, I declare that I did not take this test (I am old enough). I just heard the story and said whether this question exists? Who knows! I just want to share some knowledge with you.

Rough description of the original question:

Given a two-dimensional array, enter 0 and 1 randomly in it, and calculate the number of 0 surrounding 1 that is continuous (the diagonal line is not counted as a link) from top to bottom. Here we can regard the data composed of 1 as an island, and calculate the length of the island coastline, that is, the number of surrounding 0.

Introduction:

I have watched the man-machine game. I think it's time to get started with the idea of playing Ziqi games (2. In fact, go computing is to calculate the coastline of a solid island without eyes. In the case of an eye position, go computing is also used to calculate the length of the coastline inside the hollow Island and the length of the external coastline. What is the eye position of go, please Baidu, we will not continue to discuss the eye position of go.


Solution:

I don't know the detailed questions, so I don't know whether the internal coastline length of the hollow island needs special handling. However, it is not difficult to check whether there is an eye position when calculating the gas. Just add the mark. If it is indeed an island that contains hollow spaces, that is, the Go game that forms the eye position, we can perform special processing. We only need to deduct the total amount of gas from the internal eye position and it will be OK. How to deduct it is a problem.

Here, I will give you some ideas. It may not be good enough, but it can solve the problem.

1. determine the minimum perimeter box of the internal coastline (there may be an external coastline, but most of the external coastline can be excluded), which can save a lot of operation.

2. check whether the coastline points in the box can reach the shortest boundary of the Manhattan in the box. If yes, and the points on the coastline connected to it can be reached, they are all external coastline.

3. Otherwise, if you cannot reach the boundary of the box, it indicates that it is surrounded, belongs to the internal coastline, and all points connected to it belong to the internal coastline.

4. Based on the above method, we can detect all the non-repeated points in the surrounding box to find out which internal coastline is used.

We know that this algorithm is not highly efficient (with the smallest surrounded box, and the connected points are of the same nature, in fact, there should be very few pathfinding times), but it is correct, points inside the surrounded coastline are like little monsters trapped in the Island, which can never escape. Points on the external coastline only consider the island as an obstacle.

This algorithm can process any complex figure. Even if the road is 18 bends, the external points will go through the twists and turns to reach the coastline. There are many hollow points in the hollow Island, and there will be no mistakes in the internal points or external points.

For the second point, we can use deep-first search or the astar algorithm to find the path, rather than the shortest path.


Summary:

When I posted a blog post, I didn't abstract the computation algorithm in go into a 1-0 exam. A bold guess is that the author may have come up with the idea of this question only when he sees the relevant blog. I think some people in csdn should pay more attention to the content of the PEN questions interview questions, rather than the content of this article rather than the content of the PEN questions, I hope that you can extract abstract knowledge from various blogs so as to remain unchanged.

If you have a better idea, leave a message to discuss it.

Share a pen question from a tall Internet company

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.