| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 |
Tags
- AWS
- wss 연결 실패
- spring websocket nginx 설정
- session
- 백준 10815 # 백준 Java
- 개발 프로젝트
- 소셜 로그인
- presigned url
- 예외 처리
- springboot
- jwt
- 자바 orm
- 구글 로그인
- validation
- ec2 nginx websocket reverse proxy
- 도메인 주도 개발
- 패러다임 불일치
- 스프링부트
- 관점 지향 프로그래밍
- logout
- CustomException
- fastapi
- oauth2.0
- OpenAI API
- GoormIDE
- @Valid
- 이미지 업로드
- S3
- Flask
- spring boot
Archives
- Today
- Total
개발세발은 안되요
[C++] 백준 7562 : 나이트 본문
문제
https://www.acmicpc.net/problem/7562
풀이
시작점에서 목표 지점까지 이동할 수 있는 최소 경로를 구하는 문제이므로, BFS로 풀 수 있었다.
코드
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct State {
int y, x, n;
};
int t,l, s_x, s_y, e_x, e_y ;
int visited[301][301];
vector <int> ans;
// 나이트 이동 가능 방법
int dx[8] = {-1, 1, 2, 2, -1, 1, -2, -2 };
int dy[8] = {-2,-2, -1, 1, 2 ,2, -1, 1 };
int main() {
cin >> t;
for(int i=0; i<t; i++){
cin >> l >> s_y >> s_x >> e_y >> e_x;
// 시작점을 큐에 넣고 방문 표시
queue<State> q;
q.push({s_y, s_x, 0});
visited[s_y][s_x] = 1;
// BFS
while(!q.empty()){
State cur = q.front();
q.pop();
// 현재 위치가 목표 위치인 경우,
// 도달한 경우
if(cur.y == e_y && cur.x == e_x){
ans.push_back(cur.n);
break;
}
// 나이트 이동
for(int i=0; i<8; i++){
int ny = cur.y + dy[i];
int nx = cur.x + dx[i];
// 유효하지 않은 위치(배열 범위를 벗어남)인 경우
if(ny <0 || ny>=l || nx<0 || nx>=l) continue;
// 이미 방문한 곳인 경우
if(visited[ny][nx] == 1) continue;
// 유효한 다음 위치를 큐에 넣고, 방문 표시
q.push({ny, nx, cur.n+1});
visited[ny][nx] = 1;
}
}
// 방문 배열 초기화
for(int i=0; i<l; i++){
for(int j=0; j<l; j++){
visited[i][j] = 0;
}
}
}
for(int i=0; i<ans.size(); i++){
cout << ans[i] << "\n";
}
return 0;
}'알고리즘 > 백준' 카테고리의 다른 글
| [C++] 백준 1806 : 부분합 (3) | 2025.08.15 |
|---|---|
| [C++] 백준 2230 : 수 고르기 (1) | 2025.08.15 |
| [C++] 백준 2294 : 동전 2 (2) | 2025.08.10 |
| [C++] 백준 21317 : 징검다리 건너기 (3) | 2025.08.05 |
| [C++] 백준 1041 : 주사위 (0) | 2025.05.21 |