백준 2178 c++

2026. 1. 27. 07:58·C++ 백준

오랫만에 포트폴리오 ai 작업을 시작하기전 코테문제를 풀어보기로 했다

시작은 간단하게 BFS 문제의 교과서라고 할수있는 2178문제

 

난이도 실버1

 

가중치가 1로 동일하고 최단거리를 찾는 문제이다

https://www.acmicpc.net/problem/2178

 

입력 & 출력

 

#include "iostream"
#include "tuple"
#include "queue"

using namespace std;

//가중치가 동일한 최단거리 -> bfs
//입력 n x m, 1 or 0
//출력 최단거리

const int max_Num = 104;

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

int n, m;
int a[max_Num][max_Num];
int visited[max_Num][max_Num];
int currentY, currentX;
string s;

int main()
{
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		cin >> s;
		for (int j = 0; j < m; j++)
			a[i][j] = s[j] - '0';
	}
	queue<pair<int, int>> q;
	visited[0][0] = 1;
	q.push({ 0, 0 });
	while (q.size())
	{
		tie(currentY, currentX) = q.front(); q.pop();
		for (int i = 0; i < 4; i++)
		{
			int ny = currentY + dy[i];
			int nx = currentX + dx[i];
			if (ny < 0 || ny >= n || nx < 0 || nx >= m || a[ny][nx] == 0)
				continue;

			if (visited[ny][nx])
				continue;
			visited[ny][nx] = visited[currentY][currentX] + 1;
			q.push({ ny, nx });
		}
	}
	cout << visited[n - 1][m - 1];
	return 0;
}
저작자표시 비영리 변경금지 (새창열림)

'C++ 백준' 카테고리의 다른 글

백준 - 4375 1 c++ (모듈러연산 방식)  (0) 2025.12.05
백준 - 3986 좋은 단어 c++  (0) 2025.12.03
백준 - 1940 주몽 C++  (0) 2025.12.03
백준 (11655, 9996, 2559)  (0) 2025.11.16
'C++ 백준' 카테고리의 다른 글
  • 백준 - 4375 1 c++ (모듈러연산 방식)
  • 백준 - 3986 좋은 단어 c++
  • 백준 - 1940 주몽 C++
  • 백준 (11655, 9996, 2559)
lucodev
lucodev
언리얼 포폴개발 일기
  • lucodev
    루코 개발테이블
    lucodev
  • 전체
    오늘
    어제
    • 분류 전체보기 (236)
      • Unreal 프로젝트 다이어리 (132)
        • 첫번째 프로젝트 (73)
        • 두번째 프로젝트 (59)
      • Unreal 팁 (8)
      • Unreal 디버깅 (8)
      • C++ 프로그래머스 (52)
        • Stack,Queue (7)
        • Hash (4)
        • Heap (2)
        • Sort (5)
        • Exhaustive search (5)
        • Greedy (2)
        • BFS , DFS (7)
        • Graph (2)
        • Dynamic Programming (1)
        • C++ Math (2)
        • 기타 문제 (14)
      • C++ 백준 (5)
      • C++ 팁 (1)
      • 개인 코테 & 스타디 <비공개> (29)
        • 코드 개인보관함 (9)
        • 코딩테스트+@ (11)
        • 알고리즘 스타디 (6)
        • 알고리즘 스타디 과제 (3)
        • 비공개 (0)
  • 인기 글

  • 최근 글

  • 최근 댓글

  • 링크

  • 공지사항

  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 태그

    언리얼
    언리얼 ui
    언리얼 parkour
    unreal npc
    unreal inventory
    언리얼 비헤이비어트리
    unreal 상호작용
    언리얼 behavior tree
    unreal 인벤토리
    언리얼 behaviortree
    언리얼 인벤토리
    unreal
    언리얼 시퀀스
    언리얼 인터렉션
    언리얼 세키로
    언리얼 상호작용
    언리얼 파쿠르
    unreal 세키로
    언리얼 컷씬
    unreal 파쿠르
  • hELLO· Designed By정상우.v4.10.3
lucodev
백준 2178 c++
상단으로

티스토리툴바