# The Snake vs the Wind

## The Snake vs the Wind

**Note**: For a given wind's direction and the initial position of the snake, there exists only one unique path the snake will take to visit all the cells.

**Input Format**

`n`

, `s`

, `w`

, or `e`

) which determines the direction towards which the wind blows. In the third line, you will be given the coordinates of the corner where the snake will start the journey. The upper-left corner has the coordinates and the lower-right corner has the coordinates .**Constraints**

- The snake will always start the journey in one of the four corners of the grid.

**Output Format**

**Sample Input 0**

2 e 1 0

**Sample Output 0**

4 3 1 2

**Explanation 0**

**Sample Input 1**

4 n 0 0

**Sample Output 1**

1 2 3 4 8 7 6 5 9 10 11 12 16 15 14 13

**Explanation 1**

**Code:**

**Javascript:**

process.stdin.resume(); process.stdin.setEncoding('ascii');

var input_stdin = "";

var input_stdin_array = "";

var input_currentline = 0;

process.stdin.on('data', function (data) {

input_stdin += data;

});

process.stdin.on('end', function () {

input_stdin_array = input_stdin.split("n");

main();

});

function readLine() {

return input_stdin_array[input_currentline++];

}

/////////////// ignore above this line ////////////////////

var move = {

e: function(){

return [0, 1]

},

w: function(){

return [0, -1]

},

n: function(){

return [-1, 0]

},

s: function(){

return [1, 0]

}

}

function pos_dir(n, x, y, m){

if(x == 0 && y == 0)

return ['e', 's']

if(x == 0 && y == (n - 1))

return ['w','s']

if(x == n - 1 && y == 0)

return ['e', 'n']

if(x == n-1 && y == n-1)

return ['w', 'n']

return ['n','s','w','e']

}

function isTracingBack(str, nd, n, a, b){

var x = 0 + a, y = 0 + b;

var arr = str.split("");

arr.push(nd);

arr = arr.map(function(d){

var pos = move[d]()

x+= pos[0];

y+= pos[1];

return ((x >= 0 && y >= 0) && (x <= (n - 1) && y <= (n - 1))) ? (x + "," + y) : false

});

arr.unshift(a + "," + b)

var last = arr.pop();

return (last == false && typeof(last) !== "string") ? true : arr.includes(last);

}

function bet_pos(dirs, d, m, x, y, n){

var bet = (d == 'e' || d == 'w') ? 'ns' : 'ew';

if(m.dir)

dirs = dirs.filter(function(e){

return !isTracingBack(m.dir, e, n, x, y)

});

return dirs.length > 1 ? (dirs.includes(d) ? [d] : dirs.join("").match(new RegExp(`[${bet}]`)))[0] : dirs[0];

}

function findIndex(m, x, y){

return m.findIndex(function(e){

return e.join() == x + "," + y

})

}

function main() {

var n = parseInt(readLine());

var d = readLine();

var x_temp = readLine().split(' ');

var x = parseInt(x_temp[0]), a = x;

var y = parseInt(x_temp[1]), b = y;

var m = [];

m.dir = "";

for(var i = 0; i < n; i++)

for(var j = 0; j < n; j++)

m.push([i, j]);

var start = findIndex(m, x, y);

var i = 1;

m[start].push(i);

while(i < n*n){

var dirs = pos_dir(n, x, y, m),

dir = bet_pos(dirs, d, m, a, b, n);

try{

var new_pos = move[dir]();

}

catch(exp){

m[m.findIndex(function(e){

return e.length == 2

})].push(++i)

}

x += new_pos[0];

y += new_pos[1];

m.dir += dir;

try{

m[findIndex(m, x, y)].push(++i)

}

catch(err){

var idx = m.findIndex(function(e){

return e.length == 2

});

idx !== -1 ? m[idx].push(++i) : 0

}

}

var k = 0, counter = 0;

while(k < n*n){

process.stdout.write(m[k++][2] + " ")

if(counter++ == (n - 1)){

process.stdout.write("n")

counter = 0

}

}

}

Hi there, of course this article is actually pleasant and I have learned lot of

things from it about blogging. thanks.