Codeforces GYM 100114 B. Island 水题

时间:2023-03-09 22:02:29
Codeforces GYM 100114 B. Island 水题

B. Island

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/gym/100114

Description

On February 30th this year astronauts from the International Space Station flew over the Pacific Ocean and took a picture, on which was discovered a previously unknown island. On the digitized picture the island appears as a connected set of square cells. This means that someone can reach some cell of land from some other cell land, going from cell to cell through their common side. There is no other water area within the island. The island is surrounded by water. The coastline of the island is a closed polygonal line. The water cell are marked by minus sign ("–"), and the land cell – by plus sign ("+"). The coastline cell is a cell, which have a common border with water cell. In the figure below the length of the coastline is 14 cells. The other five cells of land are internal cells of the island. Write a program that, given dimensions of the rectangle n and m and digitized picture, calculates l – the number of cells that form the coastline of the island.

Input

The first line of input file contains two integers n and m. The following n lines contain m characters (the char "–" – cell with water, and the "+" – cell with land)

Output

The output file should consist of one integer l – the number of cells that form the coastline of the island.

Sample Input

7 8 –––––––– –––+++–– –––+++–– –+++++–– –+++++–– ––++–+–– --------

Sample Output

14

HINT

 3 ≤ n, m ≤ 1 000, l > 0.

题意

给你一个岛屿,问你他的边界有多长

题解:

对于一个点,只要他周围有一个点是海,那么这个点就是边界

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 510000
#define mod 10007
#define eps 1e-9
int Num;
//const int inf=0x7fffffff; //§ß§é§à§é¨f§³
const int inf=0x3f3f3f3f;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** string s[];
int vis[][];
int dx[]={,-,,};
int dy[]={,,,-};
struct node
{
int x,y;
};
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
//input.txt / output.txt
int n=read(),m=read();
node st;
for(int i=;i<n;i++)
cin>>s[i];
int ans=;
int flag=;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(s[i][j]=='+')
{
int flag=;
for(int k=;k<;k++)
{
if(i+dx[k]<||i+dx[k]>=n)
flag=;
else if(j+dy[k]<||j+dy[k]>=m)
flag=;
else if(s[i+dx[k]][j+dy[k]]=='-')
flag=;
}
if(flag)
ans++;
}
}
}
printf("%d\n",ans);
}