Fzu problem 2107 Hua Rong Dao (playing table Dfs AH)

Source: Internet
Author: User

Title Link: http://acm.fzu.edu.cn/problem.php?pid=2107


Problem Description

Cao Cao was hunted under thousands of enemy soldiers when he escaped from Hua Rong Dao. Assuming Hua Rong Dao is a narrow aisle (one n*4 rectangle), while the Cao Cao can be regarded as one 2*2 grid. Cross General can be regarded as one 1*2 grid. Vertical General can regarded as one 2*1 grid. Soldiers can regarded as one 1*1 grid. Now Hua Rong Dao are full of people and no grid is empty.

There is the only one Cao Cao. The number of cross general, vertical general, and soldier are not fixed. How many ways can all the people stand?

Input

There is a single integer t (t≤4) on the first line of the test data indicating, that there is t test cases.

Then for each case, only one integer-N (1≤n≤4) in a-line indicates the length of Hua Rong Dao.

Outputfor each test case, print the number of ways all the people can stand with a single line. Sample Input212 Sample Output018 Hint

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

Source "Higher Education Cup" the third Fujian University student Program Design Competition
Test Instructions:

Give N (1≤n≤4) and then put a rectangle on an n * 4 lattice, must put a 2*2 (for the background of the Devil), and then the remaining position to use three kinds of rectangular stitching .

Ps:

Enumerate first Caocao position, and then use backtracking to enumerate all possible, calculated totals.

Play the Table code: (From: http://www.shangxueba.com/jingyan/1818854.html)

#include <stdio.h> #include <string.h>const int N = 10;const int d[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};con St int Dir[3][3][2] = {{{0, 1}, {0, 0}}, {{1, 0}, {0, 0}}, {{0, 0}}};const int cnt[3] = {2, 2, 1};int R, V[n][n], tmp;const int c = 4;bool isinsert (int k, int x, int y) {for (int i = 0; i< cnt[k]; i++) {int p = x + dir[        K][i][0], q = y + dir[k][i][1];        if (p<= 0 | | p >r) return false;        if (q<= 0 | | q >c) return false;    if (V[p][q]) return false; } return true; void Clear (int k, int x, int y, int t) {for (int i = 0; i< cnt[k]; i++) {int p = x + dir[k][i][0], q = y        + dir[k][i][1];    V[P][Q] = t;    }}void dfs (int x, int y) {if (y >c) x = x + 1, y = 1;        if (x = = R + 1) {tmp++;    Return    } if (V[x][y]) Dfs (x, y + 1);            for (int i = 0; i< 3; i++) {if (Isinsert (i, x, y)) {Clear (I, X, Y, 1);            DFS (x, y + 1); ClEar (i, x, y, 0);    }}}int find (int x, int y) {memset (V, 0, sizeof (v));    for (int i = 0; i< 4; i++) V[x + d[i][0]][y + d[i][1]] = 1;    TMP = 0;    DFS (1, 1); return TMP;}    int solve () {int ans = 0;        for (int i = 1; i< r; i++) {for (int j = 1; j< C; j + +) {ans + = find (i, j); }} return ans;    int main () {int t[10];    for (r = 1; r<= 4; r++) {T[r] = solve ();    } int cas, n;    scanf ("%d", &cas);        while (cas--) {scanf ("%d", &n);    printf ("%d\n", T[n]); } return 0;}


The code is as follows:

#include <cstdio>int main () {    int t;    int n;    scanf ("%d", &t);    while (t--)    {        scanf ("%d", &n);        if (n==1)            puts ("0");        else if (n==2)            puts ("+");        else if (n==3)            puts ("284");        else if (n==4)            puts ("4862");    }    return 0;}



Fzu problem 2107 Hua Rong Dao (playing table Dfs AH)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.