#include <string>
#include <vector>
#include <queue>
#include <iostream>

using namespace std;

vector<int> solution(vector<int> prices) {
    int n = prices.size();
    vector<int> answer(n, 0);
    
    // index, price, second
    queue<pair<pair<int, int>, int>> q;
    
    q.push({{0, prices[0]}, 0});
    for (int i = 1; i < n; i++)
    {
        int q_size = q.size();
        while (q_size > 0)
        {
            auto [index_price, second] = q.front();
            q.pop();
            
            int index = index_price.first;
            int price = index_price.second;
            second++;
            if (price <= prices[i])
            {
                q.push({{index, price}, second});
            }
            else
            {
                answer[index] = second;
            }
            q_size--;
        }
        
        q.push({{i, prices[i]}, 0});
    }
    
    while (!q.empty())
    {
        auto [index_price, second] = q.front();
        q.pop();
        
        int index = index_price.first;
        answer[index] = second;
    }
    
    return answer;
}
#include <vector>
#include <queue>
#include <iostream>

using namespace std;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int visited[100][100];

int solution(vector<vector<int> > maps)
{
    int answer = 100 * 100 + 1;
    int n = maps.size(), m = maps[0].size();
    
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
            visited[i][j] = 0;
    }
    
    // pos, depth
    queue<pair<pair<int, int>, int>> q;
    q.push({{n - 1, m - 1}, 1});
    
    while(!q.empty())
    {
        auto [pos, depth] = q.front();
        q.pop();
        
        int h = pos.first, w = pos.second;
        if (h == 0 && w == 0)
        {
            answer = min(answer, depth);
            continue;
        }
        
        if (depth >= answer)
            continue;
        
        if (visited[h][w] == 1)
            continue;
        visited[h][w] = 1;
        for (int i = 0; i < 4; i++)
        {
            int nextY = h + dy[i];
            int nextX = w + dx[i];
            
            if (nextY < 0 || nextY > n - 1 || nextX < 0 || nextX > m - 1 || maps[nextY][nextX] == 0 || visited[nextY][nextX] == 1)
                continue;
            
            q.push({{nextY, nextX}, depth + 1});
        }
    }
    
    return answer == 10001 ? -1 : answer;
}

'알고리즘' 카테고리의 다른 글

프로그래머스 - 네트워크 (c++)  (0) 2025.02.23
프로그래머스 - 주식 가격 (c++)  (0) 2025.02.23
Lv. 3 나무 섭지 - C++  (0) 2024.12.10
Lv. 3 징검다리 - C++  (0) 2024.12.10
Lv. 3 함께하는 효도 - C++  (0) 2024.12.10

1. 코드

#include <iostream>
#include "windows.h"

using namespace std;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
{

    AllocConsole(); // 새로운 콘솔 창 생성
    FILE* pStreamOut = nullptr;
    freopen_s(&pStreamOut, "CONOUT$", "w", stdout);

    cout << "HELLO" << endl;

    Sleep(1000);

    cout << "Bye" << endl;

    Sleep(1000);

    fclose(pStreamOut);
    FreeConsole();
}

 

AllocConsole() 함수로 새로운 콘솔 창 생성

이후 freopen_s으로 표준 출력을 새로 생긴 pStreamOut이라는 콘솔 스트림으로 리디렉션 해줘야함

2. freopen_s 함수 시그니처

errno_t freopen_s(
    FILE** stream,       // 리디렉션된 스트림의 포인터를 저장할 변수
    const char* filename, // 새로운 출력 대상 (파일 경로 또는 특수 장치 이름)
    const char* mode,     // 파일 열기 모드 ("r", "w", "a" 등)
    FILE* old_stream      // 기존 표준 스트림(stdout, stdin, stderr 등)
);

 

old_stream의 stdout으로 리디렉션될 출력은 std::cout 및 printf 호출에 적용된다.

스트림을 닫기전까지는 호출한 상태 그대로 적용된다는 것을 명심.

+ Recent posts