UVA_10422_Knights in FEN
2016-04-08 22:37
351 查看
<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;
}
相关文章推荐
- jQuery之empty、remove、detach
- 前端复制功能的若干 -- document.execCommand()
- 解决 jsp:include 引用文件时出现乱码的问题
- FILE、inode、fork和vfork
- css清除浮动
- 3-jQuery - AJAX get()
- Angular 2知识整理第一篇 Hello,world!
- 前端开发学习笔记04---JavaScript
- 下拉菜单
- CSS3 @keyframe动画的基本用法与常见错误用法
- javascript动态在<div>中加载内容
- 名企动态网站开发--jQuery实现flash动画效果
- javascript内置对象
- javascript打算看的书
- 【剑指offer】斐波那契数列
- 学习记录--javascript多态
- 用JS模拟购物车(jQuery实现)
- js第四节-数据类型以及类型的转化
- 剑指Offer——构建数组B[i]=A[0]*A[1]*....A[i-1]*A[i+1]*A[i+2]*...A[n-1],不能使用除法
- 使用js正则表达式分割内容分别显示