Skip to content

Conversation

@hadongun
Copy link
Collaborator

@hadongun hadongun commented Jul 8, 2025

πŸ”— 문제 링크

ν—Œλ‚΄κΈ°λŠ” μΉœκ΅¬κ°€ ν•„μš”ν•΄

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

30min

✨ μˆ˜λ„ μ½”λ“œ

μ•ˆλ…•ν•˜μ„Έμš”. λ”μš΄ μ—¬λ¦„μž…λ‹ˆλ‹€.
image

이번 λ¬Έμ œλŠ” 'μ•„ λ§žλ‹€ μš°μ‚°!!!' κ³Ό λΉ„μŠ·ν•œ λ¬Έμ œμž…λ‹ˆλ‹΅

λ°©λ¬Έν•  수 μžˆλŠ” λͺ¨λ“  μ‚¬λžŒμ„ μ°Ύμ•„ 제 친ꡬ둜 λ§Œλ“€μ–΄λ²„λ¦¬κ² μŠ΅λ‹ˆλ‹€.

μ‹œμž‘ 지점 μ°ΎκΈ°

μš°μ„  μ‹œμž‘ 지점을 μ°ΎκΈ° μœ„ν•΄μ„œ selflocation ν•¨μˆ˜λ₯Ό μ œμ‘°ν–ˆμŠ΅λ‹ˆλ‹€.
λͺ¨λ“  뢀뢄을 λŒλ©΄μ„œ μ‹œμž‘ 지점 μœ„μΉ˜μΈ 'I' λ₯Ό μ°Ύκ³  κ·Έ μ’Œν‘œ 값을 νŠœν”Œμ˜ ν˜•νƒœλ‘œ λ°˜ν™˜ν•˜μ˜€μˆ¨λ‹Ή

친ꡬ λ§Œλ“€κΈ°

친ꡬλ₯Ό λ§Œλ“€μ–΄μ•Όκ² μ£ !

  1. μ €λ²ˆμ— λ°°μ› λ˜ directions λ³€μˆ˜λ₯Ό 톡해 μƒν•˜μ’Œμš°λ₯Ό 탐색해 μ£Όκ² μŠ΅λ‹ˆλ‹€.
  2. 리슀트의 λ²”μœ„λ₯Ό λ²—μ–΄λ‚¬λŠ”μ§€ 확인 ν›„ 벽을 λ§Œλ‚¬λŠ”μ§€, λ°©λ¬Έν•˜μ˜€λŠ”μ§€ ν™•μΈν•©λ‹ˆλ‹€.
  3. λ°©λ¬Έν•˜μ§€ μ•Šκ³  벽이 μ•„λ‹ˆλΌλ©΄ -> κ·Έ μ’Œν‘œκ°’μ„ 큐에 λ„£κ³  λ°©λ¬Έμ²˜λ¦¬ν•©λ‹ˆλ‹€.
  4. λ§Œμ•½ κ·Έ λ°©λ¬Έν•œ 곳에 μΉœκ΅¬κ°€ μžˆλ‹€λ©΄('P') -> 제 μΉœκ΅¬κ°€ λ˜μ–΄ result += 1을 ν•΄μ€λ‹ˆλ‹€.
μˆ˜λ„μ½”λ“œ

ν•¨μˆ˜ selflocation(campus, n, m):
    κ²°κ³Ό μ’Œν‘œ resultλ₯Ό (0, 0)으둜 μ΄ˆκΈ°ν™”
    캠퍼슀λ₯Ό μˆœνšŒν•˜λ©°
        λ§Œμ•½ campus[i][j]κ°€ 'I'이면
            result = (i, j)
    return result

