fork download
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5. public static void main(String[] args) throws IOException {
  6. PrintWriter out = new PrintWriter(System.out);
  7. int t = Integer.parseInt(in.readLine().trim());
  8. while (t-- > 0) {
  9. StringTokenizer st = new StringTokenizer(in.readLine());
  10. int n = Integer.parseInt(st.nextToken());
  11. int m = Integer.parseInt(st.nextToken());
  12. int k = Integer.parseInt(st.nextToken());
  13. char[][] grid = new char[n][m];
  14. for (int i = 0; i < n; i++) {
  15. grid[i] = in.readLine().toCharArray();
  16. }
  17. out.println(solve(n, m, k, grid));
  18. }
  19. out.flush();
  20. }
  21.  
  22. private static int solve(int n, int m, int k, char[][] g) {
  23. boolean[] rowHasEmpty = buildRowHasEmpty(n, m, g);
  24. boolean[] colHasEmpty = buildColHasEmpty(n, m, g);
  25. boolean[] rowPref = buildPrefix(rowHasEmpty);
  26. boolean[] rowSuf = buildSuffix(rowHasEmpty);
  27. boolean[] colPref = buildPrefix(colHasEmpty);
  28. boolean[] colSuf = buildSuffix(colHasEmpty);
  29.  
  30. int ans = 0;
  31. for (int i = 0; i < n; i++) {
  32. for (int j = 0; j < m; j++) {
  33. if (g[i][j] == 'g' && isCollectable(i, j, n, m, k, rowPref, rowSuf, colPref, colSuf)) {
  34. ans++;
  35. }
  36. }
  37. }
  38. return ans;
  39. }
  40.  
  41. private static boolean[] buildRowHasEmpty(int n, int m, char[][] g) {
  42. boolean[] rowE = new boolean[n];
  43. for (int i = 0; i < n; i++) {
  44. for (int j = 0; j < m; j++) {
  45. if (g[i][j] == '.') { rowE[i] = true; break; }
  46. }
  47. }
  48. return rowE;
  49. }
  50.  
  51. private static boolean[] buildColHasEmpty(int n, int m, char[][] g) {
  52. boolean[] colE = new boolean[m];
  53. for (int j = 0; j < m; j++) {
  54. for (int i = 0; i < n; i++) {
  55. if (g[i][j] == '.') { colE[j] = true; break; }
  56. }
  57. }
  58. return colE;
  59. }
  60.  
  61. private static boolean[] buildPrefix(boolean[] arr) {
  62. int len = arr.length;
  63. boolean[] pre = new boolean[len];
  64. pre[0] = arr[0];
  65. for (int i = 1; i < len; i++) {
  66. pre[i] = pre[i-1] | arr[i];
  67. }
  68. return pre;
  69. }
  70.  
  71. private static boolean[] buildSuffix(boolean[] arr) {
  72. int len = arr.length;
  73. boolean[] suf = new boolean[len];
  74. suf[len-1] = arr[len-1];
  75. for (int i = len-2; i >= 0; i--) {
  76. suf[i] = suf[i+1] | arr[i];
  77. }
  78. return suf;
  79. }
  80.  
  81. private static boolean isCollectable(int i, int j, int n, int m, int k,
  82. boolean[] rowPre, boolean[] rowSuf,
  83. boolean[] colPre, boolean[] colSuf) {
  84. if (i - k >= 0 && rowPre[i - k]) return true;
  85. if (i + k < n && rowSuf[i + k]) return true;
  86. if (j - k >= 0 && colPre[j - k]) return true;
  87. if (j + k < m && colSuf[j + k]) return true;
  88. return false;
  89. }
  90. }
  91.  
Success #stdin #stdout 0.1s 53036KB
stdin
3
2 3 1
#.#
g.g
2 3 2
#.#
g.g
3 4 2
.gg.
g..#
g##.
stdout
2
0
4