[C++] 프로그래머스 거리두기 확인하기
·
Algorithm/Programmers
#include #include using namespace std; bool SearchManhattanRange(int aX,int aY, int bX, int bY, const vector& place) { int manhattanRange = abs(aX - bX) + abs(aY - bY); if (manhattanRange > 2) { return false; } int minX = min(aX, bX); int minY = min(aY, bY); if (aY == bY) { if(place[aY][minX + 1] == 'X') { return false; } } if (aX == bX) { if (place[minY + 1][aX] == 'X') { return false; } } if (..
[C++] 프로그래머스 2016년
·
Algorithm/Programmers
#include #include using namespace std; string solution(int a, int b) { string answer = ""; int month[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 }; string week[7] = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" }; for(int i = 0; i < a - 1; i++) { b += month[i]; } answer = week[(b + 4) % 7]; return answer; } 풀이 1. 12월까지의 날짜와 각 요일을 저장한다. 요일은 일요일부터 저장한다. 7로 나눠질때 0으로 떨어지는 것을 이용. 2. 배열이 0부터 계산하여 ..
[C++] 프로그래머스 정수삼각형
·
Algorithm/Programmers
#include #include using namespace std; int solution(vector triangle) { int answer = 0; for (int i = 1; i < triangle.size(); i++) { int endNum = triangle[i].size() - 1; triangle[i][0] += triangle[i - 1][0]; triangle[i][endNum] += triangle[i - 1][endNum - 1]; answer = max(triangle[i][0], answer); answer = max(triangle[i][endNum], answer); for (int j = 1; j < endNum; j++) { int maxNum = max(triangl..
[C++] 프로그래머스 게임 맵 최단거리
·
Algorithm/Programmers
#include #include using namespace std; int solution(vector maps) { int answer = 0; int n = maps.size(); int m = maps[0].size(); bool visited[100][100] = { false }; vector direction = { {1, 0}, {0, 1}, {-1, 0}, {0, -1} }; queue node; node.push({ 0, 0 }); visited[0][0] = true; while (!node.empty()) { int value = maps[node.front().first][node.front().second]; for (int i = 0; i < direction.size(); i..
[C++] 프로그래머스 행렬 테두리 회전하기
·
Algorithm/Programmers
#include #include using namespace std; vector answer; int maxNum; void turn_border(const vector& queries, vector &table); vector solution(int rows, int columns, vector queries) { vector table; //[y][x] maxNum = rows * columns + 1; //make_table for (int y = 0; y < rows; y++) { vector columns_table_temp; for (int x = 0; x < columns; x++) columns_table_temp.push_back(y * columns + x + 1); table.pus..
[C++] 프로그래머스 N-Queen 재귀
·
Algorithm/Programmers
#include #include using namespace std; int answer = 0; void BackTracking(int y, int n, vector visited); int solution(int n) { vector visited(3); //0 x, 1 left, 2 right BackTracking(0, n, visited); return answer; } void BackTracking(int y, int n, vector visited) { if (y == n) { answer++; return; } for (int x = 0; x < n; x++) { bool next = false; //x와 대각선좌표에 해당되는지 판단 for (int j = 0; j < 3; j++) { ..