poj1321 棋盘问题 DFS深度优先搜索+剪枝

这几天一直在做一些水的DFS,直接上代码:

#include
#include
#include
#include
#include
#include
#include
#define For(i, n, m) for(i = n;i <= m; ++i)
#define Fod(i, n, m) for(i = n;i >= m; --i)

using namespace std;

const int maxn = 8;

char map[maxn][maxn];
int p[maxn];
int ans, n, m;

void dfs(int x, int y) {
    int i, j;
    For(i, 0, n - 1) {
        if(map[x][i] == '#' && p[i] == 0) {
            if(y == 1)
                ++ans;
            else {
                p[i] = 1;
                For(j, x + 1, n - y + 1)
                    dfs(j, y - 1);
                p[i] = 0;
            }
        }
    }
}

int main() {
    int j, i;
    while(1) {
        ans = 0;
        scanf("%d%d", &n, &m);
        if(n == -1 && m == -1)
            return 0;
        For(i, 0, n - 1)
            For(j, 0, n - 1)
                cin>>map[i][j];
        For(i, 0, n - 1)
            p[i] = 0;
        For(i, 0, n - m)
            dfs(i, m);
        cout<return 0;
}

你可能感兴趣的:(dfs,poj)