标签:
HDOJ题目地址:传送门

4 .X.. .... XX.. .... 2 XX .X 3 .X. X.X .X. 3 ... .XX .XX 4 .... .... .... .... 0
5 1 5 2 4
题意:问可以放多少个碉堡,碉堡不能在同一行或列,除非有墙隔开,就是两个碉堡在同一行或列的条件是必须有墙隔开
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char Map[5][5];
int res;
int n;
/**
判断是否成立
*/
bool judge(int x,int y){
if(Map[x][y]!='.')return false; //X.S都不可以放
for(int i=x-1;i>=0;i--){//判断点的上方
if(Map[i][y]=='X')break;
if(Map[i][y]=='O')return false;
}
for(int i=y-1;i>=0;i--){//判断点的左方
if(Map[x][i]=='X')break;
if(Map[x][i]=='O')return false;
}
return true;
}
/**
DFS
*/
void DFS(int x,int y,int tot){
if(x==n&&y==0){ //已经搜索了n行,求最大值
res=max(res,tot);
return;
}
if(y==n){ //这一行已经求完,从下一行第一列重新开始
DFS(x+1,0,tot);
return;
}
for(int i=y;i<n;i++){
if(judge(x,i)){
Map[x][i]='O';
DFS(x,i+1,tot+1);
Map[x][i]='.';
}
}
DFS(x+1,0,tot);//上一行搜索结束,从下一行重新开始
}
int main(){
while(scanf("%d",&n),n){
res=0;
for(int i=0;i<n;i++)
scanf("%s",Map[i]);
DFS(0,0,0);
printf("%d\n",res);
}
return 0;
}
ACM--DFS--最大碉堡数--HDOJ 1045--Fire Net
标签:
原文地址:http://blog.csdn.net/qq_26891045/article/details/51517902