题解:P10313 [SHUPC 2024] 占地斗士!

题目大意

给出一个由 .# 组成的\(n \times m\) 矩阵,然后再给你这 \(4\) 种图像,用着四种图像对矩阵进行覆盖(每个只能用一次)。其中,# 的位置不可以被图像遮挡,也不能放在不能放置的格子上。

解题思路

考虑使用爆搜

第一个图像:
if (mp[i][j] != '#' && mp[i + 1][j + 1] != '#' && mp[i + 1][j - 1] != '#' && mp[i - 1][j + 1] != '#' && mp[i - 1][j - 1] != '#')
第二个图像:
if (mp[i + 1][j] != '#' && mp[i][j - 1] != '#' && mp[i][j + 1] != '#' && mp[i - 1][j] != '#')
第三图像:
if (mp[i][j] != '#' && mp[i + 1][j] != '#' && mp[i + 1][j + 1] != '#' && mp[i][j + 1] != '#')
第三图像:
if (pd && !card[4] && mp[i][j] != '#' && mp[i + 1][j] != '#' && mp[i - 1][j] != '#' && mp[i][j + 1] != '#' && mp[i][j - 1] != '#')
注意:我们需要标记使用过的图像以及判断是否越界

Code

#include <bits/stdc++.h>
#define pd i + 1 <= n && j + 1 <= m &&j - 1 >= 1 && i - 1 >= 1
#define pd2 i + 1 <= n && j + 1 <= m
using namespace std;
int n, m, ans;
char mp[11][11];
bool card[5];
void dfs(int x, int cnt)
{
    ans = max(ans, cnt);
    if (x == 4)
    {
        cout << 18;
        exit(0);
    }
    for (int i = 1; i <= n; i++)
    {   
        for (int j = 1; j <= n; j++)
        {
            
            {if (pd && !card[1] && mp[i][j] != '#' && mp[i + 1][j + 1] != '#' && mp[i + 1][j - 1] != '#' && mp[i - 1][j + 1] != '#' && mp[i - 1][j - 1] != '#')
                card[1] = 1;
                mp[i][j] = mp[i + 1][j + 1] = mp[i + 1][j - 1] = mp[i - 1][j + 1] = mp[i - 1][j - 1] = '#';
                dfs(x + 1, cnt + 5);
                card[1] = 0;
                mp[i][j] = mp[i + 1][j + 1] = mp[i + 1][j - 1] = mp[i - 1][j + 1] = mp[i - 1][j - 1] = '.';
            }
            if (pd && !card[2] && mp[i + 1][j] != '#' && mp[i][j - 1] != '#' && mp[i][j + 1] != '#' && mp[i - 1][j] != '#')
            {
                card[2] = 1;
                mp[i + 1][j] = mp[i][j - 1] = mp[i][j + 1] = mp[i - 1][j] = '#';
                dfs(x + 1, cnt + 4);
                card[2] = 0;
                mp[i + 1][j] = mp[i][j - 1] = mp[i][j + 1] = mp[i - 1][j] = '.';
            }
            if (pd2 && !card[3] && mp[i][j] != '#' && mp[i + 1][j] != '#' && mp[i + 1][j + 1] != '#' && mp[i][j + 1] != '#')
            {
                card[3] = 1;
                mp[i][j] = mp[i + 1][j] = mp[i + 1][j + 1] = mp[i][j + 1] = '#';
                dfs(x + 1, cnt + 4);
                card[3] = 0;
                mp[i][j] = mp[i + 1][j] = mp[i + 1][j + 1] = mp[i][j + 1] = '.';
            }
            if (pd && !card[4] && mp[i][j] != '#' && mp[i + 1][j] != '#' && mp[i - 1][j] != '#' && mp[i][j + 1] != '#' && mp[i][j - 1] != '#')
            {
                card[4] = 1;
                mp[i][j] = mp[i + 1][j] = mp[i - 1][j] = mp[i][j + 1] = mp[i][j - 1] = '#';
                dfs(x + 1, cnt + 5);
                card[4] = 0;
                mp[i][j] = mp[i + 1][j] = mp[i - 1][j] = mp[i][j + 1] = mp[i][j - 1] = '.';
            }
        }
    }
}
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> mp[i][j];
    dfs(0, 0);
    cout << ans;
    return 0;
}
posted @ 2024-08-15 22:31  ACyming  阅读(30)  评论(0)    收藏  举报