Mobile Games-search for Spider outlets

Source: Internet
Author: User

When Nokia was used in the past, there was a game of "smart King" in its mobile phone. One of the projects of small intelligence was to study the crawling of SPIDER in many spider networks according to certain rules, then, determine which sequence number the spider crawls out. This model is implemented in C language.

As shown in (the leftmost digit represents the height of the horizontal line ):

Game Rules

The spider crawls down from the line at any of the above numbers. when it encounters a horizontal line, it turns and crawls along the horizontal line. When it climbs to another vertical line, it climbs down along the horizontal line. When it encounters a horizontal line, it turns again... As a result, you can crawl out of the spider's web, and then determine the number of lines that the spider crawls out of the web.

As shown in the following figure, if the spider crawls from 1, it will go through 1-2-3-4-3-2 in sequence, and the final exit is 2.

Use a programming language to implement this function.

Input: 1. Number of spider web bars;

2. Number of spider crosslines;

3. The vertical line number when the spider starts crawling.

4. The number of the starting and ending points of each horizontal line and the position of the horizontal line.

Output: the vertical line number when the spider finally climbs out.

To simplify the model, assume that the input is n, m, and K, indicating the number of spider web vertical lines, the number of horizontal lines, and the number of vertical lines when crawling. The numbers from left to right of the vertical bars are 1, 2, 3, 4 ..... Each horizontal line is determined by three parameters: the starting value XL (the number of the vertical line connected to the left end of the horizontal line) and the ending value XR (the number of the vertical line connected to the right end of the horizontal line), height y (indicating the height from the bottom 0, positive integer ).

For example, if a spider crawls from 1 in the figure above, the corresponding input/output format should be as follows:

Input:

5 9 1

1 2 8

1 2 5

1 2 3

2 3 7

2 3 2

3 4 5

3 4 3

4 5 7

4 5 1

Output: 2

Programming implementation

The most important thing here is to present the model in mathematical language mode according to the above rules.

Here, the M-length arrays XL [], xr [], and Y [] are used to store the parameter information of each horizontal line.

Assume that the first spider crawls down on the vertical line numbered K. If the vertical line has a horizontal line, the element value in the XL or XR array is K, then find the horizontal line closest to the spider. After finding it, if it is an XL element, the spider goes to the vertical line of K + 1. If it is a XR element, then the spider to the vertical line of the K-1, and so on, loop query, each time is to find the highest level than the current height of the spider, if there is no corresponding horizontal line, then the vertical line is the spider's exit.

Based on the above description, you can use an endless loop to determine the crawling path of a spider, locate the exit, and exit the loop.

The Code is as follows:

1 # include <stdio. h> 2 3 int groupnum; // Number of test data groups 4 5 // determine the last exit of the spider 6 void check (int n, int M, int K, int XL [], int XR [], int y [], int * value); 7 8 void check (int n, int M, int K, int XL [], int XR [], int y [], int * value) 9 {10 int Tempy = 1000001; 11 int temp_prey = 1000001; // record the height before this crawl 12 while (1) 13 {14 int step = 0; 15 temp_prey = Tempy; 16 for (INT I = 0; I <m; I ++) 17 {18 if (XL [I] = k) & (Y [I] <temp_prey) 19 {20 if (temp_prey = Tempy) // The first time you encounter a horizontal line 21 {22 Tempy = Y [I]; 23 step = 1; 24} 25 else if (Y [I]> Tempy) 26 {27 Tempy = Y [I]; 28 step = 1; 29} 30} 31 if (XR [I] = K) & (Y [I] <temp_prey) 32 {33 If (temp_prey = Tempy) // The first time you encounter a horizontal line 34 {35 Tempy = Y [I]; 36 step =-1; 37} 38 else if (Y [I]> Tempy) 39 {40 Tempy = Y [I]; 41 step =-1; 42} 43} 44} 45 if (Tempy = temp_prey) 46 {47 * value = K; 48 break; 49} 50 k = K + step; 51} 52} 53 54 int main () 55 {56 int N; // spider net vertical line quantity 57 int m; // spider net horizontal line quantity 58 int K; // The vertical line number 59 int result for the spider to start crawling; // record the result. The last exit of the spider is 60 scanf ("% d", & N, & M, & K); 61 int XL [m]; // record the start X axis of each line 62 int XR [m]; // record the end X axis of each line 63 int y [m]; // record the height of each line 64 for (INT I = 0; I <m; I ++) 65 {66 scanf ("% d", & XL [I], & XR [I], & Y [I]); 67} 68 69 // do not pass the value of the last parameter. You must pass the address to obtain the value of 70 check (n, m, K, XL, xr, y, & result); 71 72 // print result 73 printf ("% d", result); 74 75 return 0; 76}

The core is in the check function. It is not difficult to judge the final exit of a spider, that is, the spider keeps crawling according to the established rules, until the spider can no longer find the turning crossline, it will find the exit.

Test Results

The test result is the same as the actual verification result. Of course, you can also design more complex spider networks for verification.

The verification example in the preceding figure is as follows:

 

 

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.