Skip to content

Instantly share code, notes, and snippets.

@dstyle0210
Created July 13, 2016 07:21
Show Gist options
  • Select an option

  • Save dstyle0210/e9912e3f205b8c71e8ac36e7bea073ad to your computer and use it in GitHub Desktop.

Select an option

Save dstyle0210/e9912e3f205b8c71e8ac36e7bea073ad to your computer and use it in GitHub Desktop.
[코딩도장] Spiral Array
// 코딩도장 공부
// http://codingdojang.com/scode/266
// 임시 데이터 생성.
var col = 8;
var row = 4;
var data = (function(){
var arr = [];
for(i=0;i<row;i++){
arr[i] = new Array(col);
};
return arr;
})();
var colIdx = 0; // 들어가야될 가로자리
var rowIdx = 0; // 들어가야될 세로자리
var wallLeftCol = 0; // 좌측 세로벽.
var wallRightCol = col-1; // 우측 세로벽.
var wallTopRow = 0; // 상단 가로벽.
var wallBotRow = row-1; // 하단 가로벽.
var move = "leftToRight";
for(i=0;i<row*col;i++){
console.log(rowIdx+","+colIdx+"==>"+i+"==>"+move);
data[rowIdx][colIdx] = i;
if(move=="leftToRight"){
colIdx = (colIdx+1);
if(colIdx == wallRightCol){
wallTopRow = wallTopRow+1; // 상단벽 한칸밀고,
move = "topToBottom";
};
}else if(move=="topToBottom"){
rowIdx = (rowIdx+1);
if(rowIdx == wallBotRow){
wallRightCol = wallRightCol-1; // 우측벽 한칸밀고.
move = "rightToLeft";
};
}else if(move=="rightToLeft"){
colIdx = (colIdx-1);
if(colIdx == wallLeftCol){
wallBotRow = wallBotRow -1; // 하단벽 한칸 밀고
move = "bottomToTop";
};
}else if(move=="bottomToTop"){
rowIdx = (rowIdx-1);
if(rowIdx == wallTopRow){
wallLeftCol = wallLeftCol + 1;
move = "leftToRight";
};
};
};
console.log(data);
var tlb = "<table>";
for(j=0;j<row;j++){
tlb += "<tr>";
for(i=0;i<col;i++){
tlb += "<td>"+data[j][i]+"</td>";
}
tlb += "</tr>";
};
tlb += "</table>";
document.write(tlb);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment