issues
search
2024-TEAM-05
/
algorithm-for-kakao
μΉ΄μΉ΄μ€ κΈ°μΆ λ¬Έμ κ°μ¦μπ£
0
stars
0
forks
source link
[λ°±μ€] Puyo Puyo
#35
Open
hye-on
opened
3 weeks ago
hye-on
commented
3 weeks ago
π
Puyo Puyo
hye-on
commented
3 weeks ago
π λκΈ ν νλ¦Ώ
Language : C++
μ±λ₯
μ½λ νμ΄
```cpp #include
#include
#include
#include
using namespace std; //11:39 vector
>puyo(12,vector
(6)); bool visit[12][6]; bool isPop; int h=-1; //λμ΄ κ΅¬νκΈ° int dy[] = {0,1,0,-1}; int dx[] = {1,0,-1,0}; bool OOB(int y,int x){ if(y>=12 || y<0 || x>=6 || x<0) return true; return false; } //bfs bool playPuyo(int y,int x,char Color){ queue
>q; q.push({y,x}); visit[y][x] = true; int cnt=0; while(!q.empty()){ int curY = q.front().first; int curX = q.front().second; q.pop(); for(int i=0;i<4;i++){ int ny = curY + dy[i]; int nx = curX + dx[i]; if(OOB(ny,nx)) continue; if(!visit[ny][nx] && puyo[ny][nx]==Color){ q.push({ny,nx}); visit[ny][nx] = true; cnt++; } } } // cout<
=3){ //visit true μΈκ² .μΌλ‘ λ§λ€κΈ° for(int i=h;i<12;i++){ for(int j=0;j<6;j++){ if(visit[i][j]){ puyo[i][j]='.'; } } } return true; } else{ for(int i=h;i<12;i++){ for(int j=0;j<6;j++){ visit[i][j]=false; } } return false; } } void gravitation(){ for(int j=0;j<6;j++){ for(int i = 11;i>=h;i--){ if(puyo[i][j]!='.'){ int y = i+1; while(y<12 && puyo[y][j]=='.'){ puyo[y][j] = puyo[y-1][j]; puyo[y-1][j]='.'; y++; } } } } } int main() { string tmp=""; for(int i=0;i<12;i++){ cin>>tmp; int t=0; for(int j=0;j<6;j++){ puyo[i][j]=tmp[j]; if(tmp[j]=='.') t++; } } for(int i=0;i<12;i++){ for(int j=0;j<6;j++){ if(puyo[i][j]!='.'){ h=i; break; } } if(h>=0) break; } int answer=0; if(h < 0) h = 0; while(true){ for(int i=h;i<12;i++){ for(int j=0;j<6;j++){ if(puyo[i][j]!='.' && !visit[i][j]){ if(playPuyo(i,j, puyo[i][j])){ isPop =true; } } } } //visit μ΄κΈ°ν memset(visit,false,sizeof(visit)); gravitation(); if(isPop) answer++; else break; isPop=false; } cout<
μ½λ©νΈ
- μ΄λ ΅μ§ μμλλ° λΉ¨λ¦¬ νμ§ λͺ»νλ€μ. .μ²λ¦¬λ₯Ό ν΄μ£Όλ μμ μ΄ ν¨μ¨μ μΈμ§ λͺ¨λ₯΄κ² μ΅λλ€.
uijin-j
commented
3 weeks ago
π λκΈ ν νλ¦Ώ
Language : Java
μ±λ₯
μ½λ νμ΄
```java import java.io.*; import java.util.*; /** * 20:00 μμ! */ public class Main { static char[][] snapshot; static Deque
[] map; public static void main(String[] args) throws Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); map = new Deque[6]; for(int i = 0; i < 6; ++i) map[i] = new ArrayDeque<>(); snapshot = new char[6][12]; // map[i][j]λ iμ΄μ jν μλκ° 0ν! for(int i = 0; i < 12; ++i) { String line = bf.readLine(); char[] chars = line.toCharArray(); for(int j = 0; j < 6; ++j) { snapshot[j][i] = chars[j]; map[j].offer(chars[j]); } } int count = 0; while(hasBomb()) { // ν°μ§ λΏμλ€μ΄ μλ€λ©΄? bomb(); count++; } System.out.println(count); } static boolean[][] toRemove; static boolean[][] visit; public static boolean hasBomb() { boolean hasBomb = false; toRemove = new boolean[6][12]; // μ κ±°ν΄μΌ νλ λΏμ visit = new boolean[6][12]; for(int i = 0; i < 6; ++i) { for(int j = 0; j < 12; ++j) { if(snapshot[i][j] == '.' || visit[i][j]) continue; boolean[][] connect = new boolean[6][12]; int count = check(i, j, connect); // μλ μ°κ²°λ μ λ€μ΄ 4κ° μ΄μμΈμ§ νμΈ! if(count >= 4) { hasBomb = true; mark(connect, toRemove); } } } return hasBomb; } static int[] dx = {-1, 0, 1, 0}; static int[] dy = {0, 1, 0, -1}; public static int check(int col, int row, boolean[][] connect) { connect[col][row] = true; visit[col][row] = true; int total = 1; for(int d = 0; d < 4; ++d) { int nCol = col + dx[d]; int nRow = row + dy[d]; if(nCol >= 0 && nCol < 6 && nRow >= 0 && nRow < 12 && !connect[nCol][nRow] && snapshot[nCol][nRow] == snapshot[col][row]) { total += check(nCol, nRow, connect); } } return total; } public static void mark(boolean[][] connect, boolean[][] toRemove) { for(int i = 0; i < 6; ++i) { for(int j = 0; j < 12; ++j) { if(connect[i][j]) { toRemove[i][j] = true; } } } } public static void bomb() { for(int i = 0; i < 6; ++i) { Deque
deque = new ArrayDeque<>(); for(int j = 0; j < 12; ++j) { Character puyo = map[i].poll(); if(!toRemove[i][j]) { deque.offer(puyo); } } while(deque.size() < 12) { deque.offerFirst('.'); } for(int j = 0; j < 12; ++j) { Character puyo = deque.poll(); snapshot[i][j] = puyo; deque.offer(puyo); } map[i] = deque; } } } ```
μ½λ©νΈ
- λΉ‘ ꡬν λ¬Έμ μλ κ² κ°μ΅λλΉ.. μμ λΉμ©μ μ€μ΄κΈ° μν΄μ ν μλ£κ΅¬μ‘°λ₯Ό μ¬μ©νμ΅λλ€!
π Puyo Puyo