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

πŸ“‘ λŒ“κΈ€ ν…œν”Œλ¦Ώ

μ½”λ“œ 풀이

```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

πŸ“‘ λŒ“κΈ€ ν…œν”Œλ¦Ώ

μ½”λ“œ 풀이

```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; } } } ```

μ½”λ©˜νŠΈ

- λΉ‘ κ΅¬ν˜„ 문제 μ˜€λ˜ 것 κ°™μŠ΅λ‹ˆλ‹Ή.. μ‚­μ œ λΉ„μš©μ„ 쀄이기 μœ„ν•΄μ„œ 큐 자료ꡬ쑰λ₯Ό μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€!