ν•¨μˆ˜ exploration(campus, n, m, s):
    visited 2차원 배열을 λͺ¨λ‘ False둜 μ΄ˆκΈ°ν™”
    이동 λ°©ν–₯ directions = [(1,0), (-1,0), (0,1), (0,-1)]
    큐 q에 μ‹œμž‘μ  sλ₯Ό μ‚½μž…
    친ꡬ 수 result = 0

    while 큐가 λΉ„μ–΄μžˆμ§€ μ•ŠμœΌλ©΄:
        ν˜„μž¬ μœ„μΉ˜ (y, x) = qμ—μ„œ pop
        visited[y][x] = True

        λ„€ λ°©ν–₯에 λŒ€ν•΄ 반볡:
            (nx, ny) = (x + dx, y + dy)
            
            λ§Œμ•½ (nx, ny)κ°€ λ²”μœ„ 내이고, 
               ν•΄λ‹Ή 칸이 'X'κ°€ μ•„λ‹ˆκ³ , 
               아직 λ°©λ¬Έν•˜μ§€ μ•Šμ•˜λ‹€λ©΄:
               
                visited[ny][nx] = True
                큐에 (ny, nx) μ‚½μž…

                λ§Œμ•½ κ·Έ 칸이 'P'라면:
                    친ꡬ 수 result += 1

    return result


메인:
    n, m μž…λ ₯ λ°›μŒ
    캠퍼슀 λ§΅ μž…λ ₯ λ°›μŒ
    μ‹œμž‘ μœ„μΉ˜ μ°ΎκΈ° β†’ start = selflocation()
    친ꡬ 수 friend = exploration()

    λ§Œμ•½ friend == 0이면:
        좜λ ₯ 'TT'
    μ•„λ‹ˆλ©΄:
        좜λ ₯ friend

μ˜€λŠ˜λ„ λ„μ—°μ΄μ˜ 친ꡬλ₯Ό λ§Œλ“€μ–΄μ£Όμ—ˆμŠ΅λ‹ˆλ‹€. κ°μ‚¬ν•©λ‹ˆλ‹€
μ˜€λŠ˜λ„ 내일도 νž˜λ‚΄μ‹œκΈΈ λ°”λΌκ² μŠ΅λ‹ˆλ‹€.

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

Copy link
Contributor

@dohyeondol1 dohyeondol1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ „ν˜•μ μΈ κ·Έλž˜ν”„ 탐색 λ¬Έμ œλ„€μš”!
저도 λ™μœ€λ‹˜μ²˜λŸΌ bfsλ₯Ό μ‚¬μš©ν•˜μ—¬ ν’€μ—ˆμŠ΅λ‹ˆλ‹€.

C++
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int N, M;
vector<string> campus;
bool visited[601][601];

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

int bfs(int startX, int startY) {
    queue<pair<int, int>> q;
    q.push({startX, startY});
    visited[startX][startY] = true;

    int count = 0;
    while(!q.empty()) {
        auto [x, y] = q.front();
        q.pop();

        if(campus[x][y] == 'P') count++;

        for(int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];

            if(nx < 0 || ny < 0 || nx >= N || ny >= M) continue;
            if(visited[nx][ny] || campus[nx][ny] == 'X') continue;

            visited[nx][ny] = true;
            q.push({nx, ny});
        }
    }

    return count;
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    
    cin >> N >> M;
    campus.resize(N);

    int startX, startY;
    for(int i = 0; i < N; i++) {
        cin >> campus[i];
        for(int j = 0; j < M; j++) {
            if(campus[i][j] == 'I') {
                startX = i;
                startY = j;
            }
        }
    }

    int result = bfs(startX, startY);
    if(result == 0) cout << "TT" << '\n';
    else cout << result << '\n';

    return 0;
}

ν•¨μˆ˜λͺ…을 지을 λ•Œ, 개개인의 μŠ€νƒ€μΌμ— 따라 λ‹€λ₯΄μ§€λ§Œ 각 단어 λ‹¨μœ„λ‘œ λŒ€μ†Œλ¬Έμžλ‚˜ 띄어쓰기, 언더바 등을 μ‚¬μš©ν•΄ ꡬ뢄을 μ§€μ–΄μ£Όλ©΄ 가독성을 κ°œμ„ ν•˜λŠ”λ°μ— μ•½κ°„μ˜ 도움을 μ€λ‹ˆλ©.
넀이밍 μ»¨λ²€μ…˜μ΄λΌκ³ , μ—¬λŸ¬ μ’…λ₯˜κ°€ μžˆμœΌλ‹ˆ λ§žλŠ” μŠ€νƒ€μΌμ„ μ‚¬μš©ν•΄λ³΄λŠ”κ²ƒλ„ μΆ”μ²œλ“œλ¦½λ‹ˆλ‹€.

