[C++] 프로그래머스 [PCCP 기출문제] 2번 / 석유 시추

2024. 2. 16. 19:13·Algorithm/Programmers

 

#include <string>
#include <vector>
#include <set>
#include <math.h>

using namespace std;

bool visited[500][500];

void dfs(int x, int y, int &count, set<int>& targetNum, const vector<vector<int>>& land)
{
    if (visited[x][y])
    {
        return;
    }

    visited[x][y] = true;
    count++;
    targetNum.insert(y);

    if (x + 1 < land.size())
    {
        if (land[x + 1][y] == 1)
        {
            dfs(x + 1, y, count, targetNum, land);
        }
    }
    if (y + 1 < land[x].size())
    {
        if (land[x][y + 1] == 1)
        {
            dfs(x, y + 1, count, targetNum, land);
        }
    }
    if (x - 1 >= 0)
    {
        if (land[x - 1][y] == 1)
        {
            dfs(x - 1, y, count, targetNum, land);
        }
    }
    if (y - 1 >= 0)
    {
        if (land[x][y - 1] == 1)
        {
            dfs(x, y - 1, count, targetNum, land);
        }
    }
    
}

int solution(vector<vector<int>> land)
{
    int answer = 0;
    vector<int> maxNum(land[0].size(), 0);

    for (int i = 0; i < land.size(); i++)
    {
        for (int j = 0; j < land[i].size(); j++)
        {
            if (land[i][j] == 1)
            {
                set<int> targetNum;
                int count = 0;

                dfs(i, j, count, targetNum, land);

                for (auto num : targetNum)
                {
                    maxNum[num] += count;
                }
            }
        }
    }

    for(int i = 0; i < maxNum.size(); i++)
    {
        answer = max(maxNum[i], answer);
    }

    return answer;
}

 

풀이

1. dfs로 석유 매립 지역과 석유량을 찾는다.

2. 매립 지역에 석유량을 추가해준다.

3. 최대로 가져올 수 있는 곳을 가져온다.

 

느낀점

레벨2 문제인데 한시간걸렸다. 코딩시간을 더 단축할 수 있도록 노력을 해야될 것 같다.

 

 

https://school.programmers.co.kr/learn/courses/30/lessons/250136

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

저작자표시 (새창열림)

'Algorithm > Programmers' 카테고리의 다른 글

[C++] 프로그래머스 야근 지수  (0) 2024.02.29
[C++] 프로그래머스 숫자의 표현  (0) 2024.02.16
[C++] 프로그래머스 올바른괄호  (1) 2024.02.15
[C++] 프로그래머스 최솟값 만들기  (1) 2024.02.15
[C++] 프로그래머스 JadenCase 문자열 만들기  (0) 2024.02.15
'Algorithm/Programmers' 카테고리의 다른 글
  • [C++] 프로그래머스 야근 지수
  • [C++] 프로그래머스 숫자의 표현
  • [C++] 프로그래머스 올바른괄호
  • [C++] 프로그래머스 최솟값 만들기
chanheess
chanheess
'왜' 그렇게 했는가?에 대한 생각으로 공부 및 작업의 저장관리
  • chanheess
    왜 그렇게 생각했는가?
    chanheess
  • 전체
    오늘
    어제
    • 분류 전체보기
      • Backend Programming
      • Game Programming
        • Unreal
        • DirectX
      • C++
        • Memo
        • Basic
        • Effective Modern
      • Java
      • Algorithm
        • Memo
        • Baekjoon
        • Programmers
        • HackerRank, LeetCode
      • Data Structure
      • Design Pattern
      • Etc
        • Memo
        • Daily Log
        • Book
  • 최근 글

  • 최근 댓글

  • 태그

    SpringSecurity
    프로그래머스
    spring
    dp
    위클리 챌린지
    백준
    알고리즘
    오블완
    dfs
    Java
    c++ 기초 플러스
    티스토리챌린지
    JWT
    JPA
  • hELLO· Designed By정상우.v4.10.0
chanheess
[C++] 프로그래머스 [PCCP 기출문제] 2번 / 석유 시추

개인정보

  • 티스토리 홈
  • 포럼
  • 로그인
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.