ZOJ1002 Fire Net

直接暴力搜索即可~

#include<bits/stdc++.h>
using namespace std;
const int maxn=1014;
char s[maxn][maxn];
int n,ans;
int judge (int n,int m) {
    for (int i=n-1;i>=0;i--) {
        if (s[i][m]=='O') return 0;
        if (s[i][m]=='X') break;
    }
    for (int j=m-1;j>=0;j--) {
        if (s[n][j]=='O') return 0;
        if (s[n][j]=='X') break;
    }
    return 1;
}
void dfs (int k,int num) {
    int x,y;
    if (k==n*n) {
        if (num>ans) ans=num;
        return;
    }
    else {
        x=k/n;
        y=k%n;
        if (s[x][y]=='.'&&judge(x,y)) {
            s[x][y]='O';
            dfs (k+1,num+1);
            s[x][y]='.';
        }
        dfs (k+1,num);
    }
}
int main () {
    while (~scanf ("%d",&n)&&n) {
        ans=0;
        for (int i=0;i<n;i++) {
            getchar ();
            for (int j=0;j<n;j++)
            scanf ("%c",&s[i][j]);
        }
        dfs (0,0);
        printf ("%d\n",ans);
    }
    return 0;
}

 

posted @ 2020-02-12 22:21  zlc0405  阅读(99)  评论(0编辑  收藏  举报