FZU 2107
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.
类型:图形搜索
code:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include<cmath>
#include<cstdlib>
#include<map>
#pragma warning(disable:4996)
#include<iostream>
#include<queue>
using namespace std;
char mp[5][5];
int n;
int ans = 0;
void dfs(int x,int y)
{
int nx = x, ny = y + 1;
if (x == n&&y == 4)
{
ans++;
return;
}
if (ny > 4) {
nx++;
ny %= 4;
}
if (mp[x][y]) {
dfs(nx, ny);
return;
}
if (!(mp[x][y] || mp[x + 1][y] || x + 1 > n || y > 4))
{
mp[x][y] =mp[x + 1][y] = 1;
dfs(nx, ny);
mp[x][y] = mp[x + 1][y] = 0;
}
if (!(mp[x][y] || mp[x][y + 1] || x > n || y + 1 > 4))
{
mp[x][y] = mp[x ][y+1] = 1;
dfs(nx, ny);
mp[x][y] = mp[x ][y+1] = 0;
}
mp[x][y] = 1;
dfs(nx, ny);
mp[x][y] = 0;
}
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
int x, y;
scanf("%d",&n);
int i,j;
if (n == 1)
{
printf("0\n");
continue;
}
ans = 0;memset(mp, 0, sizeof(mp));
for (i = 1; i <=n-1;i++)
{
for (j = 1; j <= 3; j++)
{
mp[i][j] = mp[i + 1][j] = mp[i][j + 1] = mp[i + 1][j + 1] = 1;
dfs(1, 1);
mp[i][j] = mp[i + 1][j] = mp[i][j + 1] = mp[i + 1][j + 1] = 0;
}
}
printf("%d\n",ans);
}
}
类似题目poj2411
这个题该用壮压dp做的
dfs超时了hiahia
code:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include<cmath>
#include<cstdlib>
#include<map>
#pragma warning(disable:4996)
#include<iostream>
#include<queue>
using namespace std;
int mp[12][12];
int n;
int ans;
int h, w;
void dfs(int x, int y)
{
int nx = x;
int ny = y + 1;
if (ny > w)
{
nx = x + 1;
ny %= w;
}
if (x == h&&y == w&&mp[x][y])
{
ans++;
return;
}
if (mp[x][y])
{
dfs(nx, ny);
return;
}
if (!(mp[x][y+1]||y+1>w||x>h))
{
mp[x][y + 1] = 1;
dfs(nx, ny);
mp[x][y + 1] = 0;
}
if (!(mp[x][y]||mp[x+1][y] || y>w || x+1>h))
{
mp[x+1][y] = 1;
dfs(nx, ny);
mp[x+1][y] = 0;
}
}
int main()
{
while (scanf("%d %d", &h, &w) != EOF && (h || w))
{
ans = 0;
int i, j, k;
memset(mp, 0, sizeof(mp));
dfs(1, 1);
printf("%d\n", ans);
}
return 0;
}