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.
By using a map of valid locations, you can avoid bounds checking.
importjava.io.*;importjava.math.*;importjava.security.*;importjava.text.*;importjava.util.*;importjava.util.concurrent.*;importjava.util.function.*;importjava.util.regex.*;importjava.util.stream.*;importstaticjava.util.stream.Collectors.joining;importstaticjava.util.stream.Collectors.toList;classNode{intx;inty;intchoices;publicNode(intx,inty,intc){this.x=x;this.y=y;this.choices=c;}@Overridepublicbooleanequals(Objecto){Noden=(Node)o;return(n.x==x&&n.y==y&&n.choices==choices);}@OverridepublicinthashCode(){returnx*129+y*47+choices;}}classResult{/* * Complete the 'countLuck' function below. * * The function is expected to return a STRING. * The function accepts following parameters: * 1. STRING_ARRAY matrix * 2. INTEGER k */publicstaticStringgetk(intx,inty){returnx+" "+y;}publicstaticStringcountLuck(List<String>matrix,intk){// Write your code here// BFS, count choices.int[]dx={-1,0,0,1};int[]dy={0,1,-1,0};Queue<Node>todo=newLinkedList<>();intx=0;inty=0;intn=matrix.size();intm=matrix.get(0).length();Set<String>dots=newHashSet<>();intgoalx=0;intgoaly=0;for(intj=0;j<n;j++){Strings=matrix.get(j);for(inti=0;i<m;i++){charc=s.charAt(i);if(c=='M'){x=i;y=j;}if(c=='.'||c=='*'){dots.add(getk(i,j));}if(c=='*'){goalx=i;goaly=j;}}}Set<String>seen=newHashSet<>();todo.add(newNode(x,y,0));System.err.println("dots:"+dots);while(true){Nodehere=todo.remove();seen.add(getk(here.x,here.y));// count paths.x=here.x;y=here.y;if(x==goalx&&y==goaly){if(here.choices==k){return"Impressed";};return"Oops!";}intcount=0;List<Node>addme=newArrayList<>();for(intq=0;q<4;q++){intnx=x+dx[q];intny=y+dy[q];Stringkey=getk(nx,ny);if(!seen.contains(key)&&dots.contains(key)){count++;addme.add(newNode(nx,ny,0));}}intbump=(count>1)?1:0;here.choices+=bump;for(Nodenode:addme){node.choices=here.choices;}todo.addAll(addme);}}}publicclassSolution{publicstaticvoidmain(String[]args)throwsIOException{BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(System.in));BufferedWriterbufferedWriter=newBufferedWriter(newFileWriter(System.getenv("OUTPUT_PATH")));intt=Integer.parseInt(bufferedReader.readLine().trim());IntStream.range(0,t).forEach(tItr->{try{String[]firstMultipleInput=bufferedReader.readLine().replaceAll("\\s+$","").split(" ");intn=Integer.parseInt(firstMultipleInput[0]);intm=Integer.parseInt(firstMultipleInput[1]);List<String>matrix=IntStream.range(0,n).mapToObj(i->{try{returnbufferedReader.readLine();}catch(IOExceptionex){thrownewRuntimeException(ex);}}).collect(toList());intk=Integer.parseInt(bufferedReader.readLine().trim());Stringresult=Result.countLuck(matrix,k);bufferedWriter.write(result);bufferedWriter.newLine();}catch(IOExceptionex){thrownewRuntimeException(ex);}});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
Count Luck
You are viewing a single comment's thread. Return to all comments →
By using a map of valid locations, you can avoid bounds checking.