zoukankan      html  css  js  c++  java
  • foj Problem 2107 Hua Rong Dao

    Problem 2107 Hua Rong Dao

    Accept: 503    Submit: 1054
    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

    2 1 2

    Sample Output

    0 18

    Hint

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

    Source

    “高教社杯”第三届福建省大学生程序设计竞赛 
     
    题意:搜索多少种布阵方式,一定要有曹操。
    思路:回溯dfs。
    AC代码:
    #define _CRT_SECURE_NO_DEPRECATE
    #include <iostream>
    #include<vector>
    #include<algorithm>
    #include<cstring>
    #include<bitset>
    #include<set>
    #include<map>
    #include<cmath>
    #include<queue>
    using namespace std;
    #define N_MAX 7
    #define MOD 1000000007
    #define INF 0x3f3f3f3f
    typedef long long ll;
    int n, k;
    bool vis[N_MAX][N_MAX],cc;
    int ans = 0;
    void dfs(int x,int y) {
        if (x==n) {//搜索结束
            if (cc)ans++;//有曹操
            return;
        }
        int xx=x, yy=y+1;//下次要去的点
        if (yy == 4) {
            xx++, yy = 0;
        }
        
        if (vis[x][y])dfs(xx, yy);
        else {
            for (int cs = 0; cs < 4;cs++) {
                if (cs == 0&&!cc) {
                    if (x + 1 < n&&y + 1 < 4 && !vis[x][y] && !vis[x + 1][y] && !vis[x][y + 1] && !vis[x + 1][y + 1]) {
                        cc = true;
                        vis[x][y] = vis[x + 1][y] = vis[x][y + 1] = vis[x + 1][y + 1] = true;
                        dfs(xx,yy);
                        vis[x][y] = vis[x + 1][y] = vis[x][y + 1] = vis[x + 1][y + 1] = false;
                        cc = false;
                    }
                }
                if (cs == 1) {
                    if (x + 1 < n && !vis[x][y] && !vis[x + 1][y]) {
                        vis[x][y] = vis[x + 1][y] = true;
                        dfs(xx,yy);
                        vis[x][y] = vis[x + 1][y] = false;
                    }
                }
                if (cs == 2) {
                    if (y + 1 < 4 && !vis[x][y] && !vis[x][y + 1]) {
                        vis[x][y] = vis[x][y + 1] = true;
                        dfs(xx, yy);
                        vis[x][y] = vis[x][y + 1] = false;
                    }
                }
                if (cs == 3) {
                    if (!vis[x][y]) {
                        vis[x][y] = true;
                        dfs(xx, yy);
                        vis[x][y] = false;
                    }
                }
            }
        }
    }
    
    int main() {
        int t; cin >> t;
        while(t--){
            memset(vis, 0, sizeof(vis)); cc = 0; ans = 0;
            cin >> n;
            dfs(0, 0);
        cout << ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Dev C++ 工程没有调试信息 解决办法
    写一个函数,输入一个二叉树,树中每个节点存放了一个整数值,函数返回这棵二叉树中相差最大的两个节点间的差值绝对值。请注意程序效率。
    oracle10g登录em后,提示“java.lang.Exception: Exception in sending Request :: null”
    网站登录的破解
    sql 日志恢复
    Oracle expdp/impdp 使用示例
    Oracle数据库备份和恢复的基本命令
    检索 COM 类工厂中 CLSID 为 {{10020200-E260-11CF-AE68-00AA004A34D5}} 的组件时失败解决办法
    win7访问部分win2003速度慢
    公交车路线查询系统后台数据库设计--换乘算法改进与优化
  • 原文地址:https://www.cnblogs.com/ZefengYao/p/9158821.html
Copyright © 2011-2022 走看看