( Is that sth related to BFS + Hash table? I know nothing about hashing, so could anyone show me some reference on that issue? Thx
Moderator: Board moderators
#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <set>
using namespace std;
vector<vector<int> > map(4, vector<int>(6));
typedef set<vector<vector<int> > > my_set;
class step
{
public:
string path;
vector<vector<int> > map;
pair<int, int> loc;
step(string &path, vector<vector<int> > map, pair<int, int> &loc)
{
this->path = path;
this->map = map;
this->loc = loc;
}
};
void swap(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
void get_data(void)
{
for(size_t i = 0; i < 4; ++i)
for(size_t j = 0; j < 6; ++j)
cin >> map[i][j];
}
pair<int, int> operator + (const pair<int, int> &a, const pair<int, int> &b)
{
pair<int, int> sum;
sum.first = a.first + b.first;
sum.second = a.second + b.second;
return sum;
}
bool in_map(const pair<int, int> &loc)
{
if (loc.first >= 0 && loc.first <= 3 && loc.second >= 0 && loc.second <= 5)
return true;
else
return false;
}
void BFS(pair<int, int> &start)
{
my_set hash_table;
queue<step> q;
string temp;
map[start.first][start.second] |= (1 << 4);
q.push(step(temp, map, start));
pair<int, int> shift[4] = {pair<int, int>(0, -1), pair<int, int>(-1, 0), pair<int, int>(0, 1), pair<int, int>(1, 0)};
char symbol[4] = {'W', 'N', 'E', 'S'};
int anti_dir[4] = {2, 3, 0, 1};
while(!q.empty())
{
step current = q.front();
my_set::iterator iter = hash_table.find(current.map);
if (iter == hash_table.end())
{
hash_table.insert(iter, current.map);
for(size_t dir = 0; dir < 4; ++dir)
{
if ((current.map[current.loc.first][current.loc.second] & (1 << dir)) == 0)
{
pair<int, int> next_loc = current.loc + shift[dir];
string next_path = current.path + symbol[dir];
if (in_map(next_loc) == true)
{
vector<vector<int> > next_map(current.map);
next_map[current.loc.first][current.loc.second] &= ~(1 << 4);
next_map[next_loc.first][next_loc.second] |= (1 << 4);
q.push(step(next_path, next_map, next_loc));
}
else
{
cout << next_path << endl;
return;
}
}
else
{
pair<int, int> next_loc = current.loc + shift[dir];
if (in_map(next_loc) == true && (current.map[next_loc.first][next_loc.second] & (1 << dir)) == 0)
{
string next_path = current.path + symbol[dir];
vector<vector<int> > next_map(current.map);
next_map[current.loc.first][current.loc.second] &= ~(1 << 4);
next_map[current.loc.first][current.loc.second] &= ~(1 << dir);
next_map[next_loc.first][next_loc.second] |= (1 << dir);
next_map[next_loc.first][next_loc.second] &= ~(1 << anti_dir[dir]);
next_map[next_loc.first][next_loc.second] |= (1 << 4);
q.push(step(next_path, next_map, next_loc));
}
}
}
}
q.pop();
}
}
int main(int argc, char *argv[])
{
pair<int, int> start;
while((cin >> start.first >> start.second), start.first || start.second)
{
--start.first, --start.second;
swap(start.first, start.second);
get_data();
BFS(start);
}
return 0;
}
DD wrote:Is there any tricky test cases for this problem. I try to solve this problem by using BFS with memorizing to reduce the run time. However I got W.A. several times, please help me![]()
Following is my code:
- Code: Select all
#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <set>
using namespace std;
vector<vector<int> > map(4, vector<int>(6));
typedef set<vector<vector<int> > > my_set;
class step
{
public:
string path;
vector<vector<int> > map;
pair<int, int> loc;
step(string &path, vector<vector<int> > map, pair<int, int> &loc)
{
this->path = path;
this->map = map;
this->loc = loc;
}
};
void swap(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
void get_data(void)
{
for(size_t i = 0; i < 4; ++i)
for(size_t j = 0; j < 6; ++j)
cin >> map[i][j];
}
pair<int, int> operator + (const pair<int, int> &a, const pair<int, int> &b)
{
pair<int, int> sum;
sum.first = a.first + b.first;
sum.second = a.second + b.second;
return sum;
}
bool in_map(const pair<int, int> &loc)
{
if (loc.first >= 0 && loc.first <= 3 && loc.second >= 0 && loc.second <= 5)
return true;
else
return false;
}
void BFS(pair<int, int> &start)
{
my_set hash_table;
queue<step> q;
string temp;
map[start.first][start.second] |= (1 << 4);
q.push(step(temp, map, start));
pair<int, int> shift[4] = {pair<int, int>(0, -1), pair<int, int>(-1, 0), pair<int, int>(0, 1), pair<int, int>(1, 0)};
char symbol[4] = {'W', 'N', 'E', 'S'};
int anti_dir[4] = {2, 3, 0, 1};
while(!q.empty())
{
step current = q.front();
my_set::iterator iter = hash_table.find(current.map);
if (iter == hash_table.end())
{
hash_table.insert(iter, current.map);
for(size_t dir = 0; dir < 4; ++dir)
{
if ((current.map[current.loc.first][current.loc.second] & (1 << dir)) == 0)
{
pair<int, int> next_loc = current.loc + shift[dir];
string next_path = current.path + symbol[dir];
if (in_map(next_loc) == true)
{
vector<vector<int> > next_map(current.map);
next_map[current.loc.first][current.loc.second] &= ~(1 << 4);
next_map[next_loc.first][next_loc.second] |= (1 << 4);
q.push(step(next_path, next_map, next_loc));
}
else
{
cout << next_path << endl;
return;
}
}
else
{
pair<int, int> next_loc = current.loc + shift[dir];
if (in_map(next_loc) == true && (current.map[next_loc.first][next_loc.second] & (1 << dir)) == 0)
{
string next_path = current.path + symbol[dir];
vector<vector<int> > next_map(current.map);
next_map[current.loc.first][current.loc.second] &= ~(1 << 4);
next_map[current.loc.first][current.loc.second] &= ~(1 << dir);
next_map[next_loc.first][next_loc.second] |= (1 << dir);
next_map[next_loc.first][next_loc.second] &= ~(1 << anti_dir[dir]);
next_map[next_loc.first][next_loc.second] |= (1 << 4);
q.push(step(next_path, next_map, next_loc));
}
}
}
}
q.pop();
}
}
int main(int argc, char *argv[])
{
pair<int, int> start;
while((cin >> start.first >> start.second), start.first || start.second)
{
--start.first, --start.second;
swap(start.first, start.second);
get_data();
BFS(start);
}
return 0;
}
Users browsing this forum: No registered users and 1 guest