-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem.cpp
88 lines (80 loc) · 1.97 KB
/
problem.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <map>
#include "problem.h"
using namespace std;
Problem::Problem(vector<string> b)
{
_stateCount = 0;
_board = b;
_height = _board.size();
if (_height <= 0)
throw;
_width = _board[0].length();
_goals.clear();
_queries.clear();
_counts.clear();
for (int i=0; i<_height; ++i)
{
if (_board[i].length() != _width)
{
_height = i;
break;
}
for(int j=0; j<_width; ++j)
{
if (_board[i][j] == 'P')
_startState = make_pair(i,j);
else if (_board[i][j] == '.')
_goals.push_back(make_pair(i,j));
}
}
}
pair<int, int> Problem::getStartState()
{
return _startState;
}
vector<pair<int, int> > Problem::getGoals()
{
return _goals;
}
vector<pair<int, int> > Problem::getSuccessors(pair<int, int> state)
{
++_stateCount;
if (_counts.count(state) == 0)
{
_counts[state] = 1;
_queries.push_back(state);
} else
++_counts[state];
vector<pair<int, int> > ret;
ret.clear();
if (state.first>0 && _board[state.first-1][state.second]!='%')
ret.push_back(make_pair(state.first-1, state.second));
if (state.first<_height-1 && _board[state.first+1][state.second]!='%')
ret.push_back(make_pair(state.first+1, state.second));
if (state.second>0 && _board[state.first][state.second-1]!='%')
ret.push_back(make_pair(state.first, state.second-1));
if (state.second<_width-1 && _board[state.first][state.second+1]!='%')
ret.push_back(make_pair(state.first, state.second+1));
return ret;
}
int Problem::getExpansionCounts()
{
return _stateCount;
}
void Problem::dumpQueries(string taskName)
{
//(i, j) => (j, h-1-i)
try{
ofstream fout(taskName.c_str());
fout << _stateCount << endl;
for (vector<pair<int, int> >::iterator iter = _queries.begin(); iter != _queries.end(); ++iter)
{
fout << iter->second << " " << _height - 1 - iter->first << endl;
}
fout.close();
} catch (...) {}
}