UVA_10422_Knights in FEN

<pre name="code" class="cpp">#include<iostream>    
#include<sstream>    
#include<string>    
#include<vector>    
#include<list>    
#include<set>    
#include<map>    
#include<stack>    
#include<queue>    
#include<algorithm>    
#include<cmath>    
#pragma warning(disable:4996)    
using std::cin;
using std::cout;
using std::endl;
using std::stringstream;
using std::string;
using std::vector;
using std::list;
using std::pair;
using std::set;
using std::multiset;
using std::map;
using std::multimap;
using std::stack;
using std::queue;
template<class ElemType>
class Position
{
public:
	ElemType first, second;
	Position()
	{
		first = second = 0;
	}
	Position(const ElemType &a, const ElemType &b)
	{
		first = a;
		second = b;
	}
	int assign(const ElemType &a, const ElemType &b)
	{
		first = a;
		second = b;
	}
	bool operator==(const Position &another)
	{
		return first == another.first&&second == another.second;
	}
	Position operator+(const Position &another)
	{
		return{ first + another.first,second + another.second };
	}
	Position operator-(const Position &another)
	{
		return{ first - another.first,second - another.second };
	}
	void operator+=(const Position &another)
	{
		first += another.first;
		second += another.second;
	}
	void operator-=(const Position &another)
	{
		first -= another.first;
		second -= another.second;
	}
	bool operator<(const ElemType &n)
	{
		return first >= 0 && first < n&&second >= 0 && second < n;
	}
	bool operator>(const ElemType &n)
	{
		return !(operator>(n));
	}
	bool isspace(vector<vector<char>>&board)
	{
		if (board[first][second] == ' ')
		{
			return true;
		}
		return false;
	}
	Position(vector<vector<char>>&board)
	{
		bool flag = false;
		for (first = 0; first < board.size(); first++)
		{
			for (second = 0; second < board.size(); second++)
			{
				if (isspace(board))
				{
					flag = true;
					break;
				}
			}
			if (flag)
			{
				break;
			}
		}
	}
};
const vector<vector<char>>key ={{ '1','1','1','1','1'},{ '0','1','1','1','1'},{ '0','0',' ','1','1'},{ '0','0','0','0','1'},{ '0','0','0','0','0'},};
const vector<Position<int>>direction={{-2,-1},{-2,1},{2,1},{2,-1},{-1,-2},{-1,2},{1,2},{1,-2}};
int bfs(const vector<vector<char>>&initial,Position<int>&space)
{
	if (initial == key)
	{
		return 0;
	}
	set<vector<vector<char>>>hash;
	hash.insert(initial);
	queue<vector<vector<char>>>Q1, Q2;
	queue<Position<int>>Q3; Q3.push(space);
	Q1.push(*hash.begin());
	int step = 1;
	while (1)
	{
		while (!Q1.empty())
		{
			auto board = Q1.front(); Q1.pop();
			space = Q3.front(); Q3.pop();
			for (size_t i = 0; i < direction.size(); i++)
			{
				if (space + direction[i] < board.size())
				{
					std::swap(board[(space + direction[i]).first][(space + direction[i]).second], board[space.first][space.second]);
					if (hash.find(board) == hash.end())//如果当前图并未出现过
					{
						if (board == key)//如果改变后的图是正确结果图
						{
							return step;
						}
						else//否则把这张图存入哈希表中,并且放入另外一个队列中
						{
							hash.insert(board);
							Q2.push(board);
							Q3.push(space + direction[i]);
						}
					}
					//恢复现场
					std::swap(board[(space + direction[i]).first][(space + direction[i]).second], board[space.first][space.second]);
				}
			}
		}
		step++;
		while (!Q2.empty())
		{
			auto board = Q2.front(); Q2.pop();
			space = Q3.front(); Q3.pop();
			for (size_t i = 0; i < direction.size(); i++)
			{
				if (space + direction[i] < board.size())
				{
					std::swap(board[(space + direction[i]).first][(space + direction[i]).second], board[space.first][space.second]);
					if (hash.find(board) == hash.end())//如果当前图并未出现过
					{
						if (board == key)//如果改变后的图是正确结果图
						{
							return step;
						}
						else//否则把这张图存入哈希表中,并且放入另外一个队列中
						{
							hash.insert( board);
							Q1.push(board);
							Q3.push(space+direction[i]);
						}
					}
					//恢复现场
					std::swap(board[(space + direction[i]).first][(space + direction[i]).second], board[space.first][space.second]);
				}
			}
		}
		step++;
		if (step == 11)
		{
			return 11;
		}
	}
}
int main()
{
	freopen("input.txt","r",stdin);    
	freopen("output.txt","w",stdout); 
	int T; cin >> T; cin.get();
	while (T--)
	{
		vector<vector<char>>board(5);
		for (int i = 0; i < 5; i++)
		{
			string str; getline(cin, str);
			board[i].assign(str.begin(), str.end());
		}
		Position<int> space(board);
		auto result = bfs(board, space);
		if (result != 11)
		{
			printf("Solvable in %d move(s).\n",result);
		}
		else
		{
			cout << "Unsolvable in less than 11 move(s)." << endl;
		}
	}
	return 0;
}


 

你可能感兴趣的:(hash,uva,bfs)