日期:2014-05-16 浏览次数:20403 次
<script>
function getJz(n){
var jz=new Array;
for(var i=0;i<n;i++){
var tmp=new Array;
for(var j=0;j<n;j++){
tmp[j]=0;
}
jz[i]=tmp;
}
//alert(jz);
if(n%2==0){
var x=n-1;
var y=0;
var direct='r';
}else{
var x=0;
var y=n-1;
var direct='l';
}
var start=n*n;
for(var i=start;i>0;i--){
if(jz[x][y]==0){
var tmp_x=x;
var tmp_y=y;
}else{
if(direct=='l'){
if(y-1>=0&&jz[x][y-1]==0){
y--;
}else{
direct='d';
}
}
if(direct=='d'){
if(x+1<n&&jz[x+1][y]==0){
x++;
}else{
direct='r';
}
}
if(direct=='r'){
if(y+1<n&&jz[x][y+1]==0){
y++;
}else{
direct='u';
}
}
if(direct=='u'){
if(x-1>=0&&jz[x-1][y]==0){
x--;
}else{
direct='l';
if(y-1>=0&&jz[x][y-1]==0){
y--;
}else{
direct='d';
}
}
}
}
tmp_x=x;
tmp_y=y;
jz[tmp_x][tmp_y]=i;
}
document.write('<table border=1>');
for(var i=0;i<n;i++){
document.write('<tr id=tr'+i+'>');
for(var j=0;j<n;j++){
document.write('<td id=td'+jz[i][j]+'>'+jz[i][j]+'</td>');
}
document.write('</tr>');
}
document.write('</table>');
}
getJz(6);
getJz(5);
</script>