FZU 2107 Hua Rong Dao(dfs)
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
",ans);
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.