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

提供两种思路,具体见代码

Posted by Panamera at 2015-01-17 19:37:49 on Problem 3740
一:暴力+剪枝
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N = 3e+2 + 7;
int col[N], m, n;
bool solve()
{
    for (int i = 1; i < 1 << m; ++i)
    {
        int j;
        for (j = 0; j < n; ++j)
        {
            int value = i & col[j];
            if (!value || (value & (value - 1)))
                break;
        }
        if (j >= n)
            return true;
    }
    return false;
}
int main()
{
    while (~scanf("%d%d", &m, &n))
    {
        memset(col, 0, sizeof(int) * n);
        for (int i = 0, tmp; i < m; ++i)
            for (int j = 0; j < n; ++j)
                if (scanf("%d", &tmp) && tmp)
                    col[j] |= 1 << i;
        bool ans = false;
        if (find(col, col + n, 0) == col + n)
            ans = solve(); 
        printf("%s\n", ans ? "Yes, I found it" : "It is impossible");
    }
    return 0;
}
二.dfs + 剪枝
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N = 3e+2 + 7;
int col[N], m, n;
bool check(int have)
{
    for (int i = 0; i < n; ++i)
    {
        int value = have & col[i];
        if (value & (value - 1))
            return false;
    }
    return true;
}
bool dfs(int dep, int have)
{
    if (dep >= n)
        return true;
    int value = have & col[dep];
    if (value)
        return dfs(dep + 1, have);
    for (int i = 0; i < m; ++i)
        if ((col[dep] & (1 << i)) && check(have | (1 << i)) && dfs(dep + 1, have | (1 << i)))
            return true;
    return false;
}
int main()
{
    while (~scanf("%d%d", &m, &n))
    {
        memset(col, 0, sizeof(int) * n);
        for (int i = 0, tmp; i < m; ++i)
            for (int j = 0; j < n; ++j)
                if (scanf("%d", &tmp) && tmp)
                    col[j] |= 1 << i;
        bool ans = false;
        if (find(col, col + n, 0) == col + n)
            ans = dfs(0, 0);
        printf("%s\n", ans ? "Yes, I found it" : "It is impossible");
    }
    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