zoukankan      html  css  js  c++  java
  • 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
    ",ans);
    }
  • 相关阅读:
    常见的7种排序算法
    ZooKeeper
    线上问题排查(2)——JDK内置工具
    Java并发编程:深入剖析ThreadLocal
    没有main的hello world 程序——Java 分类: java 2015-06-24 16:20 11人阅读 评论(0) 收藏
    Django笔记 —— 模型
    Django笔记 —— MySQL安装
    USACO Section2.3 Controlling Companies 解题报告 【icedream61】
    USACO Section2.3 Money Systems 解题报告 【icedream61】
    USACO Section2.3 Zero Sum 解题报告 【icedream61】
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4780549.html
Copyright © 2011-2022 走看看