开发者

what is the easiest way to do this function in c#?

开发者 https://www.devze.com 2022-12-31 20:51 出处:网络
let say that we have an array [5,5] 01,02,03,04,05 06,07,08,09,10 11,12,13,14,15 16,17,18,19,20 21,22,23,24,25

let say that we have an array [5,5]

01,02,03,04,05
06,07,08,09,10
11,12,13,14,15
16,17,18,19,20
21,22,23,24,25

the user should send 2 values to the function (start,searchFOR) for example (13,25) the function should search for that value in this way

07,08,09
12,  ,14
17,18,19

if the value is n't found in this level it will goes a level higher

01,02,03,04,05
06,  ,  ,  ,10
11,  ,  ,  ,15
16,  ,  ,  ,20
21开发者_运维知识库,22,23,24,25

if the array is bigger than this and the value didn't found it will go to a level higher

Thanks for your help

<<<< EDIT >>>> FOR 25

  ,  ,  ,  ,  
  , ,  ,  ,  
  ,  ,  ,  ,  
  ,  ,  ,19,20  
  ,  ,  ,24,  

  ,  ,  ,  ,  
  ,  ,  ,  ,  
  ,  ,13,14,15
  ,  ,18,  ,  
  ,  ,23,  ,  

  ,  ,  ,  ,  
  ,07,08,09,10
  ,12,  ,  ,  
  ,17,  ,  ,  
  ,22,  ,  ,  

01,02,03,04,05
06,  ,  ,  ,  
11,  ,  ,  ,  
16,  ,  ,  ,  
21,  ,  ,  ,  


Assuming this is homework...

You will notice that in a 5X5 array, you have to add/subtract 5 to move vertically, and add/subtract 1 to move horizontally.

This works regardless of your starting position in the array.

Now you just have to figure out how to know when you're on an edge.


You will need four for loops to get around each square, nested inside an outer loop. The outer loop will work each of the squares. You should be able to use the number generated by the outer loop to help you with your calculations in your inner loops.


In your scenario could you change your implementation a bit and start out with something a little different.

int[] lvl1 = [1, 2, 3, 4, 5, 10, 15, 20, 25, 24, 23, 22, 21, 16, 11, 06]<br/>
int[] lvl2 = [7, 8, 9, 14, 19, 18, 17, 12, 7]<br/>
int[] lvl3 = [13]
0

精彩评论

暂无评论...
验证码 换一张
取 消