原题描述:
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
可以说是一开始毫无思绪。。。直接判断两个 . 之间有没有X ? 后来看了网上的思路,用的回溯法,搜索算法果然博大精深啊。
回溯思路:(因为当前位置判断只与前面的放置情况有关而与后面的放置情况无关,所以一个一个的回溯)
从第一个位置开始判断,首先判断该位置是否有X , 如果有也就是说该位置不可以放碉堡,那就直接到下一个位置,否则再判断该位置是否可以放置碉堡也就是是否符合题意,如果可以就放并且增加一个碉堡数量值,否则跳到下一个位置,依次循环下去,直到找到最后一个位置。
#include<stdio.h>
char a[ 5 ][ 5 ] ;
int n , max ;
int Judge ( int r , int c ) //测试是否可以把碉堡放在r行c列上。
{
int i ;
// 只需要测试比待测试点小的位置即可,即待测点的左边的点和上面的点。
for ( i = c-1 ; i >= 0 ; i -- )
{
if ( a[ r ][ i ] == 'X' )
break;
if ( a[ r ][ i ] == '0' )
return 0;
}
for ( i = r-1 ; i >= 0 ; i -- )
{
if ( a[ i ][ c ] == 'X' )
break;
if ( a[ i ][ c ] == '0' )
return 0;
}
return 1 ;
}
void Solve ( int k , int curNum ) //k表示第几个位置(一共n*n个),curNum表示当前的总数。
{
int x , y ;
if ( k != n*n ) //还没有搜索至最后一个位置
{
x = k/n ;
y = k%n ;
if( ( a[ x ][ y ] == '.' ) && ( Judge ( x , y ) == 1 ) ) //该位置是空的并且可以放置一个碉堡
{
a[ x ][ y ] = '0' ;
Solve ( k+1 , curNum+1 ) ; //递归进入下一个位置
a[ x ][ y ] = '.' ; //回溯。 ??
}
Solve ( k+1 , curNum ) ;
}
else if ( k == n*n && curNum > max ) //循环到最后一个位置比较max
{
max = curNum ;
return ;
}
}
int main ( )
{
int i , j ;
while ( scanf("%d",&n ) , n )
{
for ( i = 0 ; i < n ; i ++ )
{
getchar( ) ;
for ( j = 0 ; j < n ; j ++ )
scanf("%c",&a[ i ][ j ] );
}
max = 0 ;
Solve ( 0 , 0 ) ; //从左上角第一个位置开始搜索
printf("%d\n",max );
}
return 0;
}
(网上整合的代码)。
果然得好好补补这些基本的数据结构和算法。。。