Accept: 183 Submit: 465
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
Cao Cao was hunted down by thousands of enemy soldiers when he escaped from Hua Rong Dao. Assuming Hua Rong Dao is a narrow aisle (one N*4 rectangle), while Cao Cao can be regarded as one 2*2 grid. Cross general can be regarded as one 1*2 grid.Vertical general can be regarded as one 2*1 grid. Soldiers can be regarded as one 1*1 grid. Now Hua Rong Dao is full of people, no grid is empty.
There is only one Cao Cao. The number of Cross general, vertical general, and soldier is not fixed. How many ways can all the people stand?
Input
There is a single integer T (T≤4) in the first line of the test data indicating that there are T test cases.
Then for each case, only one integer N (1≤N≤4) in a single line indicates the length of Hua Rong Dao.
Output
Sample Input
Sample Output
Hint
Here are 2 possible ways for the Hua Rong Dao 2*4.

题意:
给出 T,代表有 T 组数组,后给出 N(1 ~ 4)。代表有 N * 4 个格子,现有 4 种格子块去填充这 N * 4 个格子,而 CaoCao 是必须用的,但是只能用 1 个。问一共有多少种填充方式。
思路:
DFS。N 最大也只有4,所以不会超时。
AC:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int vis[5][5], sum;
int n, way;
void dfs(int x, int y, int ans) {
if (ans == sum) {
++way;
return;
}
if (vis[x][y]) {
if (y + 1 <= 4) dfs(x, y + 1, ans);
else if (x + 1 <= n) dfs(x + 1, 1, ans);
} else {
vis[x][y] = 2;
dfs(1, 1, ans + 1);
vis[x][y] = 0;
if (y + 1 <= 4 && !vis[x][y + 1]) {
vis[x][y] = vis[x][y + 1] = 3;
dfs(1, 1, ans + 2);
vis[x][y] = vis[x][y + 1] = 0;
}
if (x + 1 <= n && !vis[x + 1][y]) {
vis[x][y] = vis[x + 1][y] = 4;
dfs(1, 1, ans + 2);
vis[x][y] = vis[x + 1][y] = 0;
}
}
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
sum = 4 * n;
way = 0;
for (int i = 1; i <= n - 1; ++i) {
for (int j = 1; j <= 3; ++j) {
memset(vis, 0, sizeof(vis));
vis[i][j] = 1;
vis[i][j + 1] = 1;
vis[i + 1][j] = 1;
vis[i + 1][j + 1] = 1;
dfs(1, 1, 4);
}
}
printf("%d\n", way);
}
return 0;
}
本文探讨了经典的华容道问题,通过深度优先搜索(DFS)算法解决特定规模的棋盘上不同棋子的排列组合问题。对于N×4大小的棋盘,在限定棋子类型的情况下,计算出所有可能的棋子布局方案。
1804

被折叠的 条评论
为什么被折叠?



