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=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//**************************************************************************************

string s[1010];
int vis[1010][1010];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
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=0;i<n;i++)
        cin>>s[i];
    int ans=0;
    int flag=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(s[i][j]=='+')
            {
                int flag=0;
                for(int k=0;k<4;k++)
                {
                    if(i+dx[k]<0||i+dx[k]>=n)
                        flag=1;
                    else if(j+dy[k]<0||j+dy[k]>=m)
                        flag=1;
                    else if(s[i+dx[k]][j+dy[k]]=='-')
                        flag=1;
                }
                if(flag)
                    ans++;
            }
        }
    }
    printf("%d\n",ans);
}

 

转载于:https://www.cnblogs.com/qscqesze/p/4780549.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值