fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. # define ll long long
  4. void fastIO(void) {
  5. ios_base::sync_with_stdio(false);
  6. cin.tie(NULL);
  7. cout.tie(NULL);
  8. }
  9. char mapGame[50][50];
  10. bool movable[50][50];
  11. int W, H , row, col;
  12. int dr[] = {1, 0, -1,0};
  13. int dc[] = {0, 1, 0, -1};
  14. int gold = 0;
  15. bool isIn(int r, int c){
  16. return r < W && r >= 0 && c < H && c >= 0;
  17. }
  18. void dfs(int r, int c){
  19. if( mapGame[r][c] == '#')
  20. return;
  21. if(mapGame[r][c] == 'G')
  22. gold++;
  23. mapGame[r][c] = '#';
  24. if(movable[r][c])
  25. for(int k = 0; k < 4; k++ )
  26. if(isIn(r + dr[k], c + dc[k]))
  27. dfs(r + dr[k], c+dc[k]);
  28. }
  29. int main() {
  30. fastIO();
  31. while(cin >> H >> W){
  32. for (int i = 0; i < W; i++)
  33. cin >> mapGame[i];
  34. memset(movable, true, sizeof(movable));
  35. for (int i = 0; i < W; i++)
  36. for (int j = 0; j < H; j++)
  37. for (int k = 0; k < 4; k++) {
  38. row = i + dr[k];
  39. col = j + dc[k];
  40. if (isIn(row, col) && mapGame[row][col] == 'T')
  41. movable[i][j] = false;
  42. }
  43. for (int r = 0; r < W; r++)
  44. for (int c = 0; c < H; c++)
  45. if (mapGame[r][c] == 'T')
  46. mapGame[r][c] = '#';
  47. else if (mapGame[r][c] == 'P') {
  48. row = r;
  49. col = c;
  50. }
  51. gold = 0;
  52. dfs(row, col);
  53. cout << gold << "\n";
  54. }
  55. return 0;
  56. }
  57.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Standard output is empty