Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.*;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.lang.reflect.Array;
- import java.util.Arrays;
- import java.util.Scanner;
- class Main
- {
- public static char[][] arr = new char[1001][1001];
- public static int[][] vis = new int[1001][1001];
- boolean exist;
- public static int best, n, m;
- public static int nextI, nextJ;
- public static void dfs(int i, int j, int d)
- {
- if(i<0 || i>=m || j<0 || j>=n || arr[i][j]!='.') return;
- arr[i][j] = 'v';
- dfs(i+1, j, d+1);
- dfs(i, j+1, d+1);
- dfs(i, j-1, d+1);
- dfs(i-1, j, d+1);
- arr[i][j] = '.';
- if(d > best)
- {
- best = d;
- nextI=i;
- nextJ=j;
- }
- }
- public static int find()
- {
- int maxlen = 0;
- if(arr[nextI][nextJ]!='.') return 0;
- while(true)
- {
- best = 0;
- dfs(nextI, nextJ, 0);
- if(best <= maxlen) break;
- else maxlen = best;
- }
- return maxlen;
- }
- public static void main (String[] args) throws java.lang.Exception
- {
- int ile, d=0;
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- ile = Integer.parseInt(br.readLine());
- while(d<ile)
- {
- String line = br.readLine();
- n = Integer.parseInt(line.split(" ")[0]);
- m = Integer.parseInt(line.split(" ")[1]);
- //char[][] t = new char[m][n];
- String linia ;
- for(int i = 0; i<m; i++)
- {
- linia = br.readLine();
- for(int j = 0; j<n; j++)
- {
- arr[i][j] = linia.charAt(j);
- }
- }
- nextI = nextJ = 0;
- for(int i=0; i<m; i++)
- for(int j=0; j<n; j++)
- if(arr[i][j]=='.')
- {
- nextI = i;
- nextJ = j;
- break;
- }
- System.out.println("Maximum rope length is " + find()+".");
- d++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement