动态规划状态压缩-小乐乐堆积木

题目链接:https://ac.nowcoder.com/acm/contest/301/B

题目描述:

 

 

 

代码实现:

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4  
 5 const int dx[]={0,1},
 6           dy[]={1,0}; 
 7 bool vis[15][15];
 8 int n,m,ans;
 9  
10 void solve(int num)
11 {
12     if(num==n*m)
13     {
14         ans++;
15         return;
16     }
17     int x,y,x1,y1;
18     x=(num-1)%n+1,y=(num-1)/n+1;
19     if(vis[x][y])
20     {
21         solve(num+1);
22         return;
23     }
24     for(int i=0;i<=1;i++)
25     {
26         x1=x+dx[i],y1=y+dy[i];
27         if(x1<=0||x1>n||y1<=0||y1>m||vis[x1][y1]) continue;
28         vis[x][y]=vis[x1][y1]=1;
29         solve(num+1);
30         vis[x][y]=vis[x1][y1]=0;
31     }
32 }
33  
34 int main()
35 {
36     while(cin>>n>>m)
37     {
38         ans=0;
39         memset(vis,0,sizeof(vis));
40         if(n*m%2)
41         {
42             cout<<0<<endl;
43             continue;
44         }
45         solve(1);
46         cout<<ans<<endl;
47     }
48     return 0;
49 }

 

posted @ 2018-12-05 09:20  里昂静  阅读(186)  评论(0编辑  收藏  举报