분류 전체보기

    [백준 2455번][C++] 지능형 기차

    #include #include using namespace std; int main() { //각 역에서의 기차안 사람 수를 저장하는 배열 int people[4]{0,}; //내리고 / 타는 사람 int down, up; //처음역에서의 사람 계산 cin >> down >> up; people[0] = up; //나머지 역에서의 사람 수 계산 for (int i = 1;i > down >> up; people[i] = people[i - 1] - down + up; } //오름차순으로 정렬 sort(people, people + 4); //가장 큰 수 출력 cout

    [백준 16503번][C++] 괄호 없는 사칙연산

    cal 함수를 만들어 사용했습니다 #include using namespace std; //사칙연산 기호에 따라 연산값을 리턴하는 함수 int cal(int n, int m, char c) { if (c == '+') return n + m; else if (c == '-') return n - m; else if(c=='/') return n / m; else return n * m; } int main() { //계산정보 입력받기 int n1, n2, n3; char c1, c2; cin >> n1 >> c1 >> n2 >> c2 >> n3; int result1, result2; //계산순서에 따라 결과값 저장하기 result1 = cal(cal(n1, n2, c1), n3, c2); result2..

    [백준 10026번][C++] 적록색약

    적록색약의 구역을 구할 때는 R이나 G 한 쪽을 정해 바꿔버리고 다시 탐색했습니다 #include #include #include #include using namespace std; //좌표 클래스 class Point { public: int x; int y; Point(int x, int y) { this->x = x; this->y = y; } ~Point(){} }; //monitor에 저장될 Data 클래스 class Data { public: char color; int isVisited = 0; Data(char color) { this->color = color; } ~Data(){} }; //크기 int n; //Data클래스 저장할 벡터 vector monitor(105, vector..

    [백준 1697번][C++] 숨바꼭질

    간단한 BFS문제 입니다 악간 DP문제풀이랑 섞어서 풀었는데 방문한 위치를 확인하는 인자를 넣어줘야 중복방문을 안하게 되는 점이 주요 포인트 였습니다 #include #include #include using namespace std; #define SIZE 100001; //위치 나타내는 point 벡터 vector point; int subin, dong; int BFS(int subin) { //bfs를 위한 큐 생성 queue bfs; //초기 subin값 큐에 삽입 bfs.push(subin); while (!bfs.empty()) { //subin 업데이트 subin = bfs.front(); point[subin].first = 1; bfs.pop(); //이동할 3가지 인덱스 저장 int ..

    [백준 15829번][C++] Hashing

    {모듈러 속성} (a + b) mod n = ((a mod n) + (b mod n)) mod n (a - b) mod n = ((a mod n) - (b mod n)) mod n (a * b) mod n = ((a mod n) * (b mod n)) mod n #include #include #include using namespace std; int main() { long long hashNum = 0; //알파벳 길이수 int len; cin >> len; //문장 받음 string str; cin >> str; //각 문자에 해당하는 수열 저장 vector nums; for (int i = 0;i < len; i++) { nums.push_back(str[i] - 'a' + 1); } long ..

    [백준 1012번][C++] 유기농 배추

    같은 실버등급에 있는 단지 수 찾기와 비슷한 문제라고 생각하여 똑같은 알고리즘으로 풀었습니다 #include #include #include using namespace std; //배추 심어진 좌표 저장할 큐 생성 queue isBaechu; //배추밭 vector 이중배열로 생성 vector field(52, vector(52, -2)); int moveX[4] = { -1,1,0,0 }; int moveY[4] = { 0,0,-1,1 }; void DeleteDFS(int x, int y) { //주변 정보 저장할 큐 생성 queue surround; //해당 좌표 0으로 변경 field[y][x] = 0; //주변정보 탐색하여 1이면 큐에 저장 for (int i = 0;i < 4;i++) { i..

    [백준 11726번][C++] 2×n 타일링

    간단하게 반복되는 규칙을 찾으면 되는 문제입니다 더보기 N의 방법의 수 = N-1의 방법의 수 + N-2의 방법의 수 #include using namespace std; int main() { int n; cin >> n; //n에 따른 방법의 수 저장할 배열 int way[1002]{0, }; //n이 1일 때는 1가지, 2일때는 2가지 방법 존재 way[1] = 1; way[2] = 2; //n까지 공식에 맞추어 배열 채워준다 for (int i = 3; i

    [백준 2667번][C++] 단지 번호 붙이기

    똑같은 좌표문제입니다 좌표문제를 많이 풀다보면 코드는 비슷하고 문제마다 약간 다른 아이디어를 첨부하는 거 같습니다 아이디어는 주석에 적어놓아 시간이 되면 따로 포스팅하겠습니다! #include #include #include #include #include using namespace std; //좌표를 저장하는 map 생성 vector map(27, vector(27, -2)); //단지내 첫 가구가 될 수 있는 좌표들의 모임인 one 생성 queue one; //각 단지의 가구 수 저장하는 danzi 생성 vector danzi; //상하좌우 체크를 위한 배열 int moveX[4] = { -1, 1, 0, 0 }; int moveY[4] = { 0, 0, 1, -1 }; //단지내 가구 수를 측정 ..

    [백준 1931번][C++] 회의실 배정

    그리디 알고리즘 이용 -> 끝나는 시간이 제일 작은 순으로 정렬하여 풀었습니다 #include #include #include #include using namespace std; //회의들을 순으로 저장 vector consel; //끝나는 시간이 빠른 순으로 정렬하되 같으면 먼저 시작하는 순으로 정렬 bool cmp(pair a, pair b) { if (a.second == b.second) return a.first > test; //회의 시간을 입력받아 저장 for (int i = 0; i < ..

    [백준 7565번][C++] 토마토

    기본적인 아이디어 토마토 판을 의미하는 2차원 벡터 생성후 숫자들 삽입 (이때 벡터에서의 잘못된 접근을 피하기 위해 첫 번째 줄인 인덱스 0을 비워둡니다) 삽입할 때 1의 숫자가 들어오는 죄표을 먼저 큐에 저장하고 첫 번째 단계라고 정의합니다 삽입이 끝나고 첫번째 단계의 큐 원소부터 주변의 0들의 값을 자신+1의 값(날짜를 의미)으로 업데이트해주며 큐에 저장해줍니다 첫번째 단계 큐의 원소들의 작업이 끝나면 두번째 단계 큐의 원소부터 다시 작업을 합니다 #include #include #include using namespace std; //주변 좌표를 탐색할 수 있도록 인덱스에 더할 값 배열 저장 int xPoint[4] = {-1, 0, 1, 0}; int yPoint[4] = {0, -1, 0, 1}..