fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define go ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
  4. using namespace std;
  5. char getNextState(char currState)
  6. {
  7. if (currState == 'B')
  8. return 'W';
  9. return 'B';
  10. }
  11. int const N = 110;
  12. int n, m;
  13. char maze[N][N];
  14. bool visited[N][N];
  15. void dfs(int x, int y, char state)
  16. {
  17. if (x >= n || x < 0 || y >= m || y < 0)
  18. return;
  19. if (visited[x][y])
  20. {
  21. return;
  22. }
  23. visited[x][y] = true;
  24. if (maze[x][y] == '-')
  25. return;
  26. maze[x][y] = state;
  27. int i[] = {1, -1, 0, 0};
  28. int j[] = {0, 0, 1, -1};
  29. for (int k = 0; k < 4; k++)
  30. {
  31. dfs(x + i[k], y + j[k], getNextState(state));
  32. }
  33. }
  34. void solve()
  35. {
  36. cin >> n >> m;
  37. for (int i = 0; i < n; i++)
  38. {
  39. for (int j = 0; j < m; j++)
  40. {
  41. cin >> maze[i][j];
  42. }
  43. }
  44. for (int x = 0; x < n; x++)
  45. {
  46. for (int y = 0; y < m; y++)
  47. {
  48. if (!visited[x][y])
  49. dfs(x, y, 'B');
  50. }
  51. }
  52. for (int i = 0; i < n; i++)
  53. {
  54. for (int j = 0; j < m; j++)
  55. {
  56. cout << maze[i][j];
  57. }
  58. cout << "\n";
  59. }
  60. }
  61.  
  62. int main()
  63. {
  64. int t = 1;
  65. // cin>>t;
  66. // go;
  67. while (t--)
  68. {
  69. solve();
  70. cout << "\n";
  71. }
  72. return 0;
  73. }
Success #stdin #stdout 0.01s 5432KB
stdin
3 3
.-.
---
--.
stdout
B-B
---
--B