An Algorithm Similar to the NPC in the ZTE Campus Program Design Competition

Source: Internet
Author: User
Tags gety

Question:

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 said to Ivanov, "you know, the north-south and east-west roads in front of the grassland are staggered. Now, there are Russian dolls made of pure gold at the intersection, which are of varying sizes and can be lightly loaded. 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 indicates the number of East-West roads, and C indicates the total number of north-south roads. Next, the R row contains a positive integer (or 0) W [R, C] indicates the weight of a Russian doll placed at the intersection of the East-West Road of Article R and the north-south road of Article C (or indicates that no doll is placed ).
Output:
The output path can have the greatest gains.

Suppose 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

Suppose 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

Note:
1) Start from <0, 0>;
2) The route cannot be repeated;
3) The final return to the starting point is not required.

 

 

Answer:// The comments part is the auxiliary test code <br/> Import Java. io. bufferedreader; <br/> Import Java. io. file; <br/> Import Java. io. filenotfoundexception; <br/> Import Java. io. filereader; <br/> Import Java. io. ioexception; <br/> Import Java. io. printwriter; <br/> Import Java. util. arraylist; <br/> Import Java. AWT. *; <br/> Import Java. AWT. event. actionevent; <br/> Import Java. AWT. event. actionlistener; <br/> Import Java. AWT. event. windowadapter; <br /> Import Java. AWT. event. extends wevent; </P> <p> class myframe extends frame {<br/> Boolean start = false; <br/> string = "Result :"; <br/> Panel = NULL; <br/> textarea text = NULL; <br/> button = NULL; <br/> textarea text1 = NULL; <br/> panel ppanel = NULL; <br/> myframe () {<br/> super ("content written into input.txt:"); <br/> setbounds (200,200,300,400 ); <br/> setvisible (true); <br/> // setresizable (false); <br/> addwindow Listener (New windowadapter () {<br/> Public void windowclosing (invalid wevent e) {<br/> setvisible (false); <br/> system. exit (-1); <br/>}< br/>); <br/> setlayout (New borderlayout ()); <br/> Panel = new Panel (); <br/> panel. setlayout (New flowlayout (); <br/> text = new textarea (20, 60); <br/> text. setvisible (true); <br/> text. requestfocus (); <br/> panel. add (text); <br/> button = new button ("Submit"); <br/> Button. addactionlistener (New actionlistener () {<br/> Public void actionreceivmed (actionevent e) {<br/> string STR = text. gettext (); <br/> printwriter out = NULL; <br/> try {<br/> out = new printwriter (new file ("input.txt ")); <br/> out. write (STR); <br/> out. close (); <br/> Start = true; <br/>} catch (filenotfoundexception E1) {<br/> e1.printstacktrace (); <br/>}< br/>); <br/> button. setback Ground (color. pink); <br/> panel. add (button); <br/> List list = new list (6, false); <br/> list. add ("1. Enter the data you want to write into the input.txt file as follows; otherwise, an exception may be thrown"); <br/> list. add ("2. note that there should be no blank rows between rows of Data "); <br/> list. add ("3. but there can be one or more spaces between data in the same row "); <br/> list. add ("4. enter the data below and click submit on the right to see the best pick-up path in the result "); <br/> list. add ("5. if the data you provided is incorrect, there is no change in the result. Please turn it off and restart the program to enter the correct data "); <br/> list. add ("5 5"); <br/> list. add ("1 16 15 14 13"); <br/> list. add (" 2 17 24 23 12 "); <br/> list. add ("3 18 25 22 11"); <br/> list. add ("4 19 20 21 10"); <br/> list. add ("5 6 7 8 9"); <br/> list. makevisible (200); <br/> ppanel = new Panel (); <br/> ppanel. setlayout (New borderlayout (); <br/> ppanel. add (list, "center"); <br/> Add (ppanel, "North"); <br/> Add (panel, "center "); <br/> text1 = new textarea (string, 5, 60); <br/> Add (text1, "South"); <br/> pack (); <br/>}</P> <p> pub LIC class cross {<br/> int X; // number of East-West roads <br/> int y; // Number of north-south roads <br/> int EW [] []; <br/> arraylist <integer> Al = new arraylist <integer> (); // It is used to save a modifiable route for picking dolls, when recursion is called, The last doll is continuously modified to ensure that all possible routes are traversed cyclically. <br/> arraylist <integer> al1 = new arraylist <integer> (); // stores the current optimal route for picking up a doll. The last element stores the total weight of the current route. <br/> arraylist <integer> Al = new arraylist <integer> (); // It is used to traverse all the routes of the doll. If the total weight of the doll obtained by this route is greater than the total weight of the current optimal route of the doll, move all elements in Al 2 In al1, at the same time, Al 2 clears </P> <p> Cross (int x, int y) {<br/> This. X = x; <br/> This. y = y; <br/> al1.add (New INTEGER (0); // avoid subsequent exceptions <br/> ew = new int [x] [Y]; // define a two-dimensional array with X rows and Y columns, the value of each element corresponds to the weight of the Russian doll at the intersection of the East-West Road and the north-south road <br/>/* For (INT I = 0; I <ew. length; I ++) {<br/> for (Int J = 0; j <EW [X]. length; j ++) {<br/> // assign a value to each element of the Two-dimensional array <br/>}< br/>}*/</P> <p >}</P> <p> int getx () {<br/> return X; <br/>}</P> <p> int Gety () {<br/> return y; <br/>}</P> <p> // Public static string STR = ""; <br/> Public static void rushget (int I, Int J, cross c) {<br/> C. al. add (integer. valueof (C. EW [I] [J]); <br/>/* <br/> * When the upper, lower, and lower boundaries are not exceeded, when the weight of the upper left and lower right dolls is greater than the weight of the newly picked dolls (or the weight of the upper left and right dolls is 0 ), you can continue to pick up in the corresponding direction <br/> */</P> <p> If (C. EW [I] [J] = 0) {// when the current doll weight is 0, the weight of the next picked doll must be greater than that of the largest currently picked doll <br/> int max = 0; <br/> If (C. al. size () = 1) {<br/> max = 0; <br/>}< br/> else {<br/> for (INT v = C. al. size ()-2; V> = 0; V --) {// obtain the weight of the largest doll currently picked in the Al container <br/> If (C. al. get (v) = 0) {<br/> If (C. al. size () = 2) {<br/> max = 0; <br/>}< br/> else <br/> continue; <br/>}< br/> else {<br/> max = C. al. get (V); // obtain the current largest doll weight in container Al <br/> break; <br/>}</P> <p> If (J + 1 <C. gety () & (c. EW [I] [J + 1]> max | C. EW [I] [J + 1] = 0) {// pick up the doll right <br/> rushget (I, j + 1, C ); <br/>}< br/> If (I + 1 <C. getx () & (c. EW [I + 1] [J]> max | C. EW [I + 1] [J] = 0) {// pull down the doll <br/> rushget (I + 1, J, C ); <br/>}< br/> If (I-1> = 0) & (c. EW [I-1] [J]> max | C. EW [I-1] [J] = 0) {// pick up doll up <br/> rushget (I-1, J, C ); <br/>}< br/> If (J-1> = 0) & (c. EW [I] [J-1]> max | C. EW [I] [J-1] = 0) {// pick up the doll from the left <br/> rushget (I, J-1, C ); <br/>}</P> <p> else {<br/> If (J + 1 <C. gety () & (c. EW [I] [J + 1]> C. EW [I] [J] | C. EW [I] [J + 1] = 0) {// pick up the doll right <br/> rushget (I, j + 1, C ); <br/>}< br/> If (I + 1 <C. getx ()) & (C. EW [I + 1] [J]> C. EW [I] [J] | C. EW [I + 1] [J] = 0) {// pick up the doll down <br/> rushget (I + 1, J, C ); <br/>}< br/> If (I-1> = 0) & (c. EW [I-1] [J]> C. EW [I] [J] | C. EW [I-1] [J] = 0) {// pick up doll up <br/> rushget (I-1, J, C ); <br/>}< br/> If (J-1> = 0) & (c. EW [I] [J-1]> C. EW [I] [J] | C. EW [I] [J-1] = 0) {// pick up the doll from the left <br/> rushget (I, J-1, C ); <br/>}< br/> int Total = 0; <br/> for (INT h = 0; H <C. al. size (); H ++) {<br/> // STR + = C. al. get (h ). intvalue () + ""; <Br/> total + = C. al. get (h); <br/> C. al2.add (C. al. get (h); <br/> // system. out. print (C. al. get (h ). intvalue () + ""); <br/>}< br/> C. al2.add (New INTEGER (total); // place the total weight of All dolls picked in the current route to the last element in the Al 2 Container <br/> If (C. al2.get (C. al2.size ()-1 ). intvalue ()> C. al1.get (C. al1.size ()-1 ). intvalue () {// if the total weight of the doll obtained from the pick-up route is greater than the total weight of the current optimal pick-up Route, move all the elements in Al 2 to al1, at the same time, the virtual IP address is cleared. <br/> C. al1.clear (); <br/> for (INT f = 0; F <C. al2.size (); F ++) {<Br/> C. al1.add (C. al2.get (f); <br/>}< br/> C. al2.clear (); <br/> C. al. remove (C. al. size ()-1); // Replace the last doll to be picked up in the current route. <br/> // STR + = "/N "; <br/> // system. out. println (); <br/>}</P> <p> Public static void main (string ARGs []) {<br/> // cross c = new cross (); <br/> // int EW [] [] = {, 11 }, {, 10 };< br/> // cross c = New Cross (5, 5 ); <br/> // int EW [] [] = {, 15, 14, 13}, {, 24, 23, 12}, {, 11}, {, 20, 21, 10}, {, 9 }}; <br/> myframe frame = new myframe (); <br/> while (true) {<br/> If (frame. start = true) {<br/> bufferedreader in = NULL; // define a read stream <br/> Cross c = NULL; <br/> try {<br/> In = new bufferedreader (New filereader ("input.txt"); // read data from the input.txt file <br/>} catch (filenotfoundexception E) {<br/> E. printstacktrace (); <br/>}< br/> string STR = ""; <br/> string str1 =" "; <Br/> int A = 0; <br/> int B = 0; <br/> try {<br/> If (STR = in. readline ())! = NULL) {<br/> STR = Str. trim (); // remove the spaces on both sides of the string <br/> string STRs [] = Str. split ("[// s] +"); // Splits a string into a character array STRs using one or more spaces as separators <br/> A = integer. parseint (STRs [0]); <br/> B = integer. parseint (STRs [1]); <br/>}< br/> C = New Cross (a, B); <br/> for (INT n = 0; (str1 = in. readline ())! = NULL; n ++) {<br/> If (n <A) {<br/> str1 = str1.trim (); // remove spaces on both sides of the string <br/> string STRs [] = str1.split ("[// s] + "); // In this program, "[// s] +" is a regular expression that matches one or more spaces <br/> for (INT m = 0; m <B; M ++) {<br/> C. EW [N] [m] = integer. parseint (STRs [m]); // assign <br/>}< br/>} catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/> // C. ew = ew; <br/> rushget (0, 0, c); <br/> // system. out. print (STR); <br/> string result = "Result:"; <br/> int nn = 0; <br/> for (int g = 0; G <C. al1.size ()-1; G ++) {<br/> system. out. print (C. al1.get (g ). intvalue () + ""); // finally print the optimal pick-up Route <br/> NN ++; <br/> If (NN % 20 = 0) {<br/> result + = C. al1.get (g ). intvalue () + "/N"; <br/>}< br/> else <br/> result + = C. al1.get (g ). intvalue () + ""; <br/>}< br/> frame. text1.settext (result); <br/>}</P> <p >}< br/>

Here, I hope you can give us some advice on algorithms. If you have good algorithms, You can exchange them and learn about O (algorithm _ knowledge) O ~

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.