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.
Also just for fun I tried to make it multi threaded to see if Hacker Rank allows this and it does. On small boards this is not a good idea but for very large boards there should be a significant performance improvement.
/*
* Complete the 'queensAttack' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER n
* 2. INTEGER k
* 3. INTEGER r_q
* 4. INTEGER c_q
* 5. 2D_INTEGER_ARRAY obstacles
*/
public static int queensAttack(int n, int k, int r_q, int c_q, List<List<Integer>> obstacles) {
AtomicInteger moves = new AtomicInteger(0);
Point queenCord = new Point(c_q, r_q);
HashMap<Point, Point> list = new HashMap<>();
//Creating the list of points representing ordered cordinates of obstacles
//and adding it to list
for(List<Integer> l : obstacles){
Point p = new Point(l.get(1),l.get(0));
list.put(p,p);
}
Point left = new Point(c_q-1,r_q);
Point right = new Point(c_q+1,r_q);
Point top = new Point(c_q,r_q+1);
Point bottom = new Point(c_q,r_q-1);
Point nE = new Point(c_q+1,r_q+1);
Point nW = new Point(c_q-1,r_q+1);
Point sW = new Point(c_q-1,r_q-1);
Point sE = new Point(c_q+1,r_q-1);
final Thread leftT = new Thread(() -> {
while(left.getX() != 0 && !list.containsKey(left)){
moves.incrementAndGet();
left.translate(-1, 0);
}
});
final Thread rightT = new Thread(() -> {
while(right.getX() != n+1 && !list.containsKey(right)){
moves.incrementAndGet();;
right.translate(1, 0);
}
});
final Thread topT = new Thread(() -> {
while(top.getY() != n+1 && !list.containsKey(top)){
moves.incrementAndGet();;
top.translate(0, 1);
}
});
final Thread bottomT = new Thread(() -> {
while(bottom.getY() != 0 && !list.containsKey(bottom)){
moves.incrementAndGet();;
bottom.translate(0,-1);
}
});
final Thread nET = new Thread(() -> {
while(nE.getY() != n+1 && nE.getX() != n+1 && !list.containsKey(nE)){
moves.incrementAndGet();;
nE.translate(1, 1);
}
});
final Thread nWT = new Thread(() -> {
while(nW.getY() != n+1 && nW.getX() != 0 && !list.containsKey(nW)){
moves.incrementAndGet();;
nW.translate(-1, 1);
}
});
final Thread sWT = new Thread(() -> {
while(sW.getY() != 0 && sW.getX() != 0 && !list.containsKey(sW)){
moves.incrementAndGet();;
sW.translate(-1, -1);
}
});
final Thread sET = new Thread(() -> {
while(sE.getY() != 0 && sE.getX()!=n+1 && !list.containsKey(sE)){
moves.incrementAndGet();;
sE.translate(1, -1);
}
});
leftT.start();
rightT.start();
topT.start();
bottomT.start();
nET.start();
nWT.start();
sET.start();
sWT.start();
try {
leftT.join();
rightT.join();
topT.join();
bottomT.join();
nET.join();
nWT.join();
sET.join();
sWT.join();
}
catch (InterruptedException i){
i.printStackTrace();
}
return moves.get();
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int n = Integer.parseInt(firstMultipleInput[0]);
int k = Integer.parseInt(firstMultipleInput[1]);
String[] secondMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int r_q = Integer.parseInt(secondMultipleInput[0]);
int c_q = Integer.parseInt(secondMultipleInput[1]);
List<List<Integer>> obstacles = new ArrayList<>();
IntStream.range(0, k).forEach(i -> {
try {
obstacles.add(
Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList())
);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
int result = Result.queensAttack(n, k, r_q, c_q, obstacles);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
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 →
Also just for fun I tried to make it multi threaded to see if Hacker Rank allows this and it does. On small boards this is not a good idea but for very large boards there should be a significant performance improvement.
import java.io.; import java.math.; import java.security.; import java.text.; import java.util.; import java.util.concurrent.; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.; import java.util.regex.; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; import java.awt.Point;
class Result {
}
public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
}