面試題:P1-數海岸線演算法,試題p1-海岸線演算法
這是一個比較偏僻的網站上的題目,做這個題目的原因是某同學去面試的時候遇到了這樣的題目,然後問我如何做,遇上這樣的問題不解決就不是我的風格了。
先給出這個網站的題目網址:http://jobs.p1.com/tech/costal.html
估計很少人上個這個網站,做個這個網站的題目更加少了,所以有公司拿這樣的現成的題目考面試者,有面試者做過了的機率是很少的。不過只要學校不太糟糕,那麼能做出這樣題目的人,進個什麼BAT不是什麼難事吧。
同時貼出題目吧:
Coast Length
|
Puzzle ID: coast
The island municipality of Soteholm is required to write a plan of action for their work with emission of greenhouse gases. They realize that a natural first step is to decide whether they are for or against global warming. For this purpose they have read the IPCC report on climate change and found out that the largest effect on their municipality could be the rising sea level.
The residents of Soteholm value their coast highly and therefore want to maximize its total length. For them to be able to make an informed decision on their position in the issue of global warming, you have to help them find out whether their coastal line will shrink or expand if the sea level rises. From height maps they have figured out what parts of their islands will be covered by water, under the different scenarios described in the IPCC report, but they need your help to calculate the length of the coastal lines.
Task
You will be given a map of Soteholm as an N×M grid. Each square in the grid has a side length of 1 km and is either water or land. Your goal is to compute the total length of sea coast of all islands. Sea coast is all borders between land and sea, and sea is any water connected to an edge of the map only through water. Two squares are connected if they share an edge. You may assume that the map is surrounded by sea. Lakes and islands in lakes are not contributing to the sea coast.
Figure 1:
Gray squares are land and white squares are water. The thick black line is the sea coast. This example corresponds to Sample Input 1.
Input
The first line of the input contains two space separated integers N and M where 1 ≤ N, M ≤ 1000. The following N lines each contain a string of length M consisting of only zeros and ones. Zero means water and one means land.
Output
Output one line with one integer, the total length of the coast in km.
|
Sample Input 1 |
|
Sample Output 1 |
5 6
011110
010110
111000
000010
000000
|
|
20 |
|
To submit a solution to this problem, send an e-mail to challenge@p1.cn
Include your source code files as attachments and set the subjectline to coast.
You should receive an answer within minutes. (make sure the subjectline is exactly: coast)
出處:http://jobs.p1.com/tech/costal.html
看上面的圖就大概能明白,就是四周是大海包圍的,數海岸線的長度,其中中間空的算是湖岸線了,不能算是海岸線,這就是其中的痛點。
主要演算法要點:
1 以地圖的四周,即地圖的邊沿,如黃色部分:
作為收拾搜尋點,使用遞迴搜尋,如果是0,代表是海水能滲入的地方,那麼就做好標識,My Code使用const char WATER = '2',標識;沿這樣的點能走到的點都標識為WATER,這樣就能標識出海水滲透到的格子,並和內湖的格子區分開來了。
2 第1步解決了標識和區分內湖的問題,然後就解決數海岸線的問題了,我們分開兩步來數,第一步數最外邊的海岸線,第二部數格子之間的海岸線;
即第一步數:
這裡應該數作5條海岸線。
第二步數格子之間的海岸線:
這裡應該數作30條海岸線,但是由於格子之間的海岸線會數重複,那麼實際的海岸線應該是30/2=15條
最終5 + 15 = 20條海岸線。
理解了兩個關鍵演算法點之後,再寫演算法就不難了,只要處理好代碼細節問題就好了。
由於這個網站的判斷系統對我來說也比較新,故此對一個新系統的輸入輸出處理和判別系統都需要熟悉以下,故此多調試以下是必然的了。
最後給出AC的代碼:
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <vector>#include <string.h>#include <algorithm>#include <iostream>#include <string>#include <limits.h>#include <stack>#include <queue>#include <set>#include <map>using namespace std;const int MAX_N = 1001;char arr[MAX_N][MAX_N];int n, m;const char WATER = '2';inline bool isLegal(int r, int c){return 0<=r && 0<=c && r<n && c<m;}void fillWater(int r, int c){if (!isLegal(r, c)) return ;if (arr[r][c] != '0') return ;arr[r][c] = WATER;fillWater(r-1, c);fillWater(r+1, c);fillWater(r, c-1);fillWater(r, c+1);}int main(){//freopen("in.txt", "r", stdin);scanf("%d %d", &n, &m);getchar();for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){char a = getchar();while (a != '0' && a != '1'){a = getchar();}///< handle io crazy problems.arr[i][j] = a;}}for (int i = 0; i < n; i++){fillWater(i, 0);fillWater(i, m-1);}for (int j = 1; j+1 < m; j++){fillWater(0, j);fillWater(n-1, j);}int outEdge = 0, inEdge2 = 0;for (int i = 0; i < n; i++){if (arr[i][0] != WATER) outEdge++;if (arr[i][m-1] != WATER) outEdge++;}for (int j = 0; j < m; j++){if (arr[0][j] != WATER) outEdge++;if (arr[n-1][j] != WATER) outEdge++;}for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){if (arr[i][j] == WATER){if (isLegal(i-1, j)){if (arr[i-1][j] != WATER) inEdge2++;}if (isLegal(i+1, j)){if (arr[i+1][j] != WATER) inEdge2++;}if (isLegal(i, j-1)){if (arr[i][j-1] != WATER) inEdge2++;}if (isLegal(i, j+1)){if (arr[i][j+1] != WATER) inEdge2++;}}else if (arr[i][j] != WATER){if (isLegal(i-1, j)){if (arr[i-1][j] == WATER) inEdge2++;}if (isLegal(i+1, j)){if (arr[i+1][j] == WATER) inEdge2++;}if (isLegal(i, j-1)){if (arr[i][j-1] == WATER) inEdge2++;}if (isLegal(i, j+1)){if (arr[i][j+1] == WATER) inEdge2++;}}}}printf("%d", outEdge + (inEdge2>>1));return 0;}