public class Wheeling {
private int rowRange;
private int colRange;
private int valRange;
private int row;
private int col;
private int val;
private int opr;
private int[][] a;
public static void main(String[]
args) {
Wheeling wheel = new Wheeling(7,8);
wheel.fill();
wheel.display();
}
public Wheeling(int row, int col) {
rowRange = row;
colRange = col;
a = new int[row][col];
valRange = row * col;
}
public void fill() {
a[0][0]=++val;
while(val<valRange){
if(operation(opr)){
continue;
}else{
++opr;
}
}
}
public void display(){
for(int i=0; i<rowRange; i++){
System.out.println();
for(int j=0; j<colRange; j++){
System.out.printf("%5d", a[i][j]);
}
}
}
public boolean operation(int opr) {
int trow = row;
int tcol = col;
switch (opr % 4) {
case 0:
trow
= row + 1;
break;
case 1:
tcol
= col + 1;
break;
case 2:
trow
= row - 1;
break;
default:
tcol
= col - 1;
break;
}
if(trow==rowRange || trow==-1 ||
tcol==colRange || tcol==-1){
return false;
}else if(a[trow][tcol]!=0){
return false;
}else{
a[trow][tcol]=++val;
row = trow;
col = tcol;
return true;
}
}
}
No comments:
Post a Comment