μ„œμ‹ 이름(λ“€)
twowords ν”Œλž«μΌ€μ΄μŠ€
TWOWORDS λŒ€λ¬Έμž, μ†Œλ¬Έμž
twoWords camelCase, dromedaryCase
TwoWords PascalCase, UpperCamelCase
two_words λ±€ μΌ€μ΄μŠ€, λ‹¬νŒ½μ΄ μΌ€μ΄μŠ€, 움푹 λ“€μ–΄κ°„ κ³³ μΌ€μ΄μŠ€
TWO_WORDS ALL_CAPS, SCREAMING_SNAKE_CASE, MACRO_CASE, CONSTANT_CASE
two_Words 낙타_λ±€_μΌ€μ΄μŠ€
Two_Words Pascal_Snake_Case, Title_Case
two-words μΌ€λ°₯ μΌ€μ΄μŠ€, λŒ€μ‹œ μΌ€μ΄μŠ€, λ¦¬μŠ€ν”„ μΌ€μ΄μŠ€, 슀파인 μΌ€μ΄μŠ€
TWO-WORDS κΈ°μ°¨ μΌ€μ΄μŠ€, μ½”λ³Ό μΌ€μ΄μŠ€, λΉ„λͺ… μΌ€λ°₯ μΌ€μ΄μŠ€
Two-Words Train-Case, HTTP-Header-Case

무엇보닀 μžκΈ°κ°€ κ°€μž₯ 잘 μ•Œμ•„λ³Ό 수 있으면 λ©λ‹ˆλ‹€!

@froglike6
Copy link
Collaborator

저도 κ°„λ‹¨νžˆ BFSλ₯Ό μ΄μš©ν•΄ 이 문제λ₯Ό ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€ γ…Žγ…Ž

import sys
from collections import deque
input = sys.stdin.readline

n, m = map(int, input().split())
campus = [list(input().rstrip()) for _ in range(n)]

start = None
for i in range(n):
    for j in range(m):
        if campus[i][j] == "I":
            start = (i, j)
            break
    if start is not None:
        break

visited = [[False]*m for _ in range(n)]
q = deque([start])
visited[start[0]][start[1]] = True
meet_count = 0

dr = [-1, 1, 0, 0]
dc = [0, 0, -1, 1]

while q:
    r, c = q.popleft()
    for d in range(4):
        nr, nc = r + dr[d], c + dc[d]
        if 0 <= nr < n and 0 <= nc < m:
            if not visited[nr][nc] and campus[nr][nc] != 'X':
                visited[nr][nc] = True
                if campus[nr][nc] == 'P':
                    meet_count += 1
                q.append((nr, nc))
if meet_count:
    print(meet_count)
else:
    print("TT")

@Fnhid
Copy link
Collaborator

Fnhid commented Jul 11, 2025

저도 λ¬΄λ‚œν•˜κ²Œ bfs둜 ν’€μ—ˆμŠ΅λ‹ˆλ‹€!

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

#define X first
#define Y second

using namespace std;

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

void bfs(vector<string> &cam, pair<int, int> iPos,int n, int m)
{

    vector<vector<int> > board(n, vector<int>(m, 0));
    queue<pair<int, int> > q;

    pair<int, int> cur;
    int nx, ny, cnt=0;
    q.push(iPos);
    while (!q.empty())
    {
        cur = q.front();
        q.pop();
        for (int i = 0; i < 4; i++)
        {
            nx = cur.X + dx[i]; ny = cur.Y + dy[i];
            if (nx < 0 || nx >= n || ny < 0 || ny >= m ||
                board[nx][ny] == 1 || cam[nx][ny] == 'X')
                continue;
            if (cam[nx][ny] == 'P') cnt++;

            board[nx][ny] = 1;
            q.push(make_pair(nx, ny));
        }

    }

    if (cnt==0) cout << "TT";
    else cout << cnt;

}


int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    int n, m;
    cin >> n >> m;

    vector<string> cam(n, "");
    pair<int, int> iPos;
    for (int i = 0; i < n; i++)
    {
        cin >> cam[i];
        for (int j= 0; j < m; j++)
        {
            if (cam[i][j] == 'I')
            {
                iPos = make_pair(i, j);
            }
        }
    }

    bfs(cam, iPos,n, m);
}

@Fnhid Fnhid merged commit 2bc1a68 into AlgoLeadMe:main Jul 11, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants