Traverse large arrays or other data structures by range

Source: Internet
Author: User
Imagine a snake game where there is a lot of food on the map. The program needs to check whether the snake head is in conflict with the food (based on the coordinates on the map ). If you use an array to save the food information, you need to traverse this array. But if there is a lot of food (the array is very large), it is unnecessary to traverse it completely, because there is only one... imagine a snake game where there is a lot of food on the map. The program needs to check whether the snake head is in conflict with the food (based on the coordinates on the map ).
If you use an array to save the food information, you need to traverse this array. However, if there are a lot of foods (the array is very large), it is unnecessary to traverse them completely, because only a certain range of foods may crash.
The problem is how to locate the food that may crash without traversing the entire array based on the coordinates.

What I can think of is to use array_filter, but basically it is traversal.
You can also use other data structures (without arrays.
Friends who have played games may have good solutions.

Reply content:

Imagine a snake game where there is a lot of food on the map. The program needs to check whether the snake head is in conflict with the food (based on the coordinates on the map ).
If you use an array to save the food information, you need to traverse this array. However, if there are a lot of foods (the array is very large), it is unnecessary to traverse them completely, because only a certain range of foods may crash.
The problem is how to locate the food that may crash without traversing the entire array based on the coordinates.

What I can think of is to use array_filter, but basically it is traversal.
You can also use other data structures (without arrays.
Friends who have played games may have good solutions.

Redis sort set type can meet your needs http://redis.readthedocs.io/e...

I think it looks like this:

  • First, the food in the greedy snake is a ball, and the array of food should be saved <坐标, 半径> , Map can be used in C ++;

  • Then, you can open an array called Map. The size of the array is w of the Map.H. suppose there is such a food <(x, y), r>, then Map needs to be assigned the value: Map [wX + y] = r; record a maxR item, representing the maximum radius of food, maxR = max (r ).

  • Finally, determine the collision and set the position of the snake header to (a, B). What you need to do is traverse to (a, B) take all coordinates (x, y) of the circle with the center radius of maxR, and then retrieve the r = Map [w * x + y], with now_r = dis (a, B ), (x, y) in comparison, if r> = now_r, it means that the snake will eat this food. if it is smaller than it, it means it cannot be eaten. continue to judge the next coordinate.

  • In fact, I guess the actual process is to traverse all the food. after all, there are not so many snake foods.

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.