fork download
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <cctype>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <stack>
  8. #include <queue>
  9. #include <deque>
  10. #include <utility>
  11.  
  12. using namespace std;//이름공간 선언
  13. string maps[12][6];
  14. int visit[12][6];//방문 기록
  15. int dx[] = {-1,1,0,0};
  16. int dy[] = {0,0,-1,1};
  17. int ans = 0;//연쇄 폭발 횟수
  18.  
  19. //입력
  20. void input(){
  21. for(int i=0;i<12;i++){
  22. for(int j=0;j<6;j++){
  23. cin>>maps[i][j];
  24. }
  25. }
  26. }
  27.  
  28. //초기화
  29. void init(){
  30. for(int i=0;i<12;i++){
  31. for(int j=0;j<6;j++){
  32. visit[i][j] = 0;
  33. }
  34. }
  35. }
  36.  
  37. //bfs탐색
  38. bool bfs(int x,int y){
  39. init();//방문지점 초기화
  40. string color = maps[x][y];
  41. int cnt = 0;//개수 체크
  42. bool flag = false;//폭발이 일어나는가?
  43. queue<pair<int,int>> q;
  44. queue<pair<int,int>> boom;//폭발한 위치를 담는 큐
  45. q.push({x,y});
  46. boom.push({x,y});
  47.  
  48. while(!q.empty()){
  49. int cx = q.front().first;
  50. int cy = q.front().second;
  51. visit[cx][cy] = 1;//방문체크
  52. cnt++;//개수 세기
  53. q.pop();
  54.  
  55. //다음지점 탐색
  56. for(int i=0;i<4;i++){
  57. int nx = cx + dx[i];
  58. int ny = cy + dy[i];
  59. //범위내에 있고 미방문,색깔이 같으면
  60. if(nx>=0 && nx<12 && ny>=0 && ny<6){
  61. if(maps[nx][ny]==color && visit[nx][ny]==0){
  62. visit[nx][ny] = 1;//방문체크
  63. q.push({nx,ny});
  64. boom.push({nx,ny});//좌표 넣기
  65. }
  66. }
  67. }
  68. }
  69.  
  70. //폭발이 일어나는가?
  71. if(cnt>=4){
  72. flag = true;
  73. //터졌으니 빈공간으로
  74. while(!boom.empty()){
  75. int bx = boom.front().first;
  76. int by = boom.front().second;
  77. maps[bx][by] = ".";
  78. boom.pop();
  79. }
  80. }
  81. return flag;
  82. }
  83.  
  84. //내리기
  85. void godown(){
  86. for(int j=0;j<6;j++){
  87. for(int i=11;i>=0;i--){
  88. if(maps[i][j] == "."){
  89. //.가 안나올때까지
  90. for(int k=i-1;k>=0;k--){
  91. if(maps[k][j] != "."){
  92. maps[i][j] = maps[k][j];
  93. maps[k][j] = ".";
  94. break;
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101.  
  102. //뿌요뿌요
  103. void puyo(){
  104. //연쇄가 일어나지 않을때까지 탐색
  105. while(true){
  106. bool boom_flag = false;//터졌는가?
  107. //맵 탐색
  108. for(int i=11;i>=0;i--){
  109. for(int j=0;j<6;j++){
  110. if(maps[i][j] != "."){
  111. if(bfs(i,j)==true){
  112. boom_flag = true;
  113. }
  114. }
  115. }
  116. }
  117. //연쇄 폭발이 이루어 졌으면 한번 더
  118. if(boom_flag == true){
  119. godown();//내리기
  120. ans++;
  121. }else{
  122. return;//반목문 종료
  123. }
  124. }
  125. }
  126. int main(){
  127. cin.tie(0);
  128. cout.tie(0);
  129.  
  130. input();//입력
  131. puyo();//탐색하기
  132. cout<<ans;//출력
  133. return 0;
  134. }
Success #stdin #stdout 0s 5540KB
stdin
......
......
......
......
......
......
......
......
.Y....
.YG...
RRYG..
RRYGG.
stdout
1