zoukankan      html  css  js  c++  java
  • 哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级) B 小乐乐搭积木 【DFS || 状压dp】

    链接:https://ac.nowcoder.com/acm/contest/301/B
    来源:牛客网

    小乐乐想要给自己搭建一个积木城堡。
    积木城堡我们假设为n*m的平面矩形。
    小乐乐现在手里有1*2,2*1两种地砖。
    小乐乐想知道自己有多少种组合方案。
    第一行输入整数n,m。(1<=n,m<=10)
     

    解题思路:

    ①DFS:
    按照格子按顺序编号,每号格子有 两种方案,遍历两种方案,同时在地图上标记那些格子被覆盖,如果格子被覆盖则继续搜下一个格子。
     
    AC code:
     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <cmath>
     6 #define INF 0x3f3f3f3f
     7 using namespace std;
     8 const int MAXN = 11;
     9 bool mmp[MAXN][MAXN];
    10 int S, N, M;
    11 int ans;
    12 
    13 bool check(int x, int y, int tp)
    14 {
    15     if(tp == 0){        //1*2的地砖
    16         if(y+1 == M) return false;
    17         if(!mmp[x][y+1]) return true;
    18     }
    19     else{               //2*1的地砖
    20         if(x+1 == N) return false;
    21         if(!mmp[x+1][y]) return true;
    22     }
    23     return false;
    24 }
    25 
    26 void dfs(int no)
    27 {
    28     if(no == S){ ans++; return;}
    29     int rx = no/M, ry = no%M;
    30     if(mmp[rx][ry]) return dfs(no+1);
    31     mmp[rx][ry] = true;
    32     for(int i = 0; i <= 1; i++){
    33         if(i == 0){
    34             if(check(rx, ry, i)){
    35                 mmp[rx][ry+1] = true;
    36                 dfs(no+1);
    37                 mmp[rx][ry+1] = false;
    38             }
    39         }
    40         else{
    41             if(check(rx, ry, i)){
    42                 mmp[rx+1][ry] = true;
    43                 dfs(no+1);
    44                 mmp[rx+1][ry] = false;
    45             }
    46         }
    47     }
    48     mmp[rx][ry] = false;
    49     return;
    50 }
    51 
    52 int main()
    53 {
    54     scanf("%d%d", &N, &M);
    55     S = N*M;
    56     dfs(0);
    57     printf("%d
    ", ans);
    58     return 0;
    59 }
  • 相关阅读:
    基于摸板匹配的目標跟蹤算法
    spoj 2713 Can you answer these queries IV
    zoj 3633 Alice's present
    hdu 3642 Get The Treasury
    poj 1195 Mobile phones
    poj 2760 End of Windless Days
    zoj 3540 Adding New Machine
    spoj 1716 Can you answer these queries III
    spoj 1043 Can you answer these queries I
    spoj 2916 Can you answer these queries V
  • 原文地址:https://www.cnblogs.com/ymzjj/p/10052325.html
Copyright © 2011-2022 走看看