ETrobocon2017 - 片山研究所モデルベース開発推進事業部
PuzzleExplorer.cpp
[詳解]
1 
2 #include "PuzzleExplorer.h"
3 
4 // コンストラクタ
6 {
7  myPosition = 0;
8 }
9 
10 // デストラクタ
12 
13 // ブロック並べエリアのマップ、ブロックの初期配置などの初期設定
14 void PuzzleExplorer::init(int *blockPositions)
15 {
16 
17  //機体初期位置10
18  setMyPosition(10);
19 
20  // ノードの設定
21  setNodes();
22 
23  //ブロックの位置設定
24  setBlocks(blockPositions);
25 
26  // 隣接ノード設定
27  setNeighborNode();
28 
29 }
30 
31 // 現在地の変更
33 {
34  myPosition = num;
35  return;
36 }
37 
38 // ノードの設定(位置番号、色、位置座標)
39 void PuzzleExplorer::setNodes()
40 {
41  for(int i=0; i<16; i++)
42  {
43  Node node;
44  node.setNum(i);
45  node.setColor();
46  node.setPosition(nodePositionList[i][0], nodePositionList[i][1]);
47  nodeList[i] = node;
48 
49  }
50 }
51 
52 // ブロック設定
53 void PuzzleExplorer::setBlocks(int *blockPositions)
54 {
55  for(int i=0; i<5; i++)
56  {
57  Block block(blockPositions[i]);
58  blockList[i] = block;
59 
60  // ブロックを持つノード設定
61  nodeList[blockPositions[i]].setHasBlock(true);
62  }
63 
64  blockList[0].setColor(BlockColor::Black);
65  blockList[1].setColor(BlockColor::Red);
66  blockList[2].setColor(BlockColor::Yellow);
67  blockList[3].setColor(BlockColor::Blue);
68  blockList[4].setColor(BlockColor::Green);
69 
70  return;
71 }
72 
73 // 隣接ノードの設定
74 void PuzzleExplorer::setNeighborNode()
75 {
76  //nodeListから隣接ノードを取得
77  for(int i=0; i<16;i++)
78  {
79  Node* neighbor[5] = {};
80 
81  for(int j=0; j<5; j++)
82  {
83  // 隣接ノード番号が-1の時(隣接ノードがないとき)nullポインタを入れる
84  if(neighborList[i][j] == -1)
85  {
86  neighbor[j] = nullptr;
87  continue;
88  }
89  Node* node = &nodeList[neighborList[i][j]];
90  neighbor[j] = node;
91  }
92 
93  nodeList[i].setNeighbor(neighbor);
94  }
95  return;
96 }
97 
98 // 位置番号からノードの色を返す
100 {
101  return nodeList[num].getColor();
102 }
103 
104 // 現在地の隣接ノード取得
106 {
107  return nodeList[myPosition].getNeighbor();
108 }
109 
110 // 現地点から最寄りのブロックの位置を返す
112 {
113  Node node = nodeList[myPosition];
114  queue<Node> que;
115 
116  // ブロックを持つノードが見つかるまで幅優先探索
117  while(!node.getHasBlock())
118  {
119  Node** nodes = node.getNeighbor();
120 
121  // 隣接ノードをキューに格納
122  for(int i=0; i<5; i++){
123 
124  // 隣接ノードがnullの時は飛ばす
125  if(nodes[i] == nullptr)continue;
126 
127  que.push(*nodes[i]);
128  }
129 
130  node = que.front();
131 
132  // キューの先頭がブロックを持っていたら終了
133  if(que.front().getHasBlock())
134  {
135  break;
136  }
137 
138  // 先頭要素の削除
139  que.pop();
140  }
141 
142  return node.getNum();
143 }
144 
145 // 2つのノード間の経路探索
146 int* PuzzleExplorer::getRoot(int startNode, int goalNode)
147 {
148  Node node = nodeList[startNode];
149  Node goal = nodeList[goalNode];
150  int i=0;
151 
152  // rootに経路を格納
153  root[i] = node.getNum();
154  i++;
155 
156  // ゴールノードに到着するまで探索
157  while(node.getNum() != goal.getNum())
158  {
159  int cost = 1000;
160  Node** explorerNodes = node.getNeighbor();
161  Node nextNode;
162 
163  // 隣接ノードからゴールまでのコストが低いノードを次の探索ノードに選択
164  for(int j=0; j<5; j++)
165  {
166  //隣接ノードがnullの時飛ばす
167  if(explorerNodes[j] == nullptr)continue;
168 
169  // ゴールノードまでの距離コストが小さければ更新
170  int neighborCost = getCost(*explorerNodes[j], goal);
171  if(cost > neighborCost)
172  {
173  cost = neighborCost;
174  nextNode = *explorerNodes[j];
175  }
176  }
177 
178  root[i] = nextNode.getNum();
179  node = nextNode;
180  i++;
181  }
182 
183  return root;
184 }
185 
186 // ノード間の距離コストを計算
187 int PuzzleExplorer::getCost(Node node1, Node node2)
188 {
189  int cost = (node1.getPositionX() - node2.getPositionX())*(node1.getPositionX() - node2.getPositionX()) + (node1.getPositionY() - node2.getPositionY())*(node1.getPositionY() - node2.getPositionY());
190 
191  return cost;
192 }
void setPosition(int x, int y)
Definition: Node.cpp:85
void setMyPosition(int num)
int getCost(Node node1, Node node2)
Definition: Node.h:9
int getPositionX()
Definition: Node.cpp:91
Node ** getNeighbor()
Definition: Node.cpp:57
void setHasBlock(bool exists)
Definition: Node.cpp:75
BlockColor
Definition: BlockColor.h:5
void init(int *blockPositions)
BlockColor getColor()
Definition: Node.cpp:69
bool getHasBlock()
Definition: Node.cpp:80
void setColor()
Definition: Node.cpp:25
int getPositionY()
Definition: Node.cpp:96
void setColor(BlockColor blockColor)
Definition: Block.cpp:19
Definition: Block.h:9
Node ** getMyNeighbor()
BlockColor getNodeColor(int num)
void setNum(int num)
Definition: Node.cpp:20
void setNeighbor(Node **nodes)
Definition: Node.cpp:47
int * getRoot(int startNode, int goalNode)
int getNum()
Definition: Node.cpp:63
int getNearestBlockPosition()