zoukankan      html  css  js  c++  java
  • POJ1321 棋盘问题(dfs)

    题目链接

    分析:

    用 dfs 一行一行的搜索,col记录当前列是否已经放置。

    AC代码如下:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <string>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cstring>
    #include <queue>
    
    using namespace std;
    
    const int maxn = 10;
    
    int n, k, cnt;
    bool col[maxn];
    char G[maxn][maxn];
    
    void dfs(int row, int num) {
        if(num == k) { cnt++; return ; }
    
        if(row+1 > n) return;
    
        for(int j=0; j<n; j++) {
            if(G[row][j] == '#') {
                if(!col[j]) {
                    col[j] = true;
                    dfs(row+1, num+1);
                    col[j] = false;
                }
            }
        }
    
        dfs(row+1, num);
    }
    
    int main() {
    
        while(scanf("%d%d", &n, &k) == 2) {
          if(n == -1 && k == -1) break;
    
          memset(col, false, sizeof(col));
    
          for(int i=0; i<n; i++) {
              scanf("%s", G[i]);
          }
    
          cnt = 0;
    
          dfs(0, 0);
    
          printf("%d
    ", cnt);
        }
    
        return 0;
    }
  • 相关阅读:
    59
    58
    57
    56
    55
    54
    53
    转 Using $.ajaxPrefilter() To Configure AJAX Requests In jQuery 1.5
    jquery用正则表达式验证密码强度
    什么是高内聚、低耦合?(转载)
  • 原文地址:https://www.cnblogs.com/tanhehe/p/3227969.html
Copyright © 2011-2022 走看看