• poj4022


    模拟

    利用pick公式area=on/2+in-1

    计算in的时候从外围广搜所有不是in的点,再用总数减去。

    View Code
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    #define maxn 150
    
    struct Point
    {
        int x, y;
    } q[maxn * maxn];
    
    char map[maxn][maxn];
    int n, m;
    bool vis[maxn][maxn];
    int dir[4][2] =
    {
    { 1, 1 },
    { 1, -1 },
    { -1, 1 },
    { -1, -1 } };
    
    int dir1[4][2] =
    {
    { 1, 0 },
    { 0, -1 },
    { 0, 1 },
    { -1, 0 } };
    
    void input()
    {
        scanf("%d%d", &n, &m);
        for (int i = 0; i < n; i++)
            scanf("%s", map[i]);
    }
    
    bool out_of_bound(Point &a)
    {
        return a.x < 0 || a.y < 0 || a.x > n + 2 || a.y > m + 2;
    }
    
    bool ok(Point &a, Point &b)
    {
        int x = min(a.x, b.x) - 1;
        int y = min(a.y, b.y) - 1;
        if (x < 0 || y < 0 || x >= n || y >= m)
            return true;
        return map[x][y] == '.';
    }
    
    bool empty(int x, int y, char ch)
    {
        if (x < 0 || y < 0 || x >= n || y >= m)
            return true;
        return map[x][y] != ch;
    }
    
    bool on_the_bound(Point &a)
    {
        return !empty(a.x - 2, a.y - 2, '\\') || !empty(a.x - 1, a.y - 1, '\\')
                || !empty(a.x - 1, a.y - 2, '/') || !empty(a.x - 2, a.y - 1, '/');
    }
    
    void work()
    {
        memset(vis, 0, sizeof(vis));
        vis[0][0] = true;
        int front, rear;
        front = rear = 0;
        Point a;
        a.x = a.y = 0;
        q[rear++] = a;
        int cnt = 1;
        while (front != rear)
        {
            a = q[front++];
            for (int i = 0; i < 4; i++)
            {
                Point b;
                b.x = a.x + dir[i][0];
                b.y = a.y + dir[i][1];
                if (!out_of_bound(b) && !on_the_bound(b) && ok(a, b)
                        && !vis[b.x][b.y])
                {
                    q[rear++] = b;
                    vis[b.x][b.y] = true;
                    cnt++;
                }
                b.x = a.x + dir1[i][0];
                b.y = a.y + dir1[i][1];
                if (!out_of_bound(b) && !on_the_bound(b) && !vis[b.x][b.y])
                {
                    q[rear++] = b;
                    vis[b.x][b.y] = true;
                    cnt++;
                }
            }
        }
        int on = n * m;
        for (int i = 0; i < n; i++)
            on -= count(map[i], map[i] + m, '.');
        cnt += on;
        int in = (n + 3) * (m + 3) - cnt;
        int ans = on / 2 + in - 1;
        printf("%d\n", ans);
    }
    
    int main()
    {
        //freopen("t.txt", "r", stdin);
        input();
        work();
    }
  • 相关阅读:
    多叉树
    PowerDesigner设置集锦(2)
    Delphi应用程序在命令行下带参数执行返回命令行提示的问题
    不允许在 '*******' 上使用扩展属性,或对象不存在
    仓库管理系统开发完成
    动态创建Frame窗体(根据类名,除T以外的字母)
    Access中的常用TSQL
    批量删除同类文件(带通配符)
    判断Access数据库中的表或查询是否存在的SQL
    序列化FastReport,重要提示少走弯路
  • 原文地址:https://www.cnblogs.com/rainydays/p/2735731.html
Copyright © 2020-2023  润新知