POJ 1321 题解

没什么卵用的目录

  • 题目地址
  • AC代码
  • 题解和题目思路

题目地址

这是地址

AC代码

#include <iostream>
#include <cstring>

using namespace std;

int list[8][8];
long long int counter;

bool tell(int x,int y,int n){
    for (int i = 0; i < n; ++i) {
        if(list[i][y]==2&&i!=x)return false;
    }
    for (int j = 0; j < n; ++j) {
        if(list[x][j]==2&&j!=y)return false;
    }
    return true;
}

void dfs(int n,int k,int x,int y,int now){
    int nx = (x+1)%n;
    int ny = y+(x+1)/n;
    if(x<n&&y<n) {
        if (list[x][y] == 0)dfs(n, k, nx, ny, now);
        else if (list[x][y] == 1 && tell(x, y, n)) {
            list[x][y] = 2;
            now++;
            if (now == k) {
                counter++;
                list[x][y] = 1;
                now--;
                dfs(n, k, nx, ny, now);
            }else {
                dfs(n, k, nx, ny, now);
                list[x][y] = 1;
                now--;
                dfs(n, k, nx, ny, now);
            }
        }else{
            dfs(n, k, nx, ny, now);
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    int n,k;
    while(cin>>n>>k,n!=-1&&k!=-1){
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                char temp;
                cin>>temp;
                if(temp=='#')list[i][j]=1;
                else list[i][j]=0;
            }
        }
        counter = 0;
        dfs(n,k,0,0,0);
        cout<<counter<<endl;
    }
    return 0;
}

题解和题目思路

这道题就是限制条件特别多的DFS,直接按照题意写就是了,模拟

你可能感兴趣的:(POJ 1321 题解)