fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. bool checkExtremeDiagonals(vector<string>& grid, int n, int m) {
  6. char topLeft = grid[0][0];
  7. char bottomLeft = grid[n - 1][0];
  8. char topRight = grid[0][m - 1];
  9. char bottomRight = grid[n - 1][m - 1];
  10. return topLeft == bottomRight && bottomLeft == topRight;
  11. }
  12.  
  13. bool checkRectangles(vector<string>& grid, int n, int m) {
  14. // Check horizontal and vertical pairs
  15. for (int i = 0; i < n - 1; ++i) {
  16. for (int j = 0; j < m - 1; ++j) {
  17. if (grid[i][j] != grid[i + 1][j] && grid[i][j] != grid[i][j + 1]) {
  18. return false;
  19. }
  20. }
  21. }
  22. // Check extreme diagonals
  23. return checkExtremeDiagonals(grid, n, m);
  24. }
  25.  
  26. int main() {
  27. int t;
  28. cin >> t;
  29.  
  30. while (t--) {
  31. int n, m;
  32. cin >> n >> m;
  33. vector<string> grid(n);
  34. for (int i = 0; i < n; ++i) {
  35. cin >> grid[i];
  36. }
  37.  
  38. bool possible = checkRectangles(grid, n, m);
  39.  
  40. if (possible) {
  41. cout << "YES" << endl;
  42. } else {
  43. cout << "NO" << endl;
  44. }
  45. }
  46.  
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0.01s 5308KB
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
NO
NO
YES
YES
NO
NO
NO
NO