AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 校园
  • 应用
  • 文章
    • 题解
    • 分享
    • 问答
  • 吐槽
  • 登录/注册

为啥dx dy 数组变一下顺序就不对ne



3


1

题目链接 acwing 1098.城堡问题

求大佬解答为啥

int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0};
//int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};

我换成下面那个就不对了

错误的代码:

// 用二进制表示的状态, 太强了,以后看到 1 2 4 8 要引起注意

#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>

#define x first
#define y second

using namespace std;
typedef pair<int ,int> PII;
const int N = 60;
bool yin[N][N];
int g[N][N];
int n, m;
int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0};
//int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
queue<PII> q;

int bfs(int a, int b)
{
    q.push({a, b});
    int area = 0;
    yin[a][b] = true;

    while(q.size())
    {
        auto t = q.front();
        q.pop();
        area ++;
        for(int i = 0; i < 4; i ++)
        {
            int sx = t.x + dx[i], sy = t.y + dy[i];
            if(sx >= n || sx < 0 || sy >= m || sy < 0 || yin[sx][sy]) continue;
            if(g[t.x][t.y] >> i & 1) continue;

            yin[sx][sy] = true;
            q.push({sx, sy});
        }

    }
    return area;
}

int main()
{
    cin >> n >> m;
    for(int i = 0; i < n; i ++)
        for(int j = 0; j < m; j ++)
            cin >> g[i][j];

    int cnt = 0, ares = 0;
    for(int i = 0; i < n; i ++)
        for(int j = 0; j < m; j ++)
        {
            if(!yin[i][j])
            {
                ares = max (ares, bfs(i, j));
                yin[i][j] = true;
                cnt ++;
            }
        }

    cout << cnt << endl;
    cout << ares << endl;
    return 0;
}

不知道为啥错了



提问于17天前
no_one
10967


1 个问答



0

1表示西墙,2表示北墙,4表示东墙,8表示南墙

回答于16天前
WangJY
8701

奥,懂了懂了,谢谢大佬 –  no_one   16天前

你这样相当于1表示西墙,2表示东墙,4表示南墙,8表示北墙 –  WangJY   16天前

你换顺序的话就不对了 –  WangJY   16天前


我来回答
你确定删除吗?

© 2018-2022 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息