Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7922 Accepted Submission(s):
4514
A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening.
Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.
The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through.
The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways.
Your task is to write a program that, given a description of a map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration.
#include<stdio.h> #include<string.h> #define maxn(x,y)(x>y?x:y) char map[10][10]; int n,m,max,tot; int judge(int r,int c) { int i; if(map[r][c]!='.') return 0;//如果当前位置不是“.”直接不能放置 for(i=r-1;i>=0;i--)//判断上方是否已经放置 { if(map[i][c]=='o')//如果已经放置则要返回0 return 0; if(map[i][c]=='X') break; } for(i=c-1;i>=0;i--)//判断左端是否已经放置 { if(map[r][i]=='o') return 0; if(map[r][i]=='X') break; } return 1; } void dfs(int x,int y,int tot) { int i; if(x==n&&y==0) max=maxn(max,tot);//如果整个图遍历完毕则看当前所走路径是否可放置最多的碉堡 else { for(i=y;i<n;i++) { if(!judge(x,i))//如果当前点不可以放置 continue; //则判断此行的下一个点 map[x][i]='o';//可以放置就标记下来 dfs(x,i+1,tot+1);//并进行此行后边点的判断 map[x][i]='.';//回溯回来,清楚标记 } dfs(x+1,0,tot);//如果上一行已经遍历完毕,开始遍历下一行 } } int main() { int i; while(scanf("%d",&n),n) { for(i=0;i<n;i++) scanf("%s",map[i]); max=0; dfs(0,0,0); printf("%d\n",max); } return 0; }