3371. 舒适的奶牛

摘要
Title: 3371. 舒适的奶牛
Tag: 枚举
Memory Limit: 64 MB
Time Limit: 1000 ms

Powered by:NEFU AB-IN

Link

3371. 舒适的奶牛

  • 题意

    Farmer John 的草地可以被看作是一个由正方形方格组成的巨大的二维方阵(想象一个巨大的棋盘)。
    初始时,草地上是空的。
    Farmer John 将会逐一地将 N 头奶牛加入到草地上。
    第 i 头奶牛将会占据方格 (xi,yi),不同于所有已经被其他奶牛占据的方格。
    一头奶牛被称为是「舒适的」,如果它水平或竖直方向上与恰好三头其他奶牛相邻。
    Farmer John 对他的农场上舒适的奶牛数量感兴趣。
    对 1…N 中的每一个 i,输出第 i 头奶牛加入到草地上之后舒适的奶牛的数量。

  • 思路

    每次加入点时,只会周围的四个点受到影响,枚举这四个点是否被占据,若占据的话更新状态即可

  • 代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    /*
    * @Author: NEFU AB-IN
    * @Date: 2022-06-21 15:39:07
    * @FilePath: \ACM\Acwing\3371\3371.cpp
    * @LastEditTime: 2022-06-21 16:09:10
    */
    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    #define SZ(X) ((int)(X).size())
    #define IOS \
    ios::sync_with_stdio(false); \
    cin.tie(0); \
    cout.tie(0);
    #define DEBUG(X) cout << #X << ": " << X << endl;
    typedef pair<int, int> PII;

    const int INF = INT_MAX;
    const int N = 1e3 + 10;

    int st[N][N];
    int vis[N][N];
    int dir[5][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {0, 0}};
    signed main()
    {
    IOS;

    int n, cnt = 0;
    cin >> n;

    auto judge = [&](int x, int y) {
    int cnt = 0;
    for (int i = 0; i < 4; ++i)
    {
    int x1 = x + dir[i][0];
    int y1 = y + dir[i][1];

    if (x1 >= 0 && y1 >= 0 && st[x1][y1])
    cnt += 1;
    }
    if (!vis[x][y] && cnt == 3)
    {
    vis[x][y] = 1;
    return 1;
    }
    else if (vis[x][y])
    {
    vis[x][y] = 0;
    return -1;
    }
    return 0;
    };

    while (n--)
    {
    int x, y;
    cin >> x >> y;
    st[x][y] = 1;

    for (int i = 0; i < 5; ++i)
    {
    int x1 = x + dir[i][0];
    int y1 = y + dir[i][1];
    if (x1 >= 0 && y1 >= 0 && st[x1][y1])
    cnt += judge(x1, y1);
    }

    cout << cnt << '\n';
    }

    return 0;
    }
使用搜索:谷歌必应百度