FZU 2107 Hua Rong Dao(dfs)
Problem 2107 Hua Rong Dao
Accept: 318 Submit: 703
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
暴力搜索
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
int vis[10][10];
int n;
int ans;
bool flag;
int judge(int x,int y)
{
if(x<1||x>n||y<1||y>4||vis[x][y])
return 0;
return 1;
}
void dfs(int count)
{
if(count==n*4&&flag)
{
ans++;
return;
}
if(count>=n*4)
return;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=4;j++)
{
if(judge(i,j)&&judge(i+1,j+1)&&judge(i,j+1)&&judge(i+1,j)&&!flag)
{
flag=1;
vis[i][j]=1;vis[i+1][j+1]=1;vis[i][j+1]=1;vis[i+1][j]=1;
dfs(count+4);
flag=0;
vis[i][j]=0;vis[i+1][j+1]=0;vis[i][j+1]=0;vis[i+1][j]=0;
}
if(judge(i,j)&&judge(i,j+1))
{
vis[i][j]=1;vis[i][j+1]=1;
dfs(count+2);
vis[i][j]=0;vis[i][j+1]=0;
}
if(judge(i,j)&&judge(i+1,j))
{
vis[i][j]=1;vis[i+1][j]=1;
dfs(count+2);
vis[i][j]=0;vis[i+1][j]=0;
}
if(judge(i,j))
{
vis[i][j]=1;
dfs(count+1);
vis[i][j]=0;
return;
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(vis,0,sizeof(vis));
ans=0;
flag=0;
dfs(0);
printf("%d\n",ans);
}
return 0;
}
FZU 2107 Hua Rong Dao(dfs)的更多相关文章
- ACM: FZU 2107 Hua Rong Dao - DFS - 暴力
FZU 2107 Hua Rong Dao Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- fzu 2107 Hua Rong Dao(状态压缩)
Problem 2107 Hua Rong Dao Accept: 106 Submit: 197 Time Limit: 1000 mSec Memory Limit : 32768 K ...
- FZU 2107 Hua Rong Dao(暴力回溯)
dfs暴力回溯,这个代码是我修改以后的,里面的go相当简洁,以前的暴力手打太麻烦,我也来点技术含量.. #include<iostream> #include<cstring> ...
- foj Problem 2107 Hua Rong Dao
Problem 2107 Hua Rong Dao Accept: 503 Submit: 1054Time Limit: 1000 mSec Memory Limit : 32768 K ...
- FZOJ Problem 2107 Hua Rong Dao
...
- ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪
FZU 2150 Fire Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- FZU 2176 easy problem (DFS序+树状数组)
对于一颗树,dfs遍历为每个节点标号,在进入一个树是标号和在遍历完这个树的子树后标号,那么子树所有的标号都在这两个数之间,是一个连续的区间.(好神奇~~~) 这样每次操作一个结点的子树时,在每个点的开 ...
- FZU 2150 Fire Game (bfs+dfs)
Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board ...
- 数据结构实验之图论二:图的深度遍历(SDUT 2107)(简单DFS)
题解:图的深度遍历就是顺着一个最初的结点开始,把与它相邻的结点都找到,也就是一直往下搜索直到尽头,然后在顺次找其他的结点. #include <bits/stdc++.h> using n ...
随机推荐
- 关于Java方法的参数
刚好看到C++的函数这块,说C++中除了引用类型的形参,其他都是实参的副本(个人总结). 隐约记得Java中方法的参数也是这么回事,于是手动测试一番. 结果 Java中方法的参数都是值传递,哪怕是引用 ...
- Spring细粒度控制扫描Bean
接Spring 依赖注入(DI)的注解 <context:component-scan base-package="" resource-pattern="**/* ...
- 不可在 for 循环体内修改循环变量,防止 for 循环失去控制
不可在 for 循环体内修改循环变量,防止 for 循环失去控制. #include <iostream> /* run this program using the console pa ...
- (转)windows下编译最新的x264
二:<windows下编译最新的x264> X264更新的比较快,每天都有更新,但算法模块,基本结构是没有多大变化的.x264都是用C语言写的包括C99,但C99语法是在VC中是没法用的( ...
- 关于多层for循环迭代的效率优化问题
关于多层for循环迭代的效率优化问题 今天笔试的时候遇到这么一道题目 说有上面这么循环嵌套 .问怎么优化 并说明原因. for(int i = 0 ; i < 1000 ;i++){ ...
- CentOS下screen 命令详解
一.背景 系统管理员经常需要SSH 或者telent 远程登录到Linux 服务器,经常运行一些需要很长时间才能完成的任务,比如系统备份.ftp 传输等等.通常情况下我们都是为每一个这样的任务开一个远 ...
- JavaSE(十)之反射
开始接触的时候可能大家都会很模糊到底什么是反射,大家都以为这个东西不重要,其实很重要的,几乎所有的框架都要用到反射,增加灵活度.到了后面几乎动不动就要用到反射. 首先我们先来认识一下对象 学生---- ...
- 使用jquery.uploadify动态传递自己的参数
上传碰到这个问题在上传文件的时候同时上传文件的类型..上网找了半天.总于解决了..分享一下了..直接例子了.. html <%@ page language="java" i ...
- How to Setup Cordova for Windows 7
Setup Cordova Text Editor / IDE You may need to prepare an IDE or Editor for working. Here for examp ...
- (转载)用vs2010开发基于VC++的MFC 串口通信一*****两台电脑同一个串口号之间的通信
此文章以visual C++数据採集与串口通信測控应用实战为參考教程 此文章适合VC++串口通信入门 一.页面布局及加入控件 1, 安装好vs2010如图 2, 新建一个基于VC++的MFC项目com ...