"Daily Learning" "Iterative deepening Search + hashing" codevs1004 four sub-link

Source: Internet
Author: User

Reprint please specify the source [ametake All rights reserved]http://blog.csdn.net/ametake

Title Description Description

On a 4*4 board placed 14 pieces, of which there are 7 white pieces, 7 black pieces, two blank areas, any one black and white pieces can be moved up and down four direction to the adjacent space, this is called a step, black and white sides alternately moves, either party can go first , If at some point a piece of any color is formed into four lines (including a slash), the position is the target.

0
0 0
0 0
0 0

Enter a description input Description
Read from the file into a 4*4 of the initial game, black pieces with B, white pieces with W, the space zone with O is indicated.
outputs description output Description

Moves the number of steps to the target game with a minimum number of steps.

sample input to sample

Bwbo
Wbwb
Bwbw
Wbwo

Sample output Sample outputs

5

After six hours of hard work, this topic finally AC ... Look at the response, Mr. Liu hours not an example, there are a few, ah, even Huang also spent one hours. However, if you encounter this problem on the Noip, I am not allowed to debug to the rest of the dish? So, it is better to improve the code, to avoid the details of the error.

Yes, the topic seems simple, but the details are very much and need careful attention.

Let's talk a little bit about ideas!


A look of course is a direct search, then the use of deep search or wide search it? Usually, we consider the wide search, because this problem requires a minimum number of steps, and it is difficult to prune, deep search effect. So why do we use iterative deepening search? The first thing to consider is that the wide search may explode space (this problem does not explode space, but often use iterative deepening is because the broad search may go beyond space, and the iterative deepening is essentially a time-changing strategy, its complexity is much higher than the deep search, according to the full binary tree to calculate a higher, but less space consumption). And we are here to consider the main: black and white pieces are likely to go first, if the wide search I personally may achieve difficulties, because to alternate team. This problem I successfully write ID, in a sense is luck, id than wide search faster than a lot of probably also with the data, positive solution should also be wide search.


We searched each stage (that is, the number of steps) two times, the first black chess, the second white chess in advance. Each search is cycled two times, because there are two spaces.


One of the inspirations of this problem is that you do not have to pass the array as a parameter to the function, just as a global variable, and backtrack after each modification. I passed two parameters, one is the current layer, in fact, if my good this does not have to pass, but the pass will be good to write some, the other is this step to go black chess or white, it is not difficult to see, this can also not pass. That is to say, what parameters are not passed or not.


How to determine if the target status is reached? Refer to the Huang long procedure:

BOOL Equ (int &a1,int &a2,int &a3,int &a4) {    if (a1!=a2| | a2!=a3| | a3!=a4| | A4!=A1) return 0;return 1;} inline bool Check () {for     (int i=0;i<4;i++)        {             if (equ (e.a[i][0],e.a[i][1],e.a[i][2],e.a[i][3])) return 1;             if (Equ (E.a[1][i],e.a[2][i],e.a[3][i],e.a[0][i])) return 1;         }     if (Equ (e.a[1][1],e.a[2][2],e.a[3][3],e.a[0][0])) return 1;     if (Equ (e.a[0][3],e.a[1][2],e.a[2][1],e.a[3][0])) return 1;     return 0;}
Here, the coordinates of the Huang is 1-4, and I am 0-3, but it really pits me. = = I did not notice at the beginning, forgot to modify, so that one step many times only to discover ...

So how do you tell if the state is repeating? Before we used set, however, according to the Ben Introduction, not open O2 optimized set is half as slow as the hash table (of course, the hash table is just arrays and modulo). So we tried the hash table. This article is "everyday learning", precisely because this is the first time the hash table appears.

The hash code is simple:

</pre> first open a 4000000 bool array hash<pre name= "code" class= "CPP" >inline bool Gethash (const node &e,const int &depth) {int va=0;for (int i=0;i<4;i++) {for (int j=0;j<4;j++) {va=va*3+e.a[i][j];}} Va%=3733799;if (Hash[va])) return False;hash[va]=true;return true;}

Above is the hash table I originally wrote. This code to the wide search can be used directly, but the iteration deepened no! Why is it?

Consider a situation in which the depth limit is 5 in a direction, we find this state in the fourth step, hash it, and then find that there is no positive solution in this direction. We went in another direction and found that the second step went to that state, but it was already being played, so we skipped. But in fact, after three steps from this state is the target State, should output 5. What can I do about it?

We carefully observe why this is not the case in the wide search? Because wide search is strictly by layer extension, if this state is found it must be found in the nearest position (second step). So we can set up an array that has the number of hashes that are found in the first few steps. When you encounter this state again, determine if the number of steps that are currently being walked is less than the number of steps saved in the array, and if it is less, we will continue to search down.

The modified code is this:

inline bool Gethash (const node &e,const int &depth) {int va=0;for (int i=0;i<4;i++) {for (int j=0;j<4;j++) {VA =VA*3+E.A[I][J];}} Va%=3733799;if (hash[va]&& (have[va]<=depth)) return False;hash[va]=true;have[va]=depth;return true;

One of the problems that should be noted here is that iterative Deepening search every time the deepening of the hash table should be emptied!!!

This is because the iteration is deepened every time from the first layer to search, if not emptied, the first state in the last search has been hashed, there is no place to search.

So the code:


I know a lot about noon today. OriginLab June, your help really let me very grateful, Baals has been silently dedication, and I sent you this morning to send a private message unexpectedly so quickly received a reply, the introduction of very detailed, really touched. Today we know the electric mules, as well as the electric mule users, a group of people who insist. I find that the mystery of the internet is slowly unfolding, allowing me to see a whole new world. So it is better to learn, to study hard to be a qualified technology house (ˉ"ˉ)


--The remnant tears in the dust, South Hope King Division another year


Copyright NOTICE: Reprint Please specify source [ametake Copyright]http://blog.csdn.net/ametake Welcome to see

"Daily Learning" "iterative deepening search + hash" codevs1004 Four sub-puzzle puzzle

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.