G.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n, m, sx, sy;
  4. int dir[4][2] = {
  5. {1, 0},
  6. {-1, 0},
  7. {0, 1},
  8. {0, -1}
  9. };
  10. char a[50][50];
  11. int book[50][50];
  12. int cnt = 0;
  13. int f(int x, int y);
  14. void dfs(int x, int y);
  15. int main() {
  16. cin >> n >> m >> sx >> sy;
  17. for (int i = 0; i < n; i++) {
  18. cin >> a[i];
  19. }
  20. for (int i = 0; i < n; i++) {
  21. for (int j = 0; j < m; j++) {
  22. book[i][j] = 0;
  23. }
  24. }
  25. dfs(sx, sy);
  26. cout << cnt << endl;
  27. }
  28. void dfs(int x, int y) {
  29. int sum = f(x, y);
  30. if (sum > cnt)cnt = sum;
  31. if (sum == 35) {
  32. cerr << x << " " << y << endl;
  33. }
  34. for (int k = 0; k < 4; k++) {
  35. int tx = x + dir[k][0];
  36. int ty = y + dir[k][1];
  37. if (tx < 0 || ty < 0 || tx >= n || ty >= m || a[tx][ty] != '.' || book[tx][ty] == 1)continue;
  38. book[tx][ty] = 1;
  39. dfs(tx, ty);
  40. book[tx][ty] = 0;
  41. }
  42. }
  43. int f(int x, int y) {
  44. int ret = 0;
  45. int i = x, j = y;
  46. while (i >= 0 && a[i][j] != '#') {
  47. if (a[i][j] == 'G')ret++;
  48. i--;
  49. }
  50. i = x, j = y;
  51. while (i < n && a[i][j] != '#') {
  52. if (a[i][j] == 'G')ret++;
  53. i++;
  54. }
  55. i = x, j = y;
  56. while (j >= 0 && a[i][j] != '#') {
  57. if (a[i][j] == 'G')ret++;
  58. j--;
  59. }
  60. i = x;
  61. j = y;
  62. while (j < m && a[i][j] != '#') {
  63. if (a[i][j] == 'G')ret++;
  64. j++;
  65. }
  66. return ret;
  67. }