Original (Russian doll Program)

Source: Internet
Author: User

I spent one afternoon writing code, and I am also learning program design for everyone to exchange and learn, so that they can promote learning and progress !!

  1. Russian doll prize

At the competition, Ivanov became a new "Grassland e" and won great honors for the tribe. The leader decided to have a lot of rewards. He told Ivanov: "child
Son, you know. In front of the grassland, the north-south and east-west roads are staggered. Now there are Russian dolls made of pure gold at the intersection, which are of varying sizes and weight.
Can be installed with light. You can fly along the road and pick up the dolls at the intersection. The requirement is that you must be a doll at any time. After installation, you cannot take it apart. Be sure not to repeat the path ."




Please plan the route for Ivanov so that he can have the greatest gains.



Input:
Cross.txt








The input includes multiple groups of test cases;







Each test case starts with an integer <R,
C>


, R


Number of East-West roads, c


Represents the total number of north-south roads; next, R


Line, each line includes C


Positive Integers (or 0


) W [R, C]


, Indicating the r


East-West roads and section C


The weight of a Russian doll placed at the intersection of a north-south road (or indicated that no doll was placed ).



Output:








The output path can have the greatest gains.




Hypothesis 1


:




Cross.txt





2 7





1 2 13 6 7
12 11





14 3 4 5 8
9 10





Output:



1
2 3 4 5 6 7 8 9 10 11 12




Hypothesis 2


:




Cross.txt




5 5





1 16 15 14 13





2 17 24 23 12





3 18 25 22 11





4 19 20 21 10





5 6 7 8
9




Output:



1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

 

The following is my idea for doing this. You are welcome to discuss and add:

1. First, start from the upper left corner and start from "1". Find the adjacent nodes that can go, and both 2 and 16 can go. Then, load 1, 2, and 1, 16 into two queues respectively. Then

The queues 1, 2, and 1, 16 are added to the end of a larger queue list.

 

2. Retrieve the first queue in the list queue and retrieve the queue 1 and 2 (after obtaining the queue, the list queue will no longer contain 1 and 2 queues ). Then, the last element 2 of the queue 1 and 2 is taken out. Find the adjacent nodes in the vicinity of 2, which are 17,3. (1 is not there because it has already passed) and the adjacent nodes are pushed to the end of the queue. Then we have two queues: 1, 2, 17, 2, and 3. We then press the two queues into the end of the list queue.

 

3. Repeat the second step to retrieve the sub-queue in the list queue. If the sub-queue can no longer find any adjacent node, we can consider this as a path. However, we do not know whether it is the optimal path. Therefore, we need to maintain an optimal path. If the importance obtained from this sub-path is greater than the optimal path, we can update this sub-path to the optimal path.

Finally, when the list queue no longer contains a sub-queue, We have traversed all the accessible paths. The optimal path we maintain is the result we need.

 

 

The following is the source code:

# Include <iostream>
# Include <set>
# Include <vector>
# Include <cstdio>
# Include <list>
# Include <fstream>
# Include <cstdlib>

Using namespace STD;

Typedef struct
{
Int row;
Int Col;
Int weight;
} Wawa;

Typedef struct
{
Int row;
Int Col;
} Size;

