Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall.
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.
The input file contains one or more map descriptions, followed by a line containing the number 0 that signals the end of the file. Each map description begins with a line containing a positive integer n that is the size of the city; n will be at most 4. The next n lines each describe one row of the map, with a '.' indicating an open space and an uppercase 'X' indicating a wall. There are no spaces in the input file.
For each test case, output one line containing the maximum number of blockhouses that can be placed in the city in a legal configuration.
Sample input:
4 .X.. .... XX.. .... 2 XX .X 3 .X. X.X .X. 3 ... .XX .XX 4 .... .... .... .... 0
Sample output:
5 1 5 2 4
ZOJ 1002
摆碉堡,碉堡之间不能互相攻击
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 struct linjiejuzhen 5 { 6 int xiabiao; 7 int keyigongcun[16]; 8 }; 9 void inpt();//输入数据 10 void creat(struct linjiejuzhen *p);//建立互相之间能不能打到的邻接矩阵 11 void read(struct linjiejuzhen *p,int mark[],int num);//递归地搜索互相之间不会攻击的碉堡 12 struct linjiejuzhen head[16]; 13 char a[17];//地形图,行数由n记录 14 int n;//长宽 15 int longest;//最大值 16 int main() 17 { 18 int i,j; 19 int mark[16]; 20 do 21 { 22 scanf("%d",&n); 23 if(n==0) 24 { 25 break; 26 } 27 longest=0; 28 inpt(); 29 for(i=0;i<n*n;i++) 30 { 31 head[i].xiabiao=i; 32 } 33 for(i=0;i<n*n;i++) 34 { 35 creat(&head[i]); 36 } 37 for(i=0;i<n*n;i++) 38 { 39 for(j=0;j<n*n;j++) 40 { 41 mark[j]=1; 42 } 43 if(a[i]!='X') 44 read(&head[i],mark,1); 45 } 46 printf("%d\n",longest); 47 }while(n!=0); 48 return 0; 49 } 50 void inpt() 51 { 52 char b[5]; 53 int i; 54 for(i=0;i<17;i++) 55 { 56 a[i]='\0'; 57 } 58 for(i=0;i<n;i++) 59 { 60 scanf("%s",b); 61 strcat(a,b); 62 } 63 } 64 void creat(struct linjiejuzhen *p) 65 { 66 int i,j; 67 int x; 68 x=p->xiabiao; 69 for(i=0;i<n*n;i++) 70 { 71 p->keyigongcun[i]=0; 72 } 73 if(a[x]!='X') 74 { 75 for(j=0;j<n*n;j++) 76 { 77 if(j!=x&&a[j]!='X') 78 { 79 p->keyigongcun[j]=1; 80 } 81 } 82 } 83 i=x-1; 84 while(i>=(x/n)*n) 85 { 86 if(a[i]=='.') 87 { 88 p->keyigongcun[i]=0; 89 } 90 else if(a[i]=='X') 91 { 92 break; 93 } 94 i--; 95 } 96 i=x+1; 97 while(i<(x/n+1)*n) 98 { 99 if(a[i]=='.') 100 { 101 p->keyigongcun[i]=0; 102 } 103 else if(a[i]=='X') 104 { 105 break; 106 } 107 i++; 108 } 109 i=x-n; 110 while(i>=0) 111 { 112 if(a[i]=='.') 113 { 114 p->keyigongcun[i]=0; 115 } 116 else if(a[i]=='X') 117 { 118 break; 119 } 120 i-=n; 121 } 122 i=x+n; 123 while(i<n*n) 124 { 125 if(a[i]=='.') 126 { 127 p->keyigongcun[i]=0; 128 } 129 else if(a[i]=='X') 130 { 131 break; 132 } 133 i+=n; 134 } 135 } 136 /*搜索可以共存的碉堡 137 通过记录每一个 138 已经记录的碉堡的不能共存的碉堡*/ 139 void read(struct linjiejuzhen *p,int mark[],int num) 140 { 141 int ed[16]; 142 int i; 143 int flag=0; 144 for(i=0;i<n*n;i++) 145 { 146 ed[i]=mark[i]; 147 } 148 for(i=0;i<n*n;i++) 149 { 150 if(p->keyigongcun[i]==0) 151 { 152 ed[i]=0; 153 } 154 } 155 for(i=0;i<n*n;i++) 156 { 157 if(ed[i]==1) 158 { 159 flag=1; 160 break; 161 } 162 } 163 if(flag==0) 164 { 165 if(num>longest) 166 { 167 longest=num; 168 } 169 } 170 for(i=0;i<n*n;i++) 171 { 172 if(ed[i]==1) 173 { 174 read(&head[i],ed,num+1); 175 } 176 } 177 }
/*只学了C
有很多地方可以优化
感觉自己写的好繁琐*/
//数学模型没好好想,一开始搜索方法不对
/*既然互相不能攻击就是一个搜索分支的任何点都不能互相攻击
那么应该每记录一点后都把新产生的攻击点“添加”进不能摆的点的记录中
下次搜索时便不记录这个点*/
浙公网安备 33010602011771号