Zoj 1002-fire net answer (Backtracking Method)

Source: Internet
Author: User

Bytes ---------------------------------------------------------------------------------------------------------------------------

Strictly speaking, this question is not what I did. Because I couldn't help searching for the online materials for this question after wa, andCodeAfter the AC.

Bytes ----------------------------------------------------------------------------------------------------------------------------

Zoj 1002: Fire net)

Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 2

In a city composed of a maximum of 4*4 grids, each grid may be "wall" (expressed by 'X') and "street" (expressed '. 'indicates ). Now, a bunker is placed in the street. Each bunker can be fired in the upper, lower, and left directions, with an infinite range of bullets. Walls can block bullets. Ask the maximum number of bunker blocks that can be placed so that they are not mutually destroyed.

For example, enter:

. X ..
....
XX ..
....

The maximum number of outputs is 5.

This question is very similar to the post-N problem, so we can use the Backtracking Method to search for the entire solution space tree. At first I used the greedy method, but wa, the reason is that the greedy method is characteristic that the obtained solution is not necessarily the overall optimal solution. Therefore, we still need to use the Backtracking Method to search for the entire spatial tree. Like the post-N solution, canput Is A discriminant function to determine whether the position (x, y) can be placed in a bunker.

The following is the code I wrote, but in general I have referencedArticle(The code is essentially the same ):

Http://www.cnblogs.com/phinecos/archive/2008/09/18/1293017.html

 

Code_1002_fire_net
/* Zoj 1002-fire net */
/* The maximum value of N is 4. Search for the spatial tree using backtracing. */
# Include < Stdio. h >

char map [ 4 ] [ 5 ];
int maxcount; /* maximum number of bunker storages */

/*Test whether the position (x, y) can be placed in the bunker*/
IntCanput (IntN,IntX,IntY)
{
/*Because the rows and columns are incremental, you only need to search up and left for whether there is a bunker.*/
IntI;

/* 0 indicates that it cannot be occupied. */
I = X;
While (I > 0   && Map [I - 1 ] [Y] ! = ' X ' )
If (Map [ -- I] [Y] = ' O ' ) Return   0 ;

/* Up */
I = Y;
While (I > 0   && Map [x] [I - 1 ] ! = ' X ' )
If (Map [x] [ -- I] = ' O ' ) Return   0 ;

/* Can be placed */
Return   1 ;
}

/* Find the largest bunker tree. n is the city size, and K is the one-dimensional length distance from the start point. */
Void Search ( Int N, Int K, Int Curcount)
{
Int X, Y;
If (K = N * N)
{
/* Arrived the bottom */
If (Curcount > Maxcount)
Maxcount = Curcount;
Return ;
}
Else
{
X = K / N;
Y = K % N;
If (Map [x] [Y] = ' . '   && Canput (n, x, y ))
{
/* If this point can be placed, it will enter this branch */
Map [x] [Y] = ' O ' ; /* Put a houseblock */
Search (n, k + 1 , Curcount + 1 );
/* Rollback */
Map [x] [Y] = ' . ' ;
}
/* To the next branch */
Search (n, k + 1 , Curcount );
}
}

Int Main ()
{
Int N, I, count;
While (Scanf ( " % D " , & N) ! = EOF && N > 0 )
{
For (I = 0 ; I < N; I ++ )
Scanf ( " % S " , Map [I]);

Maxcount = 0 ;
Search (n, 0 , 0 );
Printf ( " % D \ n " , Maxcount );
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.