일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- NEXT
- 분할 정복
- DP
- pytorch
- 너비 우선 탐색
- 2023
- dropout
- 우선 순위 큐
- 다익스트라
- 이분 탐색
- dfs
- 자바스크립트
- 회고록
- lazy propagation
- object detection
- 조합론
- 가끔은 말로
- Overfitting
- 플로이드 와샬
- 세그먼트 트리
- 미래는_현재와_과거로
- tensorflow
- 크루스칼
- BFS
- 백트래킹
- 가끔은_말로
- back propagation
- c++
- 알고리즘
- 문자열
Archives
- Today
- Total
Doby's Lab
백준 1331번: 나이트 투어 (C++) 본문
https://www.acmicpc.net/problem/1331
1331번: 나이트 투어
나이트 투어는 체스판에서 나이트가 모든 칸을 정확히 한 번씩 방문하며, 마지막으로 방문하는 칸에서 시작점으로 돌아올 수 있는 경로이다. 다음 그림은 나이트 투어의 한 예이다. 영식이는 6×
www.acmicpc.net
Level: Silver V
Solved By: Implementation
3가지 조건을 따져주어야 합니다.
- 나이트로 이동 가능한가?
- 방문했던 곳을 다시 방문하지 않는가?
- 마지막 나이트가 처음 나이트로 갈 수 있는가?
섣불리 코드를 썼다가 틀릴 수도 있는 문제입니다.
#include <iostream>
#include <vector>
#define pii pair<int, int>
using namespace std;
vector<pii> chess;
struct Direction{
int y, x;
};
Direction dir[8] = {{-2, -1}, {-2, 1}, {-1, -2}, {1, -2},
{2, -1}, {2, 1}, {1, 2}, {-1, 2}};
bool visited[7][7];
bool solve(){
visited[chess[0].first][chess[0].second] = true;
for(int i = 1; i < chess.size(); i++){
int ny = chess[i - 1].first;
int nx = chess[i - 1].second;
int dy = chess[i].first;
int dx = chess[i].second;
// visited Check
if(visited[dy][dx]) return false;
visited[dy][dx] = true;
bool flag = false;
for(int j = 0; j < 8; j++){
// can move
if(ny + dir[j].y == dy && nx + dir[j].x == dx){
flag = true;
break;
}
}
if(!flag){
return false;
}
}
for(int i = 0; i < 8; i++){ // Last to First
if(chess[35].first + dir[i].y == chess[0].first
&& chess[35].second + dir[i].x == chess[0].second){
return true;
}
}
return false;
}
int main(){
for(int i = 0; i < 36; i++){
string v; cin >> v;
int ny = 7 - (v[1] - '0');
int nx = (v[0] - 'A') + 1;
chess.push_back({ny, nx});
}
if(solve()) cout << "Valid";
else cout << "Invalid";
return 0;
}
728x90
'PS > BOJ' 카테고리의 다른 글
백준 1769번: 3의 배수 (C++) (0) | 2023.01.08 |
---|---|
백준 1448번: 삼각형 만들기 (C++) (0) | 2022.12.17 |
백준 10266번: 시계 사진들 (C++) (0) | 2022.12.03 |
백준 3295번: 단방향 링크 네트워크 (C++) (2) | 2022.12.03 |
백준 1671번: 상어의 저녁식사 (C++) (2) | 2022.12.03 |