FZU - 2107 Hua Rong Dao(回溯)
来源:互联网 发布:135端口还有漏洞吗 编辑:程序博客网 时间:2024/05/22 17:45
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
212
Sample Output
018
Hint
Here are 2 possible ways for the Hua Rong Dao 2*4.
题目大意:
给你一个4*n 的棋盘,上门放有只有1个2*2的曹操,和任意个1*2的将军、或者1*1的小兵,能组成多少种可能。
解析:这是一道回溯问题,要抓住几个关键条件只有1个曹操,所以递归的结束条件为曹操标记为真,和棋盘上格子全满。
还有一个关键点是下一层递归时,如果c == 4 的话,需要转到下一行的第一个进行判断。
#include <stdio.h> #include <string.h> const int N = 4; int cnt; bool king; bool vis[N][N]; int n; void dfs(int r,int c,int sum) { if( sum == 4*n && king ) { cnt++;return ; }if(c == 4) {dfs(r+1,0,sum);return ;}for(int i = r; i < n; i++) {for(int j = c; j < 4; j++) { //2*2 if( !vis[i][j] && !vis[i+1][j] && !vis[i][j+1] && !vis[i+1][j+1] && !king && i+1 < n && j+1 < 4) { vis[i][j] = vis[i+1][j] = vis[i][j+1] = vis[i+1][j+1] = true; king = true; dfs(i,j+1, sum+4); vis[i][j] = vis[i+1][j] = vis[i][j+1] = vis[i+1][j+1] = false; king = false; } //1*2 if( !vis[i][j] && !vis[i][j+1] && j+1 < 4) { vis[i][j] = vis[i][j+1] = true; dfs(i,j+1, sum+2); vis[i][j] = vis[i][j+1] = false; } //2*1 if( !vis[i+1][j] && !vis[i][j] && i+1 < n) { vis[i+1][j] = vis[i][j] = true; dfs(i,j+1, sum+2); vis[i+1][j] = vis[i][j] = false; } //1*1 if( !vis[i][j]) { vis[i][j] = true; dfs(i,j+1, sum+1); vis[i][j] = false; } if( j == 3) { c = 0;} } } } int main() { int t; while( scanf("%d",&t) != EOF) { while( t-- ) { memset(vis,0,sizeof(vis)); king = false; cnt = 0; scanf("%d",&n); dfs(0,0,0); printf("%d\n",cnt); } } return 0; }
- fzu 2107 Hua Rong Dao(回溯)
- FZU - 2107 Hua Rong Dao(回溯)
- FZU - 2107 Hua Rong Dao 回溯
- FZU 2107 Hua Rong Dao 递归回溯
- FZU 2107 Hua Rong Dao (DFS)
- FZU 2107 Hua Rong Dao
- FZU - 2107 Hua Rong Dao
- fzu 2107 Hua Rong Dao
- fzu 2107 Hua Rong Dao(状压dp)
- FZU Problem 2107 Hua Rong Dao (打表 dfs)
- fzu 2107 Hua Rong Dao(状态压缩)
- FZU 2107 Hua Rong Dao(dfs)
- FZU 2107F - Hua Rong Dao
- FZU 2107 Hua Rong Dao 轮廓线上的动态规划
- FZU 2107 Hua Rong Dao_暴力dfs
- FOJ FZU 2017 Hua Rong Dao【DFS+打表】
- FZU2107:Hua Rong Dao(DFS)
- JOJ 2724 hua rong dao (最小割 拆点)[无代码]
- matlab中 fprintf 和disp的用法
- 内存管理问题的总结(最强王者组:胡鹏飞 孙晓萌 张西辉 王振波)
- ③如能再活一辈子
- 高通平台mipi panel kernel读包方法
- hdu 4619
- FZU - 2107 Hua Rong Dao(回溯)
- 浅析 Linux 初始化 init 系统,第 1 部分: sysvinit
- Centos7.0的几个新特性
- ViewHolder模式超简洁写法
- sqlite3 表里插入系统时间(时间戳)
- hdu 1150
- Data Types in the Kernel <LDD3 学习笔记>
- uva 10461 Difference
- 为Hero穿衣服