fork download
  1. #include<bits/stdc++.h>
  2. #define T long long t;cin >>t;while(t--)
  3. #define fast ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
  4. #define ll long long
  5. #define lll unsigned long long
  6. using namespace std;
  7. int x,y;
  8. char grid[40][40];
  9. vector<pair<int,int>>v;
  10. int mx=0;
  11. void solve(int i,int c){
  12. if (i==v.size()){
  13. if (c>mx) mx=c;
  14. return;
  15. }
  16. if ((v[i].first+1<x)&&grid[v[i].first+1][v[i].second]=='G'){
  17. grid[v[i].first+1][v[i].second]='.';
  18. solve(i+1,c+1);
  19. grid[v[i].first+1][v[i].second]='G';
  20. }
  21. if ((v[i].first-1>=0)&&grid[v[i].first-1][v[i].second]=='G'){
  22. grid[v[i].first-1][v[i].second]='.';
  23. solve(i+1,c+1);
  24. grid[v[i].first-1][v[i].second]='G';
  25. }
  26. if ((v[i].second+1<y)&&grid[v[i].first][v[i].second+1]=='G'){
  27. grid[v[i].first][v[i].second+1]='.';
  28. solve(i+1,c+1);
  29. grid[v[i].first][v[i].second+1]='G';
  30. }
  31. if ((v[i].second-1>=0)&&grid[v[i].first][v[i].second-1]=='G'){
  32. grid[v[i].first][v[i].second-1]='.';
  33. solve(i+1,c+1);
  34. grid[v[i].first][v[i].second-1]='G';
  35. }
  36. }
  37.  
  38. void Abady(){
  39. cin >> x >> y;
  40. for (int i=0;i<x;i++){
  41. for (int j=0;j<y;j++){
  42. cin >> grid[i][j];
  43. if (grid[i][j]=='W') v.push_back({i,j});
  44. }
  45. }
  46. solve(0,0);
  47. cout << mx << endl;
  48. }
  49. int main(){
  50. fast;
  51. Abady();
  52. }
  53.  
  54.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
0