12100번 2048(easy)

동도리동·2021년 8월 17일
0

코딩테스트

목록 보기
19/76

아 해결했다!!!
아래는 오류코드임

#include <iostream>
#include <vector>
using namespace std;
int n;
int dx[4] = { -1,0,1, 0 };
int dy[4] = {  0,1,0,-1 };
int t = 0;
void DFS(int L,vector<vector<int>>& map) {
	if (L==5) {
		
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				//cout << map[i][j] << " ";
				if (t < map[i][j]) t = map[i][j];
			}
			//cout << '\n';
		}
		//cout << '\n';
		
		
		
	}
	else {
		int ans = 0;
		for (int k = 0; k < 4; k++) {
			//4방향 작업
			vector<vector<int>> ch(n, vector<int>(n));
			if (k == 0) {
				for (int i = 1; i < n; i++) {
					for (int j = 0; j < n; j++) {
						//-----------------------
						if (map[i][j] == 0) continue;
						int xx = i + dx[k];
						int yy = j + dy[k];
						int tx = i;
						int ty = j;
						
						while (xx >= 0 && xx < n && yy >= 0 && yy < n) {
							if (map[xx][yy] == 0) {
								map[xx][yy] = map[tx][ty];
								map[tx][ty] = 0;
							}
							else if (ch[xx][yy]==0 && map[xx][yy] == map[tx][ty]) {
								ch[xx][yy] = 1;
								map[xx][yy] *= 2;
								map[tx][ty] = 0;
								break;
							}
							else if (map[xx][yy] != map[tx][ty] && map[xx][yy] != 0) break;
							tx = xx;
							ty = yy;
							xx += dx[k];
							yy += dy[k];

						}
						//-----------------------
					}
				}
				
			}
			else if (k == 1) {
				for (int j = n - 2; j >= 0; j--) {
					for (int i = 0; i < n; i++) {
						//그래도 i,j로
						//-----------------------
						if (map[i][j] == 0) continue;
						int xx = i + dx[k];
						int yy = j + dy[k];
						int tx = i;
						int ty = j;
						
						while (xx >= 0 && xx < n && yy >= 0 && yy < n) {
							if (map[xx][yy] == 0) {
								map[xx][yy] = map[tx][ty];
								map[tx][ty] = 0;
							}
							else if (ch[xx][yy] == 0 && map[xx][yy] == map[tx][ty]) {
								ch[xx][yy] = 1;
								map[xx][yy] *= 2;
								map[tx][ty] = 0;
								break;
							}
							else if (map[xx][yy] != map[i][j] && map[xx][yy] != 0) break;
							tx = xx;
							ty = yy;
							xx += dx[k];
							yy += dy[k];
						}
						//-----------------------
					}
				}
				
			}
			else if (k == 2) {
				for (int i = n - 2; i >= 0; i--) {
					for (int j = 0; j < n; j++) {
						//-----------------------
						if (map[i][j] == 0) continue;
						int xx = i + dx[k];
						int yy = j + dy[k];
						int tx = i;
						int ty = j;
						
						while (xx >= 0 && xx < n && yy >= 0 && yy < n) {
							if (map[xx][yy] == 0) {
								map[xx][yy] = map[tx][ty];
								map[tx][ty] = 0;
							}
							else if (ch[xx][yy] == 0 && map[xx][yy] == map[tx][ty]) {
								ch[xx][yy] = 1;
								map[xx][yy] *= 2;
								map[tx][ty] = 0;
								break;
							}
							else if (map[xx][yy] != map[tx][ty] && map[xx][yy] != 0) break;
							tx = xx;
							ty = yy;
							xx += dx[k];
							yy += dy[k];
						}
						//-----------------------
					}
				}
				
			}
			else {
				for (int j = 1; j < n; j++) {
					for (int i = 0; i < n; i++) {
						//-----------------------
						if (map[i][j] == 0) continue;
						int xx = i + dx[k];
						int yy = j + dy[k];
						int tx = i;
						int ty = j;
						
						while (xx >= 0 && xx < n && yy >= 0 && yy < n) {
							if (map[xx][yy] == 0) {
								map[xx][yy] = map[tx][ty];
								map[tx][ty] = 0;
							}
							else if (ch[xx][yy] == 0 && map[xx][yy] == map[tx][ty]) {
								ch[xx][yy] = 1;
								map[xx][yy] *= 2;
								map[tx][ty] = 0;
								break;
							}
							else if (map[xx][yy] != map[i][j] && map[xx][yy] != 0) break;
							tx = xx;
							ty = yy;
							xx += dx[k];
							yy += dy[k];
						}
						//-----------------------
					}
				}
				
			}
			//k다 끝
			DFS(L + 1, map);
		}
		//return ans;
	}
}

