Codeforces-Round-817-Div-4-F-L-shapes


title: Codeforces Round 817 (Div. 4) F. L-shapes
date: 2023-04-15 14:37:32
categories:

  • Algorithm
  • Codeforces
    tags:
  • codeforces
  • 暴力
  • 1700

image-20230415143801598

image-20230415143843892

题目大意

在一个n*m的网格里面放L形,可以旋转,但是L之间不可以相连,点也不可以相交,问给定的图形是否合法。

思路

暴力:依次去遍历每个*,看这个 *所在的区域是否合法,如果不合法直接输出NO,否则就把这个区域的三个 *打上标记,防止重复访问。

我们只会访问到每种L形第一次出现的*,一共有下面四种情况

*   * ** **
** ** *   *

代码

#include <bits/stdc++.h>

#define int long long
using namespace std;
const int N = 100;
char g[N][N];
bool st[N][N];
int n, m;


bool check(int x, int y) {
    if (x + 1 <= n && y + 1 <= m && g[x + 1][y + 1] == '*' && g[x + 1][y] == '*') {
        st[x][y] = st[x + 1][y + 1] = st[x + 1][y] = true;
        for (int i = x - 1; i <= x + 2; i++) {
            for (int j = y - 1; j <= y + 2; j++) {
                if (i < 1 || i > n || j < 1 || j > m) continue;
                if (i == x - 1 && j == y + 2) continue;
                if ((i == x && j == y) || (i == x + 1 && j == y) || (i == x + 1 && j == y + 1)) continue;
                if (g[i][j] != '.') return false;
            }
        }
    } else if (x + 1 <= n && y - 1 >= 1 && g[x + 1][y] == '*' && g[x + 1][y - 1] == '*') {
        st[x][y] = st[x + 1][y] = st[x + 1][y - 1] = true;
        for (int i = x - 1; i <= x + 2; i++) {
            for (int j = y - 2; j <= y + 1; j++) {
                if (i < 1 || i > n || j < 1 || j > m) continue;
                if (i == x - 1 && j == y - 2) continue;
                if ((i == x && j == y) || (i == x + 1 && j == y) || (i == x + 1 && j == y - 1)) continue;
                if (g[i][j] != '.') return false;
            }
        }
    } else if (x + 1 <= n && y + 1 <= m && g[x][y + 1] == '*' && g[x + 1][y] == '*') {
        st[x][y] = st[x][y + 1] = st[x + 1][y] = true;
        for (int i = x - 1; i <= x + 2; i++) {
            for (int j = y - 1; j <= y + 2; j++) {
                if (i < 1 || i > n || j < 1 || j > m) continue;
                if (i == x + 2 && j == y + 2) continue;
                if ((i == x && j == y) || (i == x && j == y + 1) || (i == x + 1 && j == y)) continue;
                if (g[i][j] != '.') return false;
            }
        }
    } else if (y + 1 <= m && x + 1 <= n && g[x][y + 1] == '*' && g[x + 1][y + 1] == '*') {
        st[x][y] = st[x][y + 1] = st[x + 1][y + 1] = true;
        for (int i = x - 1; i <= x + 2; i++) {
            for (int j = y - 1; j <= y + 2; j++) {
                if (i < 1 || i > n || j < 1 || j > m) continue;
                if (i == x + 2 && j == y - 1) continue;
                if ((i == x && j == y) || (i == x + 1 && j == y + 1) || (i == x && j == y + 1)) continue;
                if (g[i][j] != '.') return false;
            }
        }
    } else {
        return false;
    }
    return true;
}

void solve() {
    memset(st, 0, sizeof st);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> g[i][j];
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (g[i][j] == '*' && !st[i][j]) {
                if (!check(i, j)) {
//                    cout << i << " " << j << endl;
                    cout << "NO" << endl;
                    return;
                }
            }
        }
    }

    cout << "YES" << endl;


}

signed main() {
#ifndef ONLINE_JUDGE
    freopen("../test.in", "r", stdin);
    freopen("../test.out", "w", stdout);
#endif
    int _;
    cin >> _;
    while (_--) solve();

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

重生之我是cxk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值