Algorithm for iterating over an outward spiral on a discrete 2D grid from the origin

前端 未结 12 1627
独厮守ぢ
独厮守ぢ 2020-12-05 06:54

For example, here is the shape of intended spiral (and each step of the iteration)

          y
          |
          |
   16 15 14 13 12
   17  4  3  2 11
--         


        
12条回答
  •  不思量自难忘°
    2020-12-05 07:29

    I have an algorithm in java that outputs a similar output to yours, except that it prioritizes the number on the right, then the number on the left.

      public static String[] rationals(int amount){
       String[] numberList=new String[amount];
       int currentNumberLeft=0;
       int newNumberLeft=0;
       int currentNumberRight=0;
       int newNumberRight=0;
       int state=1;
       numberList[0]="("+newNumberLeft+","+newNumberRight+")";
       boolean direction=false;
     for(int count=1;count

提交回复
热议问题