안선생의 개발 블로그
C++ 7576 토마토 본문
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
64
65
|
#include <iostream>
#include <vector>
#include<queue>
#include <cstring>
#include<algorithm>
using namespace std;
int g[502][502] = {};
int chess[502][502] = {};
bool visit[502][502] = {};
int n, m,k;
int a, b, c, d;
// 오 아 왼 위
int dx[8] = { 2,1,2,1,-2,-1,-2,-1};
int dy[8] = { 1,2,-1,-2,1,2,-1,-2};
int r = 0;
void bfs(int x, int y)
{
queue < pair<int, int>> b;
visit[x][y] = 1;
b.push({ x, y });
while (!b.empty())
{
int x1 = b.front().first;
int y1 = b.front().second;
if (x1 == c && y1 == d)
{
cout << g[x1][y1] << "\n";
return;
}
b.pop();
for (int i = 0; i < 8; i++)
{
int x2 = x1 + dx[i];
int y2 = y1 + dy[i];
if (x2 < 0 || x2 >= k || y2 < 0 || y2 >= k)
continue;
if (!visit[x2][y2])
{
g[x2][y2] = g[x1][y1] + 1;
visit[x2][y2] = 1;
b.push({ x2,y2 });
}
}
}
}
int main()
{
int input; cin >> input;
while (input--)
{
cin >> k;
cin >> a >> b >> c >> d;
bfs(a, b);
memset(g, 0, sizeof(g));
memset(visit, 0, sizeof(visit));
}
return 0;
}
|
cs |
https://www.acmicpc.net/problem/7576
7576번: 토마토
첫 줄에는 상자의 크기를 나타내는 두 정수 M,N이 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M,N ≤ 1,000 이다. 둘째 줄부터는 하나의 상자에 저장된 토마토
www.acmicpc.net
'백준' 카테고리의 다른 글
C++ 3184 양 (1) | 2023.02.24 |
---|---|
C++ 1697 숨바꼭질 (0) | 2023.02.23 |
C++ 7652 나이트의 이동 (0) | 2023.02.18 |
C++ 10026 적록색약 (0) | 2023.02.17 |
C++ 섬의 개수 (0) | 2023.02.17 |