AcWing
  • 首页
  • 题库
  • 题解
  • 分享
  • 问答
  • 活动
  • 应用
  • 吐槽
  • 登录/注册

AcWing 1113. 红与黑 0.02 AC币

作者: 作者的头像   YimingLi ,  2021-01-12 15:27:06 ,  阅读 10


0


#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int N = 30;
typedef pair<int, int> PII;

int w, h, ans, v[N][N];
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
char s[N][N];
PII start;
bool valid(int x, int y) {
    return x >= 0 && x < h && y >= 0 && y < w && s[x][y] == '.';
}
int main() {
    while (cin >> w >> h && (w || h)) {
        memset(v, 0, sizeof(v)), ans = 0;
        for (int i = 0; i < h; i++) {
            cin >> s[i];
            for (int j = 0; j < w; j++) {
                if (s[i][j] == '@') start = {i, j};
            }
        }
        queue<PII> q;
        ans++;
        q.push(start);
        while (q.size()) {
            auto now = q.front();
            q.pop();
            for (int i = 0; i < 4; i++) {
                int x = now.first + dx[i];
                int y = now.second + dy[i];
                if (!valid(x, y) || v[x][y]) continue;
                v[x][y] = 1;
                ans++;
                q.push({x, y});
            }
        }
        cout << ans << endl;
    }
    return 0;
}

0 评论

你确定删除吗?

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