-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQItem.cpp
More file actions
67 lines (50 loc) · 1.87 KB
/
QItem.cpp
File metadata and controls
67 lines (50 loc) · 1.87 KB
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
67
#include "QItem.h"
int QItem::minDistance(Board& board, int curGhostY, int curGhostX, int curPacmanY, int curPacmanX)//i is the current ghost
{
QItem source(0, 0, 0);
//starting position of ghost
source.row = curGhostY;
source.col = curGhostX;
//mark the valid places (not wall) for ghost steps
bool visited[ROW][COL];
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++)
{
if (board.getMatGame(i, j) == '#')
visited[i][j] = true;
else
visited[i][j] = false;
}
}
std::queue<QItem> q;
q.push(source);
visited[source.row][source.col] = true;
while (!q.empty()) {
QItem item = q.front();
q.pop();
// pacman found;
if (item.row == curPacmanY && item.col == curPacmanX)
return item.dist;
// moving up
if (item.row - 1 >= 0 && visited[item.row - 1][item.col] == false) {
q.push(QItem(item.row - 1, item.col, item.dist + 1));
visited[item.row - 1][item.col] = true;
}
// moving down
if (item.row + 1 < board.getDown() && visited[item.row + 1][item.col] == false) {
q.push(QItem(item.row + 1, item.col, item.dist + 1));
visited[item.row + 1][item.col] = true;
}
// moving left
if (item.col - 1 >= 0 && visited[item.row][item.col - 1] == false) {
q.push(QItem(item.row, item.col - 1, item.dist + 1));
visited[item.row][item.col - 1] = true;
}
// moving right
if (item.col + 1 < board.getRight() && visited[item.row][item.col + 1] == false) {
q.push(QItem(item.row, item.col + 1, item.dist + 1));
visited[item.row][item.col + 1] = true;
}
}
return -1;
}