Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5.  
  6.  
  7. public class Main {
  8.  
  9. public static int rows;
  10. public static int cols;
  11. public static ArrayList<String>grid;
  12. public static boolean [][]visited;
  13. public static int cnt;
  14.  
  15. public static boolean isValid(int r,int c)
  16. {
  17. if((r>=0 && r<rows) && (c>=0 && c<cols))
  18. return true;
  19. else
  20. return false;
  21. }
  22.  
  23. public static void floodFill(int r,int c)
  24. {
  25. if(!isValid(r,c) || grid.get(r).charAt(c)=='0' || visited[r][c])
  26. return;
  27.  
  28. visited[r][c]=true;
  29. cnt++;
  30.  
  31. floodFill(r+1, c); // Down
  32. floodFill(r-1, c); // UP
  33. floodFill(r, c-1); // Left
  34. floodFill(r, c+1); // Right
  35. floodFill(r-1, c-1); // UP-Left
  36. floodFill(r+1, c-1); // Down -Left
  37. floodFill(r-1, c+1); // UP - Right
  38. floodFill(r+1, c+1); // Down - Right
  39.  
  40. }
  41.  
  42. public static void main(String[] args) throws NumberFormatException, IOException {
  43. BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  44. StringBuilder ans = new StringBuilder();
  45. int t = Integer.parseInt(in.readLine());
  46. in.readLine();
  47. for(int i=0;i<t;i++)
  48. {
  49. cnt =0;
  50. grid = new ArrayList<String>();
  51. String temp;
  52. while((temp = in.readLine())!=null && temp.length()!=0 )
  53. grid.add(temp);
  54.  
  55. rows = grid.size();
  56. cols = grid.get(0).length();
  57. visited = new boolean [rows][cols];
  58. for(int j=0;j<rows;j++)
  59. {
  60. for(int k=0;k<cols;k++)
  61. {
  62. int tm = cnt;
  63. cnt=0;
  64. if(!visited[j][k] && grid.get(j).charAt(k)!='0')
  65. floodFill(j, k);
  66. cnt = Math.max(cnt, tm);
  67. }
  68. }
  69. ans.append(cnt).append("\n");
  70. if(i!=t-1)
  71. ans.append("\n");
  72. }
  73. System.out.print(ans.toString());
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement