-
Notifications
You must be signed in to change notification settings - Fork 0
/
7562_나이트의 이동.cpp
66 lines (56 loc) · 1.34 KB
/
7562_나이트의 이동.cpp
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
66
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
using namespace std;
int board[301][301];
int visited[301][301];
int cntBoard[301][301];
int dirX[8] = { 2, 2, -2, -2, 1, 1, -1, -1 };
int dirY[8] = { 1, -1, 1, -1, 2, -2, 2, -2, };
int main(void)
{
int l, testCase;
int startX, startY, destX, destY;
vector<pair<int, int>> queue;
vector<int> counts;
pair<int, int> curLoc;
cin >> testCase;
for (int i = 0; i < testCase; i++)
{
cin >> l;
cin >> startX >> startY >> destX >> destY;
for (int i = 0; i < 301; i++)
{
for (int j = 0; j < 301; j++)
{
visited[i][j] = 0;
cntBoard[i][j] = 0;
}
}
queue.push_back({ startX, startY });
while (!queue.empty())
{
curLoc = queue.front();
queue.erase(queue.begin());
if (curLoc.first == destX && curLoc.second == destY)
break;
for (int i = 0; i < 8; i++)
{
int newX = curLoc.first + dirX[i];
int newY = curLoc.second + dirY[i];
if ((newX >= 0 && newX < l) && (newY >= 0 && newY < l) && visited[newX][newY] == 0)
{
queue.push_back({ newX, newY });
visited[newX][newY] = 1;
cntBoard[newX][newY] += cntBoard[curLoc.first][curLoc.second] + 1;
}
}
}
queue.clear();
counts.push_back(cntBoard[curLoc.first][curLoc.second]);
}
for (int i = 0; i < testCase; i++)
cout << counts[i] << "\n";
return 0;
}