fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. bool checkOperations(vector<string>& grid, int n, int m) {
  6. for (int i = 0; i < n; ++i) {
  7. for (int j = 0; j < m; ++j) {
  8. // Find pairs of squares with the same color
  9. for (int x = i; x < n; ++x) {
  10. for (int y = j; y < m; ++y) {
  11. if (grid[i][j] == grid[x][y]) {
  12. // Check if a rectangle can be formed to change the color of all squares in between
  13. bool possible = true;
  14. for (int p = i; p <= x && possible; ++p) {
  15. for (int q = j; q <= y && possible; ++q) {
  16. if (grid[p][q] != grid[i][j]) {
  17. possible = false;
  18. }
  19. }
  20. }
  21. // If a rectangle can be formed, return true
  22. if (possible) {
  23. return true;
  24. }
  25. }
  26. }
  27. }
  28. }
  29. }
  30. // If no suitable rectangles found, return false
  31. return false;
  32. }
  33.  
  34. int main() {
  35. int t;
  36. cin >> t;
  37.  
  38. while (t--) {
  39. int n, m;
  40. cin >> n >> m;
  41. vector<string> grid(n);
  42. for (int i = 0; i < n; ++i) {
  43. cin >> grid[i];
  44. }
  45.  
  46. bool possible = checkOperations(grid, n, m);
  47.  
  48. if (possible) {
  49. cout << "YES" << endl;
  50. } else {
  51. cout << "NO" << endl;
  52. }
  53. }
  54.  
  55. return 0;
  56. }
  57.  
Success #stdin #stdout 0.01s 5276KB
stdin
8
2 1
W
B
6 6
WWWWBW
WBWWWW
BBBWWW
BWWWBB
WWBWBB
BBBWBW
1 1
W
2 2
BB
BB
3 4
BWBW
WBWB
BWBW
4 2
BB
BB
WW
WW
4 4
WWBW
BBWB
WWBB
BBBB
1 5
WBBWB
stdout
YES
YES
YES
YES
YES
YES
YES
YES