회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
1
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
c++ 어느부분이 틀린지 잘모르겠습니다.
12100번 - 2048 (Easy)
dobidugi
2년 전
0
게시판 반례를 넣어봐도 알맞게 작동하는거같은데
어느부분이 틀린지 모르겠어요 ㅠㅠ
#include <iostream> #include <queue> using namespace std; int n; int _max; void left(vector<vector<int>> vec, int move); void right(vector<vector<int>> vec, int move); void up(vector<vector<int>> vec, int move); void down(vector<vector<int>> vec, int move); void printMap(vector<vector<int>> vec) { for(int i=0; i<n; i++) { for(int j=0; j<n; j++) cout << vec[i][j] << " "; cout << endl; } } bool isEnd(int cnt) { return cnt == 5; } void left(vector<vector<int>> vec, int move) { if(isEnd(move)) return; for(int i=0; i<n; i++) { queue<int> q, res; int zero = 0; for(int j=0; j<n; j++) { if(vec[i][j] !=0) q.push(vec[i][j]); //0 이 아닌것 큐에넣기 else zero++; // 0 갯수 } while(!q.empty()) { int tmp = q.front(); q.pop(); if(q.empty()) { res.push(tmp); // 하나 꺼내고 큐가 비었음 멈춤 break; } else if(tmp == q.front()) { _max = max(_max, tmp*2); res.push(tmp*2); q.pop(); zero++; // 두개를 합쳤으니 0갯수 증 } else if(tmp != q.front()) { res.push(tmp); // 현재값하고 다음값 넣음 res.push(q.front()); q.pop(); } } for(int j=0; j<zero; j++) { res.push(0); // 0갯수 채우기 } for(int j=0; j<n; j++) { vec[i][j] = res.front(); res.pop(); // 배열에 넣기 } } left(vec, move+1); right(vec, move+1); up(vec, move+1); down(vec, move+1); } void right(vector<vector<int>> vec, int move) { if(isEnd(move)) return; for(int i=0; i<n; i++) { queue<int> q, res; int zero = 0; for(int j=n-1; j>=0; j--) { if(vec[i][j] !=0) q.push(vec[i][j]); else zero++; } while(!q.empty()) { int tmp = q.front(); q.pop(); if(q.empty()) { res.push(tmp); break; } else if(tmp == q.front()) { _max = max(_max, tmp*2); res.push(tmp*2); q.pop(); zero++; } else if(tmp != q.front()) { res.push(tmp); res.push(q.front()); q.pop(); } } for(int j=0; j<zero; j++) { res.push(0); } for(int j=n-1; j>=0; j--) { vec[i][j] = res.front(); res.pop(); } } left(vec, move+1); right(vec, move+1); up(vec, move+1); down(vec, move+1); } void up(vector<vector<int>> vec, int move) { if(isEnd(move)) return; for(int i=0; i<n; i++) { queue<int> q, res; int zero = 0; for(int j=0; j<n; j++) { if(vec[j][i] !=0) q.push(vec[j][i]); else zero++; } while(!q.empty()) { int tmp = q.front(); q.pop(); if(q.empty()) { res.push(tmp); break; } else if(tmp == q.front()) { _max = max(_max, tmp*2); res.push(tmp*2); q.pop(); zero++; } else if(tmp != q.front()) { res.push(tmp); res.push(q.front()); q.pop(); } } for(int j=0; j<zero; j++) { res.push(0); } for(int j=0; j<n; j++) { vec[j][i] = res.front(); res.pop(); } } left(vec, move+1); right(vec, move+1); up(vec, move+1); down(vec, move+1); } void down(vector<vector<int>> vec, int move) { if(isEnd(move)) return; for(int i=0; i<n; i++) { queue<int> q, res; int zero = 0; for(int j=n-1; j>=0; j--) { if(vec[j][i] !=0) q.push(vec[j][i]); else zero++; } while(!q.empty()) { int tmp = q.front(); q.pop(); if(q.empty()) { res.push(tmp); break; } else if(tmp == q.front()) { _max = max(_max, tmp*2); res.push(tmp*2); q.pop(); zero++; } else if(tmp != q.front()) { res.push(tmp); res.push(q.front()); q.pop(); } } for(int j=0; j<zero; j++) { res.push(0); } for(int j=n-1; j>=0; j--) { vec[j][i] = res.front(); res.pop(); } } left(vec, move+1); right(vec, move+1); up(vec, move+1); down(vec, move+1); } void dfs(vector<vector<int>>& vec){ left(vec, 0); right(vec, 0); up(vec, 0); down(vec, 0); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; vector<vector<int>> vec(n, vector<int>(n)); for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { cin >> vec[i][j]; _max = max(_max, vec[i][j]); } } dfs(vec); cout << _max; return 0; }
댓글을 작성하려면
로그인
해야 합니다.
dobidugi 2년 전
게시판 반례를 넣어봐도 알맞게 작동하는거같은데
어느부분이 틀린지 모르겠어요 ㅠㅠ