zoukankan      html  css  js  c++  java
  • FZU 2107 Hua Rong Dao DFS

    Hua Rong Dao
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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

    2
    1
    2
    

    Sample Output

    0
    18
    

    Hint

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

     
    题意:华容道宽度为4 长为n
    一定要塞一个2*2的曹操 其他随便怎么放 要放满 问有多少种方法
     
     
    一开始想着是排列组合  后来发现想多了
    观摩了菊花的代码后写了一发
    就是暴力搜索一下
    不打表也不会超时
     
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <iomanip>
    #include <math.h>
    #include <map>
    using namespace std;
    #define FIN     freopen("input.txt","r",stdin);
    #define FOUT    freopen("output.txt","w",stdout);
    #define INF     0x3f3f3f3f
    #define INFLL   0x3f3f3f3f3f3f3f
    #define lson    l,m,rt<<1
    #define rson    m+1,r,rt<<1|1
    typedef long long LL;
    typedef pair<int,int> PII;
    
    int n;
    int ans, flag;
    int vis[10][10];
    
    bool check(int x, int y){
       return x >= 1 && x <= n && y >= 1 && y <= 4 && vis[x][y] != 1;
    }
    
    void dfs(int cnt){
       if(cnt == 4 * n && flag == 1){
           ans ++;
           return ;
       }
       if(cnt >= 4 * n)  return ;
       for(int i = 1; i <= n; i ++){
           for(int j = 1; j <= 4; j ++){
               if(check(i, j) && check(i + 1, j) && check(i, j + 1) && check(i + 1, j + 1) && !flag){
                   vis[i][j] = vis[i + 1][j] = vis[i][j + 1] = vis[i + 1][j + 1] = 1;
                   flag = 1;
                   dfs(cnt + 4);
                   vis[i][j] = vis[i + 1][j] = vis[i][j + 1] = vis[i + 1][j + 1] = 0;
                   flag = 0;
               }
               if(check(i, j) && check(i + 1, j)){
                   vis[i][j] = vis[i + 1][j] = 1;
                   dfs(cnt + 2);
                   vis[i][j] = vis[i + 1][j] = 0;
               }
               if(check(i, j) && check(i, j + 1)){
                   vis[i][j] = vis[i][j + 1] = 1;
                   dfs(cnt + 2);
                   vis[i][j] = vis[i][j + 1] = 0;
               }
               if(check(i, j)){
                   vis[i][j] = 1;
                   dfs(cnt + 1);
                   vis[i][j] = 0;
                   return ;
               }
           }
       }
    }
    
    int main()
    {
        //FIN
        int T;
        scanf("%d", &T);
        while(T--){
           memset(vis, 0, sizeof(vis));
           scanf("%d", &n);
           ans = 0;
           flag = 0;
           dfs(0);
           printf("%d
    ", ans);
        }
    
    }
    

      

  • 相关阅读:
    2.Liunx 系统设置
    1.Liunx 文件管理
    Liunx 更新环境时用到的命令
    解决SSH Secure Shell 连接Liunx 有乱码情况。
    JMeter 性能测试基本过程及示例(4)
    在 macOS 中怎样获取当前文件夹的路径?
    Mac环境安装启动jmeter
    StringUtils工具类常用方法汇总1(判空、转换、移除、替换、反转)
    Json与Gson
    Quartz的基本使用之入门(2.3.0版本)
  • 原文地址:https://www.cnblogs.com/Hyouka/p/5790889.html
Copyright © 2011-2022 走看看