BOJ::7562 나이트의 이동
https://www.acmicpc.net/problem/7562
bfs를 통하여 나이트가 이동할 수 있는 지점들에 대해 탐색하며 카운팅 해주면 된다.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #include <stdio.h> #include <queue> using namespace std; int t,n,sx,sy,ex,ey,ax,ay,x,y; int dx[] = { -2,-2,-1,-1,1,1,2,2 }; int dy[] = { 1,-1,2,-2,2,-2,1,-1 }; int d[300][300]; bool check[300][300]; struct Node { int x; int y; Node(int _x, int _y) { x = _x; y = _y; } }; void bfs(int a, int b) { check[a][b] = true; queue<Node> q; q.push(Node(a, b)); while (!q.empty()){ x = q.front().x; y = q.front().y; q.pop(); for (int i = 0; i < 8; i++) { ax = x + dx[i]; ay = y + dy[i]; if (ax >= 0 && ay >= 0 && ax < n&&ay < n) { if (!check[ax][ay]) { check[ax][ay] = true; d[ax][ay] = d[x][y] + 1; q.push(Node(ax, ay)); } } } } } void init() { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { check[i][j] = false; d[i][j] = 0; } } } int main() { scanf("%d", &t); for (int tc = 0; tc < t; tc++) { scanf("%d", &n); scanf("%d %d", &sx, &sy); scanf("%d %d", &ex, &ey); init(); bfs(sx, sy); printf("%d\n", d[ex][ey]); } } | cs |
'BOJ::문제풀이' 카테고리의 다른 글
9465 스티커 (0) | 2018.01.06 |
---|---|
7576 토마토 (0) | 2018.01.06 |
6603 로또 (0) | 2018.01.06 |
6588 골드바흐의 추측 (0) | 2018.01.06 |
5014 스타트링크 (0) | 2018.01.06 |