海岸线——模拟题

题目链接:http://icpc.upc.edu.cn/problem.php?cid=2230&pid=0

题目描述

一个王国分成n*m个六边形区域,每个区域内是陆地或者是水。如果一条边两侧为陆地和水,则该条边成为海岸线,求这个王国海岸线的长度。

输入

第一行两个整数N,M。
以下N行每行M个字符,“.”表示水,“#”表示陆地。偶数行需要向右移半格,具体见样例。

输出

一个整数,海岸线的长度。

样例输入

3 6
..#.##
.##.#.
#.#...

样例输出

19

提示

数据表示如下图像:
可以看出,海岸线长度为19。
在这里插入图片描述
【数据范围】
对100%的数据1<=N,M<=50

///尴尬的。。。。。。。。。。。。。。。。。。。。。。。。。。。。。分界线
每一个元素有六个方向,运用两个for循环解决上述问题

#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize (2)
#pragma G++ optimize (2)
#include <bits/stdc++.h>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define wuyt main
typedef long long ll;
#define HEAP(...) priority_queue<__VA_ARGS__ >
#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >
template<class T> inline T min(T &x,const T &y){return x>y?y:x;}
template<class T> inline T max(T &x,const T &y){return x<y?y:x;}
//#define getchar()(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 21) + 1], *p1 = buf, *p2 = buf;
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();
if(c == '-')Nig = -1,c = getchar();
while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();
return Nig*x;}
#define read read()
const ll inf = 0x3f3f3f3f;
const int maxn = 2e5 + 7;
const int mod = 1e9 + 7;
#define start int wuyt()
#define end return 0
int n,m;
char ss[100][100];
start{
    n=read,m=read;
    int cnt=0;
    for(int i=1;i<=n;i++)
        cin>>ss[i]+1;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(ss[i][j]=='#'){
                if(ss[i][j-1]=='.') cnt++;
                if(ss[i][j+1]=='.') cnt++;
                if(ss[i+1][j]=='.') cnt++;
                if(ss[i-1][j]=='.') cnt++;
                if(i%2){
                    if(ss[i+1][j-1]=='.') cnt++;
                    if(ss[i-1][j-1]=='.') cnt++;
                }
                else{
                    if(ss[i+1][j+1]=='.') cnt++;
                    if(ss[i-1][j+1]=='.') cnt++;
                }
            }
        }
    }
    cout<<cnt;
	end;
}

原文地址:https://www.cnblogs.com/PushyTao/p/13144215.html