ACM: FZU 2107 Hua Rong Dao - DFS - 暴力
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
Sample Input
2
1
2
Sample Output
0
18
Hint
Here are 2 possible ways for the Hua Rong Dao 2*4.
/*/
题意:
华容道,N*4的格子,N=[1,4],一个曹操 2*2 有 2*1、1*2和1*1的士兵,问有多少中放法。 一开始不知道自己怎么想的,看一下数据这么小,而且1,2,3很容易就推出来了,以为可以推出来,【MDZZ】结果在N==4这种情况下爆炸了。。 下面进入正题。直接DFS暴力。 如果怕超时可以打表,实际上不会。 AC代码:
/*/
#include"algorithm"
#include"iostream"
#include"cstring"
#include"cstdlib"
#include"cstdio"
#include"string"
#include"vector"
#include"stack"
#include"queue"
#include"cmath"
#include"map"
using namespace std;
typedef long long LL ;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FK(x) cout<<"["<<x<<"]\n"
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(x))
#define bigfor(T) for(int qq=1;qq<= T ;qq++)
int n;
int ans,flag;
bool vis[10][10]; bool check(int x,int y) {
if(x<0||y<0||x>=n||y>=4||vis[x][y])return 0;
return 1;
} void DFS(int x) {
if(x==4*n&&flag==1) {
ans++;
return ;
}
if(x>=4*n) {
return ;
}
for(int i=0; i<4; i++) {
for(int j=0; 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(x+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(x+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(x+2);
vis[i][j]=vis[i][j+1]=0;
}
if(check(i,j)) {
vis[i][j]=1;
DFS(x+1);
vis[i][j]=0;
return ;
}
}
}
}
//int _ans[5]={0,0,18,284,4862};
int main() {
int T;S
scanf("%d",&T);
bigfor(T) {
scanf("%d",&n);
memset(vis,0);
flag=0;
ans=0;
DFS(0);
printf("%d\n",ans);
}
return 0;
}
ACM: FZU 2107 Hua Rong Dao - DFS - 暴力的更多相关文章
- FZU 2107 Hua Rong Dao(dfs)
Problem 2107 Hua Rong Dao Accept: 318 Submit: 703 Time Limit: 1000 mSec Memory Limit : 32768 KB Prob ...
- FZU 2107 Hua Rong Dao(暴力回溯)
dfs暴力回溯,这个代码是我修改以后的,里面的go相当简洁,以前的暴力手打太麻烦,我也来点技术含量.. #include<iostream> #include<cstring> ...
- fzu 2107 Hua Rong Dao(状态压缩)
Problem 2107 Hua Rong Dao Accept: 106 Submit: 197 Time Limit: 1000 mSec Memory Limit : 32768 K ...
- 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 ...
- ACM: Gym 100935G Board Game - DFS暴力搜索
Board Game Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Gym 100 ...
- hdu 5612 Baby Ming and Matrix games(dfs暴力)
Problem Description These few days, Baby Ming is addicted to playing a matrix game. Given a n∗m matr ...
- hdu 1010 Tempter of the Bone(dfs暴力)
Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...
随机推荐
- linux下常见解压缩命令
linux下常见的压缩文件格式有tar.gz.tar.gz.tar.bz2.zip等等.对于不同的压缩文件格式有对应的解压缩命令.下面就对此小结一下: 1.后缀为.tar 用 tar –xvf 解压 ...
- Java 数据库操作类
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...
- IoC/DI基本思想的演变
---------------------------------------------------------------------------------- (1)IoC/DI的概念 IoC ...
- 反射的一些用法(WP8.1下)
我初步的理解:反射就是动态调用(dll)类. 比如某个dll有一个类,通过反射就可以知道它里面属性.方法,就可以实现调用. 确实,dll可以直接引用,但是如果遇到这种情况: 添加.删除功能同属一个Dl ...
- mysql登录报错“Access denied for user 'root'@'localhost' (using password: YES”的处理方法
使用/etc/mysql/debian.cnf文件中[client]节提供的用户名和密码: 文件内容: [client]host = localhostuser = debian-sys-maint ...
- PHP 的异常处理、错误处理:error_reporting,try-catch,trigger_error,set_error_handler,set_exception_handler,register_shutdown_function
一.错误.异常 等级常量表 error:不能在编译期发现的运行期错误,比如试图用 echo 输出一个未赋值的变量,这类问题往往导致程序或逻辑无法继续下去而需要中断: exception:程序执行过程中 ...
- 【重点】Shell入门教程:流程控制(2)条件判断的写法
第三节:条件判断的写法 if条件判断中,if的语法结构中的“条件判断”可以有多种形式.测试结果是真是假,就看其传回的值是否为0. 条件测试的写法,有以下10种: 1.执行某个命令的结果 这里的命令,可 ...
- javascript数据结构-介绍
github博客地址 名词解释 数据结构是计算机存储.组织数据的方式.数据结构是指相互之间存在一种或多种特定关系的数据元素的集合.通常情况下,精心选择的数据结构可以带来更高的运行或者存储效率.数据结构 ...
- HahsRouter hash 路由
无刷新页面,切换视图,用hash 实现路由切换,本身附带history记录,简单舒服. 最近用vue,看到vue-route的路由,做单页应用切换视图真心易如反掌,分分钟爽到不行.为了加深理解其内涵原 ...
- [解决WebClient或HttpWebRequest首次连接缓慢问题]
[编程环境]Visual Studio 2010, NET4.0 [开发语言]C#, 理论上VB.NET等依赖.NET Framework框架的语言均受此影响 [问题描述] 使用HttpWebRequ ...