Marquee.java

來源:互聯網
上載者:User

public class Marquee {
    int[][] matrix;
    int n ; //走過的步數
    int time;//控制結束的一個參數
    int rank ; //階
    coordinate start;//起始點
    public static void main(String[] s) {
 a ma = new a(12,new coordinate(0,0) );

 coordinate temp = ma.start;
 
 while(true) {
     int oldTime = ma.time();
     ///在這裡給出你的步驟
     temp = ma.rightOf(temp);
     temp = ma.downOf(temp);
     temp = ma.leftOf(temp);
     temp = ma.upOf(temp);
     //
     if( oldTime == ma.time() ) break;
 }
 ma.print();
    }
    a(int rank,coordinate start) {
 n = 1;
 this.rank = rank;
 matrix = new int[rank][rank];
 time = n;
 this.start = start;
 matrix[start.x][start.y] = n;
    }
    public int time() {
 return this.time;
    }
    public void print() {
 for(int i =0; i<this.rank; i++) {
     System.out.println();
     for (int j =0; j<this.rank; j++) {
  System.out.print(this.matrix[i][j]+" ");
     }
 }
    }
    //把矩陣看做座標系
    //從xyRightFrom座標開始向右走,直到碰到邊界或者已經當地當前點已被寫
    //四個類似作用的方法
    private coordinate rightOf(coordinate xyRightFrom) {//xyRightFrom向右的起始點
 int i = xyRightFrom.x;
 int j = xyRightFrom.y;
 while(j+1<rank && matrix[i][j+1]==0 ){
     n++;
     matrix[i][++j] = n;
 }
 if ( matrix[i][j] == 0) matrix[i][j] = ++n;
 time = n;
 return new coordinate(i,j);
    }
    private coordinate downOf(coordinate xyDownFrom) {
 int i = xyDownFrom.x;
 int j = xyDownFrom.y;
 while( i+1<rank && matrix[i+1][j] ==0 ){
     n++;
     matrix[++i][j] = n;
 }
 if ( matrix[i][j] == 0) matrix[i][j] = ++n;
 time = n;
 return new coordinate(i,j);
    }
    private coordinate leftOf(coordinate xyLeftFrom) {
 int i = xyLeftFrom.x;
 int j = xyLeftFrom.y;
 while( j-1>=0 && matrix[i][j-1] ==0) {
     n++;
     matrix[i][--j] = n;
 }
 if ( matrix[i][j] == 0) matrix[i][j] = ++n;
 time = n;
 return new coordinate(i,j);
    }
    private coordinate upOf(coordinate xyUpFrom) {
 int i = xyUpFrom.x;
 int j = xyUpFrom.y;
 while( i-1 >=0 && matrix[i-1][j] ==0 ) {
     n++;
     matrix[--i][j] = n;
 }
 if ( matrix[i][j] == 0) matrix[i][j] = ++n;
 time = n;
 return  new coordinate(i,j);
    }
}
class coordinate {
    int x;
    int y;
    coordinate(int x,int y) {
 this.x = x;
 this.y = y;
    }
   
}
   
       

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.