// Compare two struct Wawa, if equal return 0
// If A> B, return a positive number
// Else B> A, return a negative number
Int CMP (Wawa A, Wawa B)
{
If (A. Row = B. Row & A. Col = B. col) return 0;
If (A. Row> B. Row | (A. Row = B. Row & A. Col> B. col) return 1;
If (A. Row <B. Row | (A. Row = B. Row & A. Col <B. col) Return-1;
}

// This function is used to read all the useful
// Numbers in the text file, the path can be modified
// By users with text file in a different path
Void inputall (vector <Wawa> & item, size & sz)
{
Ifstream in ("/home/huyi/desktop/myprogram/Wawa/cross1.txt ");
Char Buf [10];
Int row, Col, weight;
Char ch;
Int I, J;

Wawa ad;

If (! In)
{
Cerr <"error opening the file" <Endl;
}

In. Get (BUF, sizeof (BUF ),'');
Row = atoi (BUF); // get the total number of rows
In. Getline (BUF, sizeof (BUF ));
Col = atoi (BUF); // get the total number of Columns

SZ. Row = row;
SZ. Col = Col;

For (I = 0; I <row; I ++) // The whole is used to get the row Value
{//, The column and weight value from the text
For (j = 0; j <col-1; j ++) // file, then push then to a vector.
{
In. Getline (BUF, sizeof (BUF ),'');
Ad. Weight = atoi (BUF );
Ad. Row = I;
Ad. Col = J;
Item. push_back (AD );
}
In. Getline (BUF, sizeof (BUF ));
Ad. Weight = atoi (BUF );
Ad. Col = J;
Item. push_back (AD );
}
}

// In this function, we donot know the weight value in
// The "Neighbor" Wawa struct, so I write this function in
// Order to get the weight value by iterating the Vector
Void getweight (vector <Wawa> & item, Wawa & neighbor)
{
For (INT I = 0; I <item. Size (); I ++)
{
If (CMP (item [I], neighbor) = 0)
Neighbor. Weight = item [I]. weight;
}
}

// Check if the "Neighbor" Wawa struct is in the list <Wawa>
// If it is in, return true;
// Else return false
Bool checklist (list <Wawa> & wawapicked, Wawa & neighbor)
{
List <Wawa >:: const_iterator Pos;

For (Pos = wawapicked. Begin (); pos! = Wawapicked. End (); ++ POS)
{
If (CMP (* POs, neighbor) = 0) return true;
}
Return false;
}

// This function is used to find the neighbors of a particle
// Waawa struct. And then put all the neighbor Wawa struct
// A vector called "neighbors"
Void findneighbors (list <Wawa> & wawapicked, Wawa & last, vector <Wawa> & neighbors, size & SZ, vector <Wawa> & item)
{
Wawa ngbor;
If (last. Row <Sz. Row-1)
{
Ngbor. Row = last. Row + 1;
Ngbor. Col = last. Col;
Getweight (item, ngbor );
If (! Checklist (wawapicked, ngbor) & ngbor. weight> last. Weight)
Neighbors. push_back (ngbor );
}

If (last. Row> 0)
{
Ngbor. Row = last. Row-1;
Ngbor. Col = last. Col;
Getweight (item, ngbor );
If (! Checklist (wawapicked, ngbor) & ngbor. weight> last. Weight)
Neighbors. push_back (ngbor );
}

If (last. Col <Sz. col-1)
{
Ngbor. Row = last. row;
Ngbor. Col = last. Col + 1;
Getweight (item, ngbor );
If (! Checklist (wawapicked, ngbor) & ngbor. weight> last. Weight)
Neighbors. push_back (ngbor );
}

If (last. Col> 0)
{
Ngbor. Row = last. row;
Ngbor. Col = last. col-1;
Getweight (item, ngbor );
If (! Checklist (wawapicked, ngbor) & ngbor. weight> last. Weight)
Neighbors. push_back (ngbor );
}
}

// Compute the whole weight of a list containing Wawa struct.
// And add all the weight. then return the sum
Int computeweight (list <Wawa> & listtmp)
{
Int weight;
List <Wawa >:: iterator Pos;
For (Pos = listtmp. Begin (); pos! = Listtmp. End (); ++ POS)
Weight + = pos-> weight;
Return weight;
}

// This is the core function of find the best way which has the biggest
// Weight value. In this function I use a breadth-first-Search Algorithm
// The "carriedwawa" list is used to store all the possible paths.
// And the "finalpath" is used to store the path which currently has
// Biggest weight
Bool findbestway (vector <Wawa> & item, list <Wawa> & carriedwawa, size & SZ, list <Wawa> & finalpath, Int & maxweight)
{
If (carriedwawa. Empty () return true; // if the whole paths we have searched, we can return and we has found the best way

List <Wawa> listtmp = carriedwawa. Front ();
Carriedwawa. pop_front ();
Wawa wawatmp = listtmp. Back ();
Vector <Wawa> neighbors;
Int Weight = 0;

Findneighbors (listtmp, wawatmp, neighbors, SZ, item); // find all neighbors which we has gone to before
If (neighbors. Size () = 0) // if the neighbors doesnot exist, it means we get a new path
{// Then we compare to the "finalpath", if it has bigger weight
Weight = computeweight (listtmp); // than "finalpath", then we update "finalpath" to the current
If (weight> maxweight) // path
{
Finalpath. Clear ();
Maxweight = weight;
Finalpath = listtmp;
}
Return false;
}
 
For (INT I = 0; I <neighbors. Size (); I ++)
{
Listtmp. push_back (neighbors [I]);
Carriedwawa. push_back (listtmp );
Listtmp. pop_back ();
}
Return false;
}

// This is the main function, with all the initial values,
// I think it's a bit complicate. If you really want to figure
// Out what is going on in the whole program, findbestway Function
// Is the most critical one.
Int main ()
{
List <Wawa> carriedwawa;
List <Wawa> finalpath;
List <list <Wawa> Init;
Vector <Wawa> item;
Int maxweight = 0;
Size SZ;
Inputall (item, SZ );
Carriedwawa. push_back (item [0]);
Init. push_back (carriedwawa );

While (! Findbestway (item, init, SZ, finalpath, maxweight ));

Cout <"the final path is" <Endl;

List <Wawa >:: iterator Pos;
For (Pos = finalpath. Begin (); pos! = Finalpath. End (); ++ POS)
Cout <pos-> weight <"";
Cout <Endl;
}

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.