int main() {
	freopen("in1.txt", "rt", stdin);
	int res = 0;
	cin >> n;
	vector<vector<int>> map(n, vector<int>(n));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> map[i][j];
		//	cout << map[i][j] << " ";
		}
	//	cout << '\n';
	}
	DFS(0, map);
	cout << t << '\n';
	return 0;
}

드디어......
아래는 정답코드!

#include <iostream>
#include <vector>
using namespace std;
int n;
int dx[4] = { -1,0,1, 0 };
int dy[4] = { 0,1,0,-1 };
int t = -1;
int dir[5];
void DFS(int L, vector<vector<int>> map) {
	if (L == 5) {
		for (int k = 0; k < 5; k++) {
			vector<vector<int>> ch(n, vector<int>(n));
			if (dir[k] == 0) {
				while (1) {
					bool ok = true;
					for (int i = 1; i < n; i++) {
						for (int j = 0; j < n; j++) {
							//-----------------------
							if (map[i][j] == 0) continue;
							if (map[i - 1][j] == 0) {
								map[i - 1][j] = map[i][j];
								map[i][j] = 0;
								ok = false;
							}
							else if (ch[i - 1][j] == 0 && map[i - 1][j] == map[i][j] && ch[i][j] == 0) {
								ch[i - 1][j] = 1;
								map[i - 1][j] *= 2;
								map[i][j] = 0;
								ok = false;
							}
						}
					}
					if (ok) break;
				}
			}
			else if (dir[k] == 1) {
				while (1) {
					bool ok = true;
					for (int j = n - 2; j >= 0; j--) {
						for (int i = 0; i < n; i++) {
							//그래도 i,j로
							//-----------------------
							if (map[i][j] == 0) continue;
							if (map[i][j + 1] == 0) {
								map[i][j + 1] = map[i][j];
								map[i][j] = 0;
								ok = false;
							}
							else if (ch[i][j + 1] == 0 && map[i][j] == map[i][j + 1] && ch[i][j] == 0) {
								ch[i][j + 1] = 1;
								map[i][j + 1] *= 2;
								map[i][j] = 0;
								ok = false;
							}
						}
						//-----------------------
					}
					if (ok) break;
				}

			}
			else if (dir[k] == 2) {
				while (1) {
					bool ok = true;
					for (int i = n - 2; i >= 0; i--) {
						for (int j = 0; j < n; j++) {
							//-----------------------
							if (map[i][j] == 0) continue;
							if (map[i + 1][j] == 0) {
								map[i + 1][j] = map[i][j];
								map[i][j] = 0;
								ok = false;
							}
							else if (ch[i + 1][j] == 0 && map[i + 1][j] == map[i][j] && ch[i][j] == 0) {
								ch[i + 1][j] = 1;
								map[i + 1][j] *= 2;
								map[i][j] = 0;
								ok = false;
							}
							//-----------------------
						}
					}
					if (ok) break;
				}
			}
			else {
				while (1) {
					bool ok = true;
					for (int j = 1; j < n; j++) {
						for (int i = 0; i < n; i++) {
							//-----------------------
							if (map[i][j] == 0) continue;
							if (map[i][j - 1] == 0) {
								map[i][j - 1] = map[i][j];
								map[i][j] = 0;
								ok = false;
							}
							else if (ch[i][j - 1] == 0 && map[i][j - 1] == map[i][j] && ch[i][j] == 0) {
								ch[i][j - 1] = 1;
								map[i][j - 1] *= 2;
								map[i][j] = 0;
								ok = false;
							}

							//-----------------------
						}
					}
					if (ok) break;
				}
			}
		}

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (t==-1||t < map[i][j]) t = map[i][j];
			}
		}
		return;
	}
	else {
		for (int i = 0; i < 4; i++) {
			dir[L] = i;
			DFS(L + 1,map);
			dir[L] = 0;
		}
	}
}

int main() {
	//freopen("in1.txt", "rt", stdin);
	cin >> n;
	vector<vector<int>> map(n, vector<int>(n));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> map[i][j];
		}
	}
	DFS(0, map);
	cout << t << '\n';
	return 0;
}
profile
긍정코딩세상

0개의 댓글

관련 채용 정보