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

DFS FZU 2107

时间:2014-12-05 21:26:56      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:dfs

Problem 2107 Hua Rong Dao

Accept: 247    Submit: 555
Time Limit: 1000 mSec    Memory Limit : 32768 KB

bubuko.com,布布扣 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?

bubuko.com,布布扣 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.

bubuko.com,布布扣 Output

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

bubuko.com,布布扣 Sample Input

212

bubuko.com,布布扣 Sample Output

018

bubuko.com,布布扣 Hint

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

bubuko.com,布布扣

bubuko.com,布布扣 Source

“高教社杯”第三届福建省大学生程序设计竞赛
//用搜索来记录来统计有多少种情况
#include <stdio.h>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define MAXN 6
int vis[MAXN][MAXN];
int n,m;
int flag;
int ans;

bool f(int x,int y){
    if(x>=n || x<0 || y>=4 || y<0){
        return false;
    }
    return true;
}

void DFS(int con){
    int i,j;
    if(flag==1 && con==n*m){
        ans++;
        //flag = 0;
        return ;
    }
    if(con > n*m){
        return ;
    }
    for(int i=0;i<n;i++){
        for(j=0;j<m;j++){
            if(vis[i][j]==0&&vis[i][j+1]==0&&vis[i+1][j]==0&&vis[i+1][j+1]==0&&f(i,j+1)==true&&f(i+1,j)==true&&f(i+1,j+1)==true && flag==0){
                vis[i][j] = 1;
                flag = 1;
                vis[i][j+1] = 1;
                vis[i+1][j] = 1;
                vis[i+1][j+1] = 1;
                DFS(con+4);
                flag = 0;
                vis[i][j] = 0;
                vis[i+1][j] = 0;
                vis[i][j+1] = 0;
                vis[i+1][j+1] = 0;
            }
            if(vis[i][j]==0&&vis[i][j+1]==0&&f(i,j+1)==true){
                vis[i][j+1] = 1;
                vis[i][j] = 1;
                DFS(con+2);
                vis[i][j+1] = 0;
                vis[i][j] = 0;
            }
            if(vis[i][j]==0&&vis[i+1][j]==0&&f(i+1,j)==true){
                vis[i][j] = 1;
                vis[i+1][j] = 1;
                DFS(con+2);
                vis[i][j] = 0;
                vis[i+1][j] = 0;
            }
            if(vis[i][j] == 0){
                vis[i][j] = 1;
                DFS(con+1);
                vis[i][j] = 0;
                return ;
            }
        }
    }

    //return ;
}

int main(){
    int T;

    while(~scanf("%d",&T)){
        while(T--){
            scanf("%d",&n);
            m = 4;
            memset(vis,0,sizeof(vis));
            ans = 0;
            if(n<2){
                printf("0\n");
                continue;
            }
            flag = 0;
            DFS(0);
            printf("%d\n",ans);
        }
    }

    return 0;
}


DFS FZU 2107

标签:dfs

原文地址:http://blog.csdn.net/zcr_7/article/details/41751535

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