fork download
  1. #include <bits/stdc++.h>
  2. #define pi pair<int, int>
  3. #define x first
  4. #define y second
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. int t;
  11. cin>>t;
  12. std::vector<string> re;
  13. for(int i=0;i<t;i++){
  14. int n,m;
  15. cin>>n>>m;
  16. char ar[n][m];
  17. bool v[n][m];
  18. int numG,numB;
  19.  
  20. for(int j=0;j<n;j++){
  21. for(int k=0;k<m;k++){
  22. v[j][k] = false;
  23. cin>>ar[j][k];
  24. if(ar[j][k]=='G') numG++;
  25. else if(ar[j][k]=='B') numB++;
  26. else if(ar[j][k]=='#') v[j][k] = true;
  27.  
  28. }
  29. }
  30.  
  31. // cout<<"\n-----------------------------------------------------\n";
  32. // for(int j=0;j<n;j++){
  33. // cout<<"\n";
  34. // for(int k=0;k<m;k++){
  35. // cout<<" "<<ar[j][k];
  36.  
  37. // }
  38. // }
  39.  
  40. bool needBreak = false;
  41. if(numG==0) {
  42. re.push_back("Yes");
  43. needBreak = true;
  44. continue;
  45. }
  46.  
  47. int dx[] = {0,0,1,-1};
  48. int dy[] = {1,-1,0,0};
  49.  
  50.  
  51.  
  52. for(int j=0;j<n;j++){
  53. for(int k=0;k<m;k++){
  54. if(ar[j][k]=='B'){
  55. if(needBreak) break;
  56. v[j][k] = true;
  57. for(int h=0;h<4;h++){
  58. //cout<<"\n j "<<j<<" k "<<k <<" h "<<h;
  59. int x = j+dx[h];
  60. int y = k+dy[h];
  61. if(x<0 || y<0||x>=n||y>=m) continue;
  62. //cout<<"\n --> x "<<x<<" y "<<y;
  63. if(ar[x][y]=='G') {
  64. needBreak = true;
  65. re.push_back("No");
  66. //cout<<"\n -->break: x "<<x<<" y "<<y;
  67. break;
  68. } else if(ar[x][y]=='.') {
  69. ar[x][y]='#';
  70. v[x][y] = true;
  71. }
  72.  
  73. }
  74.  
  75. }
  76. }
  77. }
  78.  
  79. if(needBreak) continue;
  80. else {
  81. if(ar[n-1][m-1]=='#'){
  82. re.push_back("No");
  83. continue;
  84. }
  85. int count = 0;
  86. std::queue<pi> q ;
  87. q.push({n-1, m-1});
  88. v[n-1][m-1] = true;
  89. while(!q.empty()){
  90. pi top = q.front();
  91. q.pop();
  92.  
  93. for(int h=0;h<4;h++){
  94. //cout<<"\n j "<<j<<" k "<<k <<" h "<<h;
  95. int x = top.x+dx[h];
  96. int y = top.y+dy[h];
  97. if(x<0 || y<0||x>=n||y>=m || v[x][y]) continue;
  98. //cout<<"\n --> x "<<x<<" y "<<y;
  99. q.push({x,y});
  100. v[x][y] = true;
  101. if(ar[x][y]=='G') {
  102. count++;
  103. //cout<<"\n -->break: x "<<x<<" y "<<y;
  104. }
  105.  
  106. }
  107. }
  108. if(count>=numG) re.push_back("Yes");
  109. else re.push_back("No");
  110.  
  111. }
  112.  
  113. }
  114.  
  115. for(auto s:re){
  116. cout<<s<<"\n";
  117. }
  118.  
  119. return 0;
  120. }
Success #stdin #stdout 0.01s 5276KB
stdin
1
1 1
.
stdout
No