标签:

4 .X.. .... XX.. .... 2 XX .X 3 .X. X.X .X. 3 ... .XX .XX 4 .... .... .... .... 0
5 1 5 2 4
题意:与八皇后类似,炮台不能存在于同行同列,除非中间隔墙
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
char map[10][10];
int n;
int sum;
bool check(int x, int y){
if(map[x][y] != '.') return false;
for(int i = x - 1; i >= 0; --i){//向上搜索
if(map[i][y] == '@') return false;
if(map[i][y] == 'X') break;
}
for(int i = y - 1; i >= 0; --i){//向左搜索
if(map[x][i] == '@') return false;
if(map[x][i] == 'X') break;
}
return true;
}
void dfs(int x, int y, int ans){
if(x == n) sum = max(ans, sum);
else if(y == n) dfs(x + 1, 0, ans);
else{
for(int i = y; i < n; ++i){
if(check(x, i)){
map[x][i] = '@';//放置大炮
dfs(x, i + 1, ans + 1);
map[x][i] = '.';//回溯
}
}
dfs(x + 1, 0, ans);
}
}
int main (){
while(scanf("%d", &n), n){
for(int i = 0; i < n; ++i){
scanf("%s", map[i]);
}
sum = 0;
dfs(0, 0, 0);
printf("%d\n", sum);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/hpuhjh/article/details/47282261