题解题解占地斗士!
hymcr05
题目大意
给出一个由 .
和 #
组成的n×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
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
| #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; }
|