using System.Collections.Generic;
public static void Main()
var food = new int[,] { {1, 2}, {0, 1}};
var snake = new Snake(3, 2, food);
Console.WriteLine(snake.Move("R"));
Console.WriteLine(snake.Move("D"));
Console.WriteLine(snake.Move("R"));
Console.WriteLine(snake.Move("U"));
Console.WriteLine(snake.Move("L"));
Console.WriteLine(snake.Move("L"));
Console.WriteLine(snake.Move("D"));
Console.WriteLine(snake.Move("R"));
private readonly int[,] _food;
private LinkedList<Point> _snake;
private HashSet<string> _hash;
public Snake(int width, int height, int[,] food)
_snake = new LinkedList<Point>();
_hash = new HashSet<string>();
private string GetKey(int x, int y)
return string.Format("{0}.{1}", x, y);
private void AddFirst(int x, int y)
_snake.AddFirst(new Point(x, y));
private void RemoveLast()
var last = _snake.Last.Value;
_hash.Remove(GetKey(last.x, last.y));
private bool IsInvalid(int x, int y)
return x < 0 || x >= _w || y < 0 || y >= _h || _hash.Contains(GetKey(x,y));
private bool FindFood(int x, int y)
return _fIdx < _food.GetLength(0) && x == _food[_fIdx, 1] && y == _food[_fIdx, 0];
public int Move(string direction)
var cur = _snake.First.Value;
public Point(int x, int y)