`
hnjzsyjyj
  • 浏览: 27710 次
  • 性别: Icon_minigender_1
  • 来自: 南京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

Snake algorithm java implementation

阅读更多
转载自http://www.codeweblog.com/snake-algorithm-java-implementation/#

public class test {

        /**
         *  Describes a  :
         * 5 Line 5 column snake-like algorithm  
         * (0,0)(4,4)- Line 1 and countdown  1 Difference between rows  4
         * (1,0)(4,3)(0,1)(3,4)- Line 2 and countdown  2 Difference between rows  3
         * (0,2)(2,4)(1,1)(3,3)(2,0)(4,2)- The first 3 rows and countdown  3 Difference between rows  2
         * (3,0)(4,1)(2,1)(3,2)(1,2)(2,3)(0,3)(1,4)- The fourth row and countdown  4 Difference between rows  1
         *  Section 5 behavior segmentation line  
         *  According to the above description, obtain the following algorithm  
         */
        /**
         *  Description of second  :
         * dao Variable functions  :
         *  The main calculation downward diagonal coordinate values  .
         *  For example,  :5 Line 5 columns   
         * {
                   (0,0)                  -- Known as line  0
                   (1,0)(0,1)             -- Known as line  1
           (0,2)(1,1)(2,0)        -- Known as line  2
           (3,0)(2,1)(1,2)(0,3)   -- Known as line  3
           }--- All coordinates on the diagonal  
       (0,4)(1,3)(2,2)(3,1)(4,0)--- Here called diagonal  4
           {
                  (4,1)(3,2)(2,3)(1,4)  -- Known as line  5
                  (2,4)(3,3)(4,2)       -- Known as line  6
                  (4,3)(3,4)            -- Known as line  7
                  (4,4)                 -- Known as line  8
           }--- Downward diagonal all coordinates  
           dao The maximum value of the variable is line  * Column  
            Get all the coordinates of the diagonal values by dao variables and  temp Variable functions  
         */
        /**
         * @param irow  Line  
         * @param icol  Column  
         * @param sheLength  Sections snake segment, which is line with diagonal  
         */
        public void sheAglo(int irow,int icol,int sheLength){
                int row = 0,col = 0;
                int ban = (sheLength-1)/2+1;
                int array[][] = new int[irow][icol];
                int m = 1,num = ban - 1;
                int dao = irow*icol;
                for(int i=0;i<ban;i++){
                        row = col = i;
                        int  temp = dao-i;// The first n rows downward diagonal values from  dao-i Start  
                        if(i%2==0){             // Handling double-row    
                                /**
                                 * (0,0)
                                 * (0,2)(1,1)(2,0)
                                 *  Characteristic is the increment, decrement column rows  
                                 */
                                while(col>=0){
                                        System.out.print("["+(i-col)+"]["+(col)+"]("+m+"),");
                                        if(i!=ban-1){// This is the diagonal  
                                                System.out.print("["+(i-col+num)+"]["+(col+num)+"]("+(temp++)+"),");
                                        }
                                        array[i-col][col] = m;
                                        dao--;
                                        col--;
                                        m++;
                                }
                        }else if(i%2!=0){// Handle single-line  
                                /**
                                 * (1,0)(0,1)
                                 * (3,0)(2,1)(1,2)(0,3)
                                 *  Characteristics are diminishing, incrementing column rows  
                                 */
                                while(row>=0){
                                        System.out.print("["+(row)+"]["+(i-row)+"]("+m+"),");
                                        if(i!=ban-1){
                                                System.out.print("["+(row+num)+"]["+(i-row+num)+"]("+(temp++)+"),");
                                        }
                                        array[row][i-row] = m;
                                        dao--;
                                        row--;
                                        m++;
                                }
                        }
                        num--;
                        System.out.println();
                }
        }
        
        public static void main(String args[]){
                test test = new test();
                test.sheAglo(6,6,11);
        }
        
}



另外一个重要网站:http://users.ecs.soton.ac.uk/msn/book/new_demo/Snakes/
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics