HDU 1045

http://acm.hdu.edu.cn/showproblem.php?pid=1045

#include <bits/stdc++.h>
using namespace std;
int n;
char ch[10][10];
int dp[4][2] = {-1,0,1,0,0,-1,0,1};
int ans;
int check(int x,int y){
    if(ch[x][y] != '.') return 0;
    for(int i = 0; i < 4; i++){
        int dx = x + dp[i][0];
        int dy = y + dp[i][1];
        while(true){
            if(dx < 0 || dx >= n || dy < 0 || dy >= n || ch[dx][dy] == 'X' )
                break;//这里不是return 0
            else if(ch[dx][dy] == '1')
                return 0;
            dx += dp[i][0];
            dy += dp[i][1];
        }
    }
    return 1;
}
void dfs(int x){
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(check(i,j)){
                ch[i][j] = '1';
                dfs(x + 1);
                ch[i][j] = '.';
            }
        }
    }
    if(ans < x)
        ans = x;
}
int main(){
    //freopen("in","r",stdin);
    ios::sync_with_stdio(0);
    while(cin >> n){
        if(!n) break;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++)
                cin >> ch[i][j];
        }
        ans = 0;
        dfs(0);
        cout << ans << endl;
    }
    return 0;
}

 

posted @ 2020-04-11 18:22  Hazelxcf  阅读(147)  评论(0编辑  收藏  举报