Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. public class Solution {
  2.     static int destR, destC, count;
  3.     static boolean[][] visited;
  4.    
  5.     public static void main(String[] args) {
  6.         Scanner in = new Scanner(System.in);
  7.         for(int t=in.nextInt(); t>0; --t) {
  8.             int n=in.nextInt(), m=in.nextInt();
  9.             int startR=0, startC=0;
  10.             visited = new boolean[n][m];
  11.             for(int i=0; i<n; ++i) {
  12.                 String row=in.next();
  13.                 for(int j=0; j<m; ++j) {
  14.                     if(row.charAt(j)=='X')
  15.                         visited[i][j]=true;
  16.                     else if(row.charAt(j)=='M') {
  17.                         startR=i;
  18.                         startC=j;
  19.                     } else if(row.charAt(j)=='*') {
  20.                         destR=i;
  21.                         destC=j;
  22.                     }
  23.                 }
  24.             }
  25.             int k=in.nextInt();
  26.             count=0;
  27.             dfs(startR, startC);
  28.             System.out.println(k==count?"Impressed":"Oops!");
  29.         }
  30.     }
  31.    
  32.     static boolean dfs(int r, int c) {
  33.         visited[r][c]=true;
  34.         if(r==destR&&c==destC)
  35.             return true;
  36.         List<Point> neighbors = new ArrayList<Point>();
  37.         if(r>0&&!visited[r-1][c])
  38.             neighbors.add(new Point(r-1, c));
  39.         if(c>0&&!visited[r][c-1])
  40.             neighbors.add(new Point(r, c-1));
  41.         if(r<visited.length-1&&!visited[r+1][c])
  42.             neighbors.add(new Point(r+1, c));
  43.         if(c<visited[0].length-1&&!visited[r][c+1])
  44.             neighbors.add(new Point(r, c+1));
  45.         for(Point neighbor : neighbors) {
  46.             if(dfs(neighbor.x, neighbor.y)) {
  47.                 if(neighbors.size()>1)
  48.                     ++count;
  49.                 return true;
  50.             }
  51.         }
  52.         return false;
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement