03:40:42 accepted 1505 C ++ 00:00. 63 1648 K tiaotiao
It is now 03:45 am. This victory is really hard to come by. When I am about to collapse, my eyes are suddenly bright ~ AC! It's really a time to go back to the road... :) next I will go into the subject. The problem solved this time is that when time and space are quite difficult, I used the legendary tedious two-way search and wrote it once in high school, later, I thought this thing was a useless vase like a clue binary tree and never touched it again .. I couldn't think of writing two-way and wide-searching, but I also learned a lot of skills.
Zju1505 Solitaire
Question:
On an 8*8 board, fourSameEach time, you can move one of them up and down to the left and right; if there is a barrier, you can also skip a piece. according to such a moving rule, ask if you can move from a given checkerboard status to another status within 8 steps. time Limit: 10 s
Analysis:
The question seems very clear, and a typical wide search model. deep Search is also very convenient if it is convenient to determine the weight. however, after careful consideration, although there are only eight steps, the number of statuses is quite large. if you do not consider determining the weight, you can select four pieces in each step. Each piece can have four directions, that is, there are 16 possibilities for each step, and there are 16 ^ 8 types of 8 steps in total, that is, about 4*10 ^ 9 types... think about it .. | therefore, the task with heavy weight is very important. there is also a trick to express the status of each board. because the pawns are the same, the Four pawns have eight coordinates in total, so I used to sort the Four pawns by coordinate and convert them into an int type. however, according to the consistent weighting method, we need to open an array about 9*10 ^ 8... so we lost our heavy-duty solution again ..
So I searched the internet and found someone using the legendary two-way search. because each layer of the search can be expanded quite quickly, if two-way search is used, the search will be completed as long as the branches on both sides have intersections. Otherwise, the search on both sides only needs to be extended by Layer 4! In this case, if you do not consider the weight, the total number of extended nodes is 2*16 ^ 4, which is about 13*10 ^ 4, which is completely acceptable! The space problem is solved !. However, I still have some concerns in terms of time. But in this case, I can't care so much about it. I suddenly went out for a fight. I didn't expect it to be an AC, and the time was 0.63s, so I was quite satisfied.
In the process of Bidirectional wide search, we encountered such issues.
1. How do I search in turn?
You do not need to write the code twice. the two BFs arrays are A and B. The included Cursor Is Pa, QA, Pb, and QB. when the front node is expanded, the and B names are exchanged, and the p and q cursors are exchanged. how to exchange a and B? Hey, use A0 and B0 to open the array, and declare a and B as the pointer type!
2. How to determine the weight? And judge whether the search is complete?
The most stupid method I use. because each time a is used as the current array and B is the array of the other party, the weight is scanned in all States before; to determine the intersection, scan all the States before B...> _ <|
3. How can I skip a pawn?
In fact, whether to skip the pawns is only related to the current status. Therefore, this problem can be well solved by adding a judgment.
4. How do I deal with 'within 8 step '?
When the number of nodes to be expanded in a certain direction is already four steps, the number of nodes to be expanded after the start is also four steps. therefore, nodes in this direction do not need to be extended. so now we only need to expand the nodes of the other party. during implementation, you can use a flag to indicate whether the direction can be switched.
Summary:
Direct wide search and deep search fail, and the greatest difficulty lies in the large number of States, so it cannot be directly hashed. by using Bidirectional wide search, the search layers are reduced by half. Reducing one layer at a very fast speed can also bring unexpected benefits. in general, two-way wide-search saves a lot of status compared to classic wide-search, but it is a waste of time to judge the intersection.