fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. /*
  5. // vector = array dinamis
  6. vector<int> arr;
  7. cout << arr.size() << endl;
  8. for(int i = 0; i < 10; i++)
  9. arr.push_back(i);
  10. cout << arr.size() << endl;
  11. for(int i = 0; i < 10; i++)
  12. cout << arr[i] << " ";
  13. cout << endl;
  14. */
  15.  
  16. /*
  17. pair<string, double> data;
  18. data.first = "Ini string";
  19. data.second = 1.2345;
  20. data = make_pair("Ini string", 1.2345);
  21. */
  22.  
  23. vector<int> adjList[10];
  24. bool visited[10];
  25. int n = 7, e;
  26.  
  27. void dfs(int node) {
  28. if(visited[node]) return;
  29. visited[node] = true;
  30. // cout << node << endl;
  31. for(int i = 0; i < adjList[node].size(); i++)
  32. dfs(adjList[node][i]);
  33. }
  34.  
  35. int R, C;
  36. string grid[100];
  37. int dx[] = {0, 1, 0, -1};
  38. int dy[] = {1, 0, -1, 0};
  39.  
  40. void ff(int x, int y, char ganti) {
  41. grid[x][y] = ganti;
  42. int xx, yy;
  43. for(int i = 0; i < 4; i++) {
  44. xx = x+dx[i];
  45. yy = y+dy[i];
  46. if(xx >= 0 && xx < R && yy >= 0 && yy < C && grid[xx][yy] == '.')
  47. ff(xx, yy, ganti);
  48. }
  49. }
  50.  
  51. int main() {
  52. // Teori Graf
  53. // Himpunan titik & himpunan garis yg masing2 menghubungkan 2 titik
  54. // Directed vs undirected
  55. // Weighted vs unweighted
  56. // - 3 representasi graf (Adj Matrix, Adj List, Edge List)
  57. // Adjacency Matrix
  58. // pro: cepet, koding sederhana
  59. // con: memory n^2
  60. /*
  61. int adjMat[6][6];
  62. for(int i = 0; i < 6; i++)
  63. for(int j = 0; j < 6; j++)
  64. adjMat[i][j] = 0;
  65. cin >> e;
  66. for(int i = 0; i < e; i++) {
  67. int a, b, c;
  68. cin >> a >> b >> c;
  69. adjMat[a][b] = c;
  70. }
  71. for(int i = 0; i < 6; i++) {
  72. for(int j = 0; j < 6; j++)
  73. printf("%2d ", adjMat[i][j]);
  74. cout << endl;
  75. }
  76. */
  77. // Adjacency List
  78. // pro: tengah2 antara adj Matrix & Edge list
  79. // con: -
  80. /*
  81. vector<pair<int, int> > adjList[6];
  82. cin >> e;
  83. for(int i = 0; i < e; i++) {
  84. int a, b, c;
  85. cin >> a >> b >> c;
  86. adjList[a].push_back(make_pair(b, c));
  87. }
  88. for(int i = 0; i < 6; i++) {
  89. cout << i << ": ";
  90. for(int j = 0; j < adjList[i].size(); j++)
  91. cout << "(" << adjList[i][j].first << ", "
  92. << adjList[i][j].second << ") ";
  93. cout << endl;
  94. }
  95. */
  96.  
  97. // Object-oriented programming
  98.  
  99. // Edge List
  100. /*
  101. vector<pair<int, pair<int, int> > > edgeList;
  102. cin >> e;
  103. for(int i = 0; i < e; i++) {
  104. int a, b, c;
  105. cin >> a >> b >> c;
  106. edgeList.push_back(make_pair(c, make_pair(a, b)));
  107. }
  108. sort(edgeList.begin(), edgeList.end());
  109. for(int i = 0; i < e; i++)
  110. cout << "(" << edgeList[i].second.first << ", "
  111. << edgeList[i].second.second << "): "
  112. << edgeList[i].first << endl;
  113. */
  114.  
  115. // - Graph traversal: Depth-First Search
  116. /*
  117. cin >> n >> e;
  118. for(int i = 0; i < e; i++) {
  119. int a, b;
  120. cin >> a >> b;
  121. adjList[a].push_back(b);
  122. }
  123. int start = 1;
  124. for(int i = 0; i < n; i++)
  125. visited[i] = false;
  126. dfs(start);
  127. for(int i = 0; i < n; i++)
  128. if(visited[i])
  129. cout << i << endl;
  130. */
  131.  
  132. // - Flood-fill on implicit graph
  133. cin >> R >> C;
  134. for(int i = 0; i < R; i++)
  135. cin >> grid[i];
  136. char ganti = 'a';
  137. for(int i = 0; i < R; i++)
  138. for(int j = 0; j < C; j++)
  139. if(grid[i][j] == '.') {
  140. ff(i, j, ganti++);
  141. for(int k = 0; k < R; k++)
  142. cout << grid[k] << endl;
  143. cout << endl;
  144. }
  145.  
  146. /*
  147. ##########
  148. #..##....#
  149. #####....#
  150. #...###..#
  151. #.....#..#
  152. #....#####
  153. #....#...#
  154. ##########
  155. v
  156. ##########
  157. #..##....#
  158. #####....#
  159. #aaa###..#
  160. #aaaaa#..#
  161. #aaaa#####
  162. #aaaa#...#
  163. ##########
  164.  
  165. ##########
  166. #....#...#
  167. #....#.#.#
  168. ######.###
  169. #....#...#
  170. #.######.#
  171. #........#
  172. ##########
  173.  
  174. */
  175.  
  176. return 0;
  177. }
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204. // https://o...content-available-to-author-only...s.com/contests/yhx2yb
Success #stdin #stdout 0.01s 5280KB
stdin
8 10
##########
#..##....#
#####....#
#...###..#
#.....#..#
#....#####
#....#...#
##########
stdout
##########
#aa##....#
#####....#
#...###..#
#.....#..#
#....#####
#....#...#
##########

##########
#aa##bbbb#
#####bbbb#
#...###bb#
#.....#bb#
#....#####
#....#...#
##########

##########
#aa##bbbb#
#####bbbb#
#ccc###bb#
#ccccc#bb#
#cccc#####
#cccc#...#
##########

##########
#aa##bbbb#
#####bbbb#
#ccc###bb#
#ccccc#bb#
#cccc#####
#cccc#ddd#
##########