Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- static int destR, destC, count;
- static boolean[][] visited;
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- for(int t=in.nextInt(); t>0; --t) {
- int n=in.nextInt(), m=in.nextInt();
- int startR=0, startC=0;
- visited = new boolean[n][m];
- for(int i=0; i<n; ++i) {
- String row=in.next();
- for(int j=0; j<m; ++j) {
- if(row.charAt(j)=='X')
- visited[i][j]=true;
- else if(row.charAt(j)=='M') {
- startR=i;
- startC=j;
- } else if(row.charAt(j)=='*') {
- destR=i;
- destC=j;
- }
- }
- }
- int k=in.nextInt();
- count=0;
- dfs(startR, startC);
- System.out.println(k==count?"Impressed":"Oops!");
- }
- }
- static boolean dfs(int r, int c) {
- visited[r][c]=true;
- if(r==destR&&c==destC)
- return true;
- List<Point> neighbors = new ArrayList<Point>();
- if(r>0&&!visited[r-1][c])
- neighbors.add(new Point(r-1, c));
- if(c>0&&!visited[r][c-1])
- neighbors.add(new Point(r, c-1));
- if(r<visited.length-1&&!visited[r+1][c])
- neighbors.add(new Point(r+1, c));
- if(c<visited[0].length-1&&!visited[r][c+1])
- neighbors.add(new Point(r, c+1));
- for(Point neighbor : neighbors) {
- if(dfs(neighbor.x, neighbor.y)) {
- if(neighbors.size()>1)
- ++count;
- return true;
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement