码迷,mamicode.com
首页 > 其他好文 > 详细

FZU 2107 Hua Rong Dao 递归回溯

时间:2015-04-10 22:07:08      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:


Problem 2107 Hua Rong Dao

Accept: 254    Submit: 591
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

For each test case, print the number of ways all the people can stand in a single line.

技术分享 Sample Input

212

技术分享 Sample Output

018

技术分享 Hint

Here are 2 possible ways for the Hua Rong Dao 2*4.

技术分享

技术分享 Source

“高教社杯”第三届福建省大学生程序设计竞赛


在n*4的矩形里面放满一些小的矩形,要求必须有一个2*2的矩形,还有1*2,2*1,1*1的矩形,求有多少种方法满足条件。
递归枚举所有情况。

//15 ms	208KB
#include<stdio.h>
#include<string.h>
int vis[10][10];
int n,ans,flag;
bool judge(int x,int y)
{
    if(x>=1&&x<=n&&y>=1&&y<=4&&!vis[x][y])return true;
    return false;
}
void dfs(int count)
{
    if(count==n*4&&flag){ans++;return;}
    if(count>=4*n)return;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=4;j++)
        {
            if(judge(i,j)&&judge(i,j+1)&&judge(i+1,j)&&judge(i+1,j+1)&&!flag)
            {
                flag=1;
                vis[i][j]=vis[i][j+1]=vis[i+1][j]=vis[i+1][j+1]=1;
                dfs(count+4);
                flag=0;
                vis[i][j]=vis[i][j+1]=vis[i+1][j]=vis[i+1][j+1]=0;
            }
            if(judge(i,j)&&judge(i,j+1))
            {
                vis[i][j]=vis[i][j+1]=1;
                dfs(count+2);
                vis[i][j]=vis[i][j+1]=0;
            }
            if(judge(i,j)&&judge(i+1,j))
            {
                vis[i][j]=vis[i+1][j]=1;
                dfs(count+2);
                vis[i][j]=vis[i+1][j]=0;
            }
            if(judge(i,j))
            {
                vis[i][j]=1;
                dfs(count+1);
                vis[i][j]=0;
                return;
            }
        }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ans=flag=0;
        memset(vis,0,sizeof(vis));
        scanf("%d",&n);
        dfs(0);
        printf("%d\n",ans);
    }
    return 0;
}


FZU 2107 Hua Rong Dao 递归回溯

标签:

原文地址:http://blog.csdn.net/crescent__moon/article/details/44984951

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!