【hihocoder 1317】搜索四·跳舞链

【题目链接】:http://hihocoder.com/problemset/problem/1317

【题意】

【题解】

dfs就能过吧.
在选取的时候;
把选取的这一行,占据的列,列的权值+1;
按列搜;
在搜第col列的时候,前i-1列保证有且只有一个行占据着
如果选择的某一行,在前col-1列有格子;则不能选它;
否则修改这一列以及这一列后面的列的权值;
在搜某一列的时候,如果发现它的权值为1;则不用搜了;
如果权值大于1,直接返回上层;

【Number Of WA

0

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 110;

int t;
int f[N][N],n,m,pre[N][N],a[N][N];

bool dfs(int col,int dep)
{
    if (col>m) return true;
    if (f[dep][col]==1) return dfs(col+1,dep);
    else
        if (f[dep][col]>1) return false;
    rep1(i,1,n)
        if (a[i][col] && !pre[i][col])
        {
            rep1(j,col,m)
                f[dep+1][j]=f[dep][j]+a[i][j];
            if (dfs(col+1,dep+1)) return true;
        }
    return false;
}

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
    cin >> t;
    while (t--)
    {
        cin >> n >> m;
        ms(f,0),ms(pre,0);
        rep1(i,1,n)
        {
            int t = 0;
            rep1(j,1,m)
            {
                pre[i][j] = t;
                cin >> a[i][j];
                t |= a[i][j];
            }
        }
        if (dfs(1,1))
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626358.html