Queen's Attack II Discussions | Algorithms | HackerRank
We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Java 8, But has a lot of memory. I was trying to recycle this in a future Java project that creates a chess game. It failed only three test cases because of time limit.
publicstaticintqueensAttack(intn,intk,intr_q,intc_q,List<List<Integer>>obstacles){intmoves=0;PointqueenCord=newPoint(c_q,r_q);List<Point>list=newArrayList<>();//Creating the list of points representing ordered cordinates of obstacles//and adding it to list for(List<Integer>l:obstacles){Pointp=newPoint(l.get(1),l.get(0));list.add(p);}Pointleft=newPoint(c_q-1,r_q);Pointright=newPoint(c_q+1,r_q);Pointtop=newPoint(c_q,r_q+1);Pointbottom=newPoint(c_q,r_q-1);PointnE=newPoint(c_q+1,r_q+1);PointnW=newPoint(c_q-1,r_q+1);PointsW=newPoint(c_q-1,r_q-1);PointsE=newPoint(c_q+1,r_q-1);while(left.getX()!=0&&!list.contains(left)){moves++;left.translate(-1,0);}while(right.getX()!=n+1&&!list.contains(right)){moves++;right.translate(1,0);}while(top.getY()!=n+1&&!list.contains(top)){moves++;top.translate(0,1);}while(bottom.getY()!=0&&!list.contains(bottom)){moves++;bottom.translate(0,-1);}while(nE.getY()!=n+1&&nE.getX()!=n+1&&!list.contains(nE)){moves++;nE.translate(1,1);}while(nW.getY()!=n+1&&nW.getX()!=0&&!list.contains(nW)){moves++;nW.translate(-1,1);}while(sW.getY()!=0&&sW.getX()!=0&&!list.contains(sW)){moves++;sW.translate(-1,-1);}while(sE.getY()!=0&&sE.getX()!=n+1&&!list.contains(sE)){moves++;sE.translate(1,-1);}returnmoves;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Queen's Attack II
You are viewing a single comment's thread. Return to all comments →
Java 8, But has a lot of memory. I was trying to recycle this in a future Java project that creates a chess game. It failed only three test cases because of time limit.