標題:全球變暖
你有一張某海域NxN像素的照片,".“表示海洋、”#"表示陸地,如下所示:
…
.##…
.##…
…##.
…####.
…###.
…
####其中"上下左右"四個方向上連在一起的一片陸地組成一座島嶼。例如上圖就有2座島嶼。
####由於全球變暖導致了海面上升,科學家預測未來幾十年,島嶼邊緣一個像素的範圍會被海水淹沒。具體來說如果一塊陸地像素與海洋相鄰(上下左右四個相鄰像素中有海洋),它就會被淹沒。
####例如上圖中的海域未來會變成如下樣子:
…
…
…
…
…#…
…
…
####請你計算:依照科學家的預測,照片中有多少島嶼會被完全淹沒。
【輸入格式】
第一行包含一個整數N。 (1 <= N <= 1000)
以下N行N列代表一張海域照片。
照片保證第1行、第1列、第N行、第N列的像素都是海洋。
【輸出格式】
一個整數表示答案。
【輸入樣例】
7
…
.##…
.##…
…##.
…####.
…###.
…
【輸出樣例】
1
思路:就是利用深度優先的算法把把與海洋相鄰的陸地變成海洋,再進行一次搜索,查找圖中的連通塊數量,就是島嶼的數量(DFS)見第一第二段代碼
改進:這題的島嶼淹沒後有可能產生多個島嶼,所以我們要判斷哪些是完全淹沒,哪些是不會完全淹沒的,甚至會產生型島嶼的。首先就要bfs搜索一共有多少個島嶼,再進行一次bfs找到不會完全淹沒的島嶼數量,最後兩數字相減便是完全淹沒的島嶼數量了;(BFS)見下面第三段代碼
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<stdio.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
map<int, int>::iterator it;
struct node
{
int x, y;
node(int x = 0, int y = 0) :x(x), y(y) {}
};
int n;
int cnt;
char mp[1005][1005];
int vis[1005][1005];
vector<node> d[maxn];
int ne[4][2] = { 1,0,-1,0,0,1,0,-1 };
void find(int x, int y)//尋找相連點
{
d[cnt].push_back(node(x, y));
vis[x][y] = 1;
for (int i = 0; i < 4; i++)
{
int tx = x + ne[i][0];
int ty = y + ne[i][1];
if (tx < 0 || tx >= n || ty < 0 || ty >= n || vis[tx][ty] || mp[tx][ty] == '.')
continue;
find(tx, ty);
}
return;
}
void Preprocess()//預處理島嶼
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (mp[i][j] == '#' && !vis[i][j])
{
cnt++;
find(i, j);
}
return;
}
void dfs(int x, int y)//侵蝕
{
vis[x][y] = 1;
for (int i = 0; i < 4; i++)
{
int tx = x + ne[i][0];
int ty = y + ne[i][1];
if (tx < 0 || tx >= n || ty < 0 || ty >= n || vis[tx][ty])
continue;
if (mp[tx][ty] == '#')
vis[tx][ty] = 1;
else
dfs(tx, ty);
}
return;
}
void solve()//開始侵蝕
{
mem(vis, 0);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (mp[i][j] == '.' && !vis[i][j])
dfs(i, j);
return;
}
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
cin >> mp[i];
//Preprocess();
solve();
Preprocess();
cout << cnt << endl;
system("pause");
return 0;
}
這是精簡過後的代碼
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
struct node
{
int x, y;
node(int x1 = 0, int y1= 0)
{
x = x1; y = y1;
}
};
int n;
int cnt;
char mp[1005][1005];//存符號
int vis[1005][1005];
vector<node> d[1000];
int mov[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };//座標移動
void find(int x, int y)//尋找相連點
{
d[cnt].push_back(node(x, y));
vis[x][y] = 1;
for (int i = 0; i < 4; i++)
{
int tx = x + mov[i][0];
int ty = y + mov[i][1];
if (tx < 0 || tx >= n || ty >= n || ty < 0 || vis[tx][ty] || mp[tx][ty] == '.')
continue;
find(tx, ty);
}
return;
}
void preprocess()//處理島嶼
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (mp[i][j] == '#'&&!vis[i][j])
{
cnt++;
find(i, j);
}
}
}
}
void dfs(int x, int y)//侵蝕
{
vis[x][y] = 1;
for (int i = 0; i < 4; i++)
{
int tx = x + mov[i][0];
int ty = y + mov[i][1];
if (tx < 0 || tx >= n || ty < 0 || ty >= n || vis[tx][ty])
continue;
if (mp[tx][ty] == '#')//深度優先的終止條件
{
vis[tx][ty] = 1;
}
else
dfs(tx, ty);
}
return;
}
void solve()//開始腐蝕
{
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (mp[i][j] == '.' && !vis[i][j])
dfs(i, j);
}
}
return;
}
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> mp[i];
}
solve();
preprocess();//查找連通塊
cout << "剩下的島嶼" << cnt << endl;
system("pause");
return 0;
}
BFS的代碼(真正的答案)
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
char a[1000][1000];//記錄圖的信息
int n, vis[1000][1000];
int dir[4][2] = { -1,0,0,-1,0,1,1,0 };
int num = 0;//橋樑陸地
struct point
{
int x, y;
point(int a, int b)
{
x = a;
y = b;
}
};
void find(int x, int y)//尋找相連點
{
vis[x][y] = 1;
for (int i = 0; i < 4; i++)
{
int tx = x + dir[i][0];
int ty = y + dir[i][1];
if (tx < 0 || tx >= n || ty < 0 || ty >= n || vis[tx][ty] || a[tx][ty] == '.')
continue;
find(tx, ty);
}
return;
}
int bfs(int x, int y)
{
queue<point> q;//隊列
q.push(point(x, y));
int left=0;
while (!q.empty())
{
point p = q.front();
q.pop();
int cnt = 0;//一塊陸地周圍的陸地數量
for (int i = 0; i < 4; i++)
{
int dx = p.x + dir[i][0];
int dy = p.y + dir[i][1];
if (dx < 0 || dx >= n || dy < 0 || dy >= n)
continue;
if (a[dx][dy] == '#')
{
cnt++;
if (!vis[dx][dy])
{
vis[dx][dy] = 1;
q.push(point(dx, dy));
/*if ((a[dx - 1][dy] == '#'&&a[dx + 1][dy] == '#'&&a[dx][dy + 1] == '.'&&a[dx][dy - 1] == '.') || (a[dx - 1][dy] == '.'&&a[dx + 1][dy] == '.'&&a[dx][dy + 1] == '#'&&a[dx][dy - 1] == '#'))
{
num++;
}*/
}
}
}
if (cnt == 4)
left++;
}
return left;
}
int main()
{
int i, j, ans = 0;//不會被完全淹沒的連通塊的數量
int cnt = 0;//淹沒前的連通塊數量
cin >> n;
for (i = 0; i < n; i++)
{
cin >> a[i];
}
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (a[i][j] == '#' && !vis[i][j])
{
cnt++;
find(i, j);
}
memset(vis, 0, sizeof(vis));
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (a[i][j] == '#' && !vis[i][j])
{
vis[i][j] = 1;
if (!bfs(i, j))
ans++;
}
}
}
cout << "被完全淹沒的島嶼數量" << endl;
cout << cnt << " " << ans << endl;
cout << cnt - ans << endl;
system("pause");
return 0;
}