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);
        }
    
    }
    

      

  • 相关阅读:
    迭代器和生成器
    函数嵌套
    页面调用dll
    C++MFC之picture control控件铺满图片
    C++中去掉string字符串中的 等
    C++之map使用
    C++之条形码,windows下zint库的编译及应用(二)
    C++之条形码,windows下zint库的编译及应用(一)
    C++通过HTTP请求Get或Post方式请求Json数据(转)
    从长字符串中获取想要的字符串
  • 原文地址:https://www.cnblogs.com/Hyouka/p/5790889.html
Copyright © 2011-2022 走看看