简体   繁体   中英

Snail Sort - Stuck in the middle, not sure how to proceed

I am stuck at this place, where, I couldn't proceed further. The question goes like this. Given an nxn array, return the array elements arranged from outermost elements to the middle element, traveling clockwise.

array = [[1,2,3],
         [4,5,6],
         [7,8,9]]
snail(array) #=> [1,2,3,6,9,8,7,4,5]

For better understanding, please follow the numbers of the next array consecutively:

array = [[1,2,3],
         [8,9,4],
         [7,6,5]]
snail(array) #=> [1,2,3,4,5,6,7,8,9]

This image will illustrate things more clearly:

The attempt I have got so far is:

arr = [[1, 2, 3], [3, 4]];
var n = arr[0].length;
var i = 0, j = 0;
var fa = [];
var count = 0;
var direction = "right";
console.log(n);
while (fa.length < n * n) {
    fa.push(`${i}, ${j}`);
    if (j == n - 1 && i < n - 1) {
        direction = "down";
    } else if (j == 0 && i != 0) {
        direction = "up";
    } else if (i == n - 1) {
        direction = "left";
    } else if (i == 0) {
        direction = "right";
    }
    switch (direction) {
        case "right":
            j++;
            break;
        case "left":
            j--;
            break;
        case "up":
            i--;
            break;
        case "down":
            i++;
            break;
    }
    if (count++ == 15)
        break;
}
console.log(fa);

I am kind of not sure how to proceed. I need to know two things.

  1. How should I proceed from here?
  2. What is the mistake I am making?

Here is what I did for this problem. You can see how I am keeping track of the min and max col and row values as we go. I'm not sure exactly how you were doing that in your code but hopefully this helps.

var m = 4;
var n = 4;
var board = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
    [13, 14, 15, 16]
];

var result = [];
var dir = 'right';
var imin = 0;
var imax = m - 1;
var jmin = 0;
var jmax = n - 1

var i = imin;
var j = jmin;
var done = false;

while (!done)
{

    switch (dir)
    {
        case 'right':                
            i = imin;
            j = jmin;
            for (j; j <= jmax; j++)
                result.push(board[i][j]);
            console.log(result);
            dir = 'down';
            imin++;
            break;
        case 'left':
            i = imax;
            j = jmax;                
            for (j; j >= jmin; j--)
                result.push(board[i][j]);
            console.log(result);
            dir = 'up';
            imax--;
            break;
        case 'down':
            i = imin;
            j = jmax;
            for (i; i <= imax; i++)
                result.push(board[i][j]);
            console.log(result);
            dir = 'left';
            jmax--;
            break;
        case 'up':
            i = imax;
            j = jmin;
            for (i; i >= imin; i--)
                result.push(board[i][j]);
            console.log(result);
            dir = 'right';
            jmin++;
            break;
    }

    if (imin > imax || jmin > jmax)
        done = true;
}

console.log(result);

Sorry for being this late.

My solution to the problem is

 arr = [ [1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16], ]; function rotate(arr) { copy = []; rows = arr.length; for(i = 0; i < rows; i++) { cols = arr[i].length; for(j = 0; j < cols; j++) { if(copy[cols - 1 - j] === undefined) copy[cols - 1 - j] = []; copy[cols - 1 - j][i] = arr[i][j]; } } return copy; } res = []; do { res = res.concat(arr.shift()); arr = rotate(arr); } while(arr.length); console.log(res); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM