Home > Back-end >  Greedy algorithm
Greedy algorithm

Time:09-17

(1) the study of Lct preparation a algorithm called "Link - the Cut - Tree", he thinks that this algorithm is very beautiful, so he said "lcttxdy" every day, one day, he picked a n? M board, but this made him very strange is on the board every piece has a small letters, he wanted to know how many letters are there in together can constitute a 'lcttxdy', (note: only when a letter on the left, right, upper left, lower left, upper right and lower right, we call this two letters together)
Enter
The first line contains an integer t (1 t 100 or less) or less, input the number of test cases,
First lines of each test case contains two integers n and m, the length and width of checkerboard (1 acuities were n, m, 100 or less)
The following n lines each line contains m letters, they are in English lowercase
O
For each case, the output two lines:
In the first line of the output "YES" or "NO" - to find or not find "lcttxdy
"Output the number of "lcttxdy" in the second line, if not found print - 1


Strives for the code

CodePudding user response:

 public static void main (String [] args) {
F (10, 10);
}
/m/n chessboard
Public static void F (int n, int m) {
Char [] [] charset=new char [n] [m].
for (int i=0; I & lt; n; I++) {
For (int j=0; J & lt; m; J++) {
Charset [I] [j]=(char) (Math. The random () * 26 + 97);
System. The out. Print (charset [I] [j]);
}
System.out.println();

}

Select (new char [] [] {new char [] {' l ', 'c', 't', 't', 'x', 'd', 'y'}});


}
The static LinkedList String1=new LinkedList (a);
//1 x y a current board subscript cha find characters
Public static void Seach (char [] [] qi, int x, int y, int, char [] cha) {
If (a==cha. Length - 1) {
System. The out. Println (" successful,,,,,, ");
String1. ForEach (e - & gt; System. The out. Println (e));
System.exit(0);
return ;
}
String ok=isok (qi, x, y, cha [a]);
If (ok!=null) {
String [] s=ok. The split (", ");
String1. The add (ok);
Seach (qi, Integer. ParseInt (s [0]), an Integer. The parseInt (s) [1], a + 1, cha);
String1. RemoveLast ();
}


}
Public static void the select (char [] [] qi) {
For (int I=0; I & lt; Qi. Length; I++) {
For (int j=0; J & lt; Qi [0]. Length; J++) {
Seach (qi, I, j, 0, new char [] {' l ', 'c', 't', 't', 'x', 'd', 'y'});
}
}
}
//query and returns the coordinates
Public static String isok (char [] [] qi, int x, int y, char a) {

If (ishh (qi, x + 1, y, a)) return (x + 1) + ", "+ y;
If (ishh (qi, 1 x, y, a)) return (x - 1) + ", "+ y;
If (ishh (qi, 1 x, y + 1, a)) return (x - 1) + ", "+ (y + 1);
If (ishh (qi, x 1, y - 1, a)) return (x - 1) + ", "+ (y - 1);
If (ishh (qi, x + 1, y + 1, a)) return (x + 1) + ", "+ (y + 1);
If (ishh (qi, x + 1, y - 1, a)) return (x + 1) + ", "+ (y - 1);
If (ishh (qi, x, y + 1, a)) return x + ", "+ (y + 1);
If (ishh (qi, x, y - 1, a)) return x + ", "+ (y - 1);



return null;
}
Public static Boolean ishh (char [] [] qi, int x, int y, char a) {
Try {
Return qi [x] [y]==a;
} the catch (Exception e) {
return false;
}

}
select to randomly generated above the line but the face ah,,,,
  • Related