Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register
北京大学《ACM/ICPC大学生程序设计竞赛训练》暑期课面向全球招生!

以行为DFS深度 尝试所放至列 一行可以什么也不放是关键

Posted by 315727400 at 2015-05-30 11:53:53 on Problem 1321
#include <iostream>
#include <cstdio>
int N, cnt, sum;
int X[10];
char M[10][10];
using namespace std;

void DFS(int row, int res){
    if(res == cnt){
        sum ++;
        return;
    }
    if(row > N || res + 1 + (N - row) < cnt) //如果放到row行,即便N-row行都放置仍然不足要求解树,剪枝。
        return;
    DFS(row + 1, res); //这一行不放
    for(int i = 1; i <= N; ++i){
        if(M[row][i] == '#' && !X[i]){
            X[i] = 1;
            DFS(row + 1, res + 1);    //放
            X[i] = 0;
        }
    }

}

int main()
{
    while(scanf("%d%d", &N, &cnt) && N + cnt > 0)
    {
        for(int i = 1; i <= N; ++i){
            char tmp[10];
            X[1] = 0;
            scanf("%s", tmp + 1);
            for(int j = 1; j <= N; ++j)
                M[i][j] = tmp[j];
        }
        sum = 0;
        DFS(1, 0);
        printf("%d\n", sum);
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator