본문 바로가기

CodingTest Exam

(110)
89. 토마토(BFS 활용) (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★★★★ #include #include using namespace std; int a[1000][1000]; int dis[1000][1000]; struct Tom { int x; int y; Tom(int x, int y) { this -> x = x; this -> y = y; } }; int main() { int m, n, res = 0; bool flag = true; int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, -1, 0, 1}; scanf("%d %d", &m, &n); // m과 n을 읽는 순서를 조심하자!!! queue Q; for(int i = 1; i
87. 섬나라 아일랜드(BFS 활용) (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★★☆☆ #include #include using namespace std; int n, cnt = 0; struct Pos { int x; int y; Pos(int x, int y) { this -> x = x; this -> y = y; } }; int main() { scanf("%d", &n); int a[n+2][n+2]; // 외곽 라인은 전부 0으로 만들기 위해 int dx[8] = {0, 1, 1, 1, 0 ,-1 , -1, -1}; // x좌표의 상하좌우, 대각선 int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1}; // y좌표의 상하좌우, 대각선 for(int i = 1; i
86. 피자 배달 거리(삼성 SW역량평가 기출문제 : DFS 활용) (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★★★★ #include #include #include using namespace std; int n, m, res = 2147000000, sum = 0; int a[50][50]; int ch[50]; vector house; vector pizza; void DFS(int s, int L) { if(L == m) // 피자집 m개를 뽑았으면 { sum = 0; for(int i = 0; i < house.size(); i++) { int x1 = house[i].first; // 피자집과의 거리를 비교할 집의 x좌표 int y1 = house[i].second; // 피자집과의 거리를 비교할 집의 y좌표 int dis = 2147000000; // 비교할 거리 for(int j = 0; j < m; j+..
85. 수식만들기(삼성 SW역량평가 기출문제 : DFS 활용) (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★☆☆☆ #include int n, mxAns = 0, mnAns = 2147000000; int N[10]; int C[4]; void DFS(int x, int y) { if(x == n) { if(mxAns y) mnAns = y; } else { for(int i = 0; i < 4; i++) { if(C[i] != 0) // 해당 수식의 개수가 남아있다면 { C[i]--; // 쓴다 if(i == 0) DFS(x+1, y+N[x+1]); else if(i == 1) DFS(x+1, y-N[x+1]); else if(i == 2) DFS(x+1, y*N[x+1]); else DFS(x+1, y/N[x+1]); C[i]++; // 쓰고나면 다시 복구 } } ..
84. 휴가(삼성 SW역량평가 기출문제 : DFS 활용) (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★★★★ #include #include using namespace std; int n, t, p, money = 0; struct COS { int day; int cost; COS(int x, int y) { this -> day = x; this -> cost = y; } }; vector v; void DFS(int x, int y) { if(x == n+1) // n+1부터 휴가이므로 { if(y > money) money = y; } else { COS temp = v[x]; // 해당 일차의 드는 시간과 받는 돈 if(x + temp.day
83. 복면산 SEND+MORE=MONEY (MS 인터뷰) (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★★★☆ #include int a[8]; int ch[10]; int Send() { return a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3]; } int More() { return a[4] * 1000 + a[5] * 100 + a[6] * 10 + a[1]; } int Money() { return a[4] * 10000 + a[5] * 1000 + a[2] * 100 + a[1] * 10 + a[7]; } void DFS(int x) { if(x == 8) { if(Send() + More() == Money()) { if(a[0] == 0 || a[4] == 0) return; printf(" %d %d %d %d\n", a[0], a[1], a[2], a[3]); p..
82. 순열구하기(DFS : Depth First Search) (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★★★☆ #include int n, r, cnt = 0; int a[17]; int ch[17]; int res[17]; using namespace std; void DFS(int x) { if(x == r) { for(int i = 0; i < r; i++) printf("%d ", res[i]); printf("\n"); cnt++; } else { for(int i = 1; i
80. 다익스트라 알고리즘 (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★★★☆ #include #include #include using namespace std; struct Edge { int node; int cost; Edge(int x, int y) { this -> node = x; this -> cost = y; } bool operator cost > a.cost; } }; int main() { int n, m, a, b, c; scanf("%d %d", &n ,&m); vector dis(n+1, 2147000000); // 각 노드까지의 거리를 무한대로 설정 vector v[n+1]; priority_queue pQ; for(int i = 1; i nextCost) { dis[nextNode] = nextCost; pQ.push(Edge(nextNode, nex..