classSnakeGame:def__init__(self,width:int,height:int,food:List[List[int]]):self.m=heightself.n=widthself.food=foodself.score=0self.idx=0self.q=deque([(0,0)])self.vis={(0,0)}defmove(self,direction:str)->int:i,j=self.q[0]x,y=i,jmatchdirection:case"U":x-=1case"D":x+=1case"L":y-=1case"R":y+=1ifx<0orx>=self.mory<0ory>=self.n:return-1if(self.idx<len(self.food)andx==self.food[self.idx][0]andy==self.food[self.idx][1]):self.score+=1self.idx+=1else:self.vis.remove(self.q.pop())if(x,y)inself.vis:return-1self.q.appendleft((x,y))self.vis.add((x,y))returnself.score# Your SnakeGame object will be instantiated and called as such:# obj = SnakeGame(width, height, food)# param_1 = obj.move(direction)
classSnakeGame{privateintm;privateintn;privateint[][]food;privateintscore;privateintidx;privateDeque<Integer>q=newArrayDeque<>();privateSet<Integer>vis=newHashSet<>();publicSnakeGame(intwidth,intheight,int[][]food){m=height;n=width;this.food=food;q.offer(0);vis.add(0);}publicintmove(Stringdirection){intp=q.peekFirst();inti=p/n,j=p%n;intx=i,y=j;if("U".equals(direction)){--x;}elseif("D".equals(direction)){++x;}elseif("L".equals(direction)){--y;}else{++y;}if(x<0||x>=m||y<0||y>=n){return-1;}if(idx<food.length&&x==food[idx][0]&&y==food[idx][1]){++score;++idx;}else{intt=q.pollLast();vis.remove(t);}intcur=f(x,y);if(vis.contains(cur)){return-1;}q.offerFirst(cur);vis.add(cur);returnscore;}privateintf(inti,intj){returni*n+j;}}/** * Your SnakeGame object will be instantiated and called as such: * SnakeGame obj = new SnakeGame(width, height, food); * int param_1 = obj.move(direction); */
classSnakeGame{public:SnakeGame(intwidth,intheight,vector<vector<int>>&food){m=height;n=width;this->food=food;score=0;idx=0;q.push_back(0);vis.insert(0);}intmove(stringdirection){intp=q.front();inti=p/n,j=p%n;intx=i,y=j;if(direction=="U"){--x;}elseif(direction=="D"){++x;}elseif(direction=="L"){--y;}else{++y;}if(x<0||x>=m||y<0||y>=n){return-1;}if(idx<food.size()&&x==food[idx][0]&&y==food[idx][1]){++score;++idx;}else{inttail=q.back();q.pop_back();vis.erase(tail);}intcur=f(x,y);if(vis.count(cur)){return-1;}q.push_front(cur);vis.insert(cur);returnscore;}private:intm;intn;vector<vector<int>>food;intscore;intidx;deque<int>q;unordered_set<int>vis;intf(inti,intj){returni*n+j;}};/** * Your SnakeGame object will be instantiated and called as such: * SnakeGame* obj = new SnakeGame(width, height, food); * int param_1 = obj->move(direction); */
typeSnakeGamestruct{mintnintfood[][]intscoreintidxintq[]intvismap[int]bool}funcConstructor(widthint,heightint,food[][]int)SnakeGame{returnSnakeGame{height,width,food,0,0,[]int{0},map[int]bool{}}}func(this*SnakeGame)Move(directionstring)int{f:=func(i,jint)int{returni*this.n+j}p:=this.q[0]i,j:=p/this.n,p%this.nx,y:=i,jifdirection=="U"{x--}elseifdirection=="D"{x++}elseifdirection=="L"{y--}else{y++}ifx<0||x>=this.m||y<0||y>=this.n{return-1}ifthis.idx<len(this.food)&&x==this.food[this.idx][0]&&y==this.food[this.idx][1]{this.score++this.idx++}else{t:=this.q[len(this.q)-1]this.q=this.q[:len(this.q)-1]this.vis[t]=false}cur:=f(x,y)ifthis.vis[cur]{return-1}this.q=append([]int{cur},this.q...)this.vis[cur]=truereturnthis.score}/** * Your SnakeGame object will be instantiated and called as such: * obj := Constructor(width, height, food); * param_1 := obj.Move(direction); */
classSnakeGame{privatem:number;privaten:number;privatefood:number[][];privatescore:number;privateidx:number;privateq:number[];privatevis:Set<number>;constructor(width:number,height:number,food:number[][]){this.m=height;this.n=width;this.food=food;this.score=0;this.idx=0;this.q=[0];this.vis=newSet([0]);}move(direction:string):number{constp=this.q[0];consti=Math.floor(p/this.n);constj=p%this.n;letx=i;lety=j;if(direction==='U'){--x;}elseif(direction==='D'){++x;}elseif(direction==='L'){--y;}else{++y;}if(x<0||x>=this.m||y<0||y>=this.n){return-1;}if(this.idx<this.food.length&&x===this.food[this.idx][0]&&y===this.food[this.idx][1]){++this.score;++this.idx;}else{constt=this.q.pop()!;this.vis.delete(t);}constcur=x*this.n+y;if(this.vis.has(cur)){return-1;}this.q.unshift(cur);this.vis.add(cur);returnthis.score;}}/** * Your SnakeGame object will be instantiated and called as such: * var obj = new SnakeGame(width, height, food) * var param_1 = obj.move(direction) */