比较水的一道题,在4*4的棋盘上有黑白子,现在有某种移动方式,问能否通过它将棋盘从某个状态移动到另一种状态

只要想好怎么保存hash表来去重,其他就差不多了...

#include <iostream>
#include <algorithm>
#include <cmath>
#include<functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <climits>//形如INT_MAX一类的
#define MAX 100005
#define INF 0x7FFFFFFF
#define REP(i,s,t) for(int i=(s);i<=(t);++i)
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define mp(a,b) make_pair(a,b)
#define L(x) x<<1
#define R(x) x<<1|1
# define eps 1e-5
//#pragma comment(linker, "/STACK:36777216") ///传说中的外挂
//#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std; struct node {
string s;
int step;
} a,st,end;
map <string, int > vis;
queue <node> q; struct MAP {
string s;
} pos[1111]; int cnt;
char tmp[5];
string mp[4];
void init() {
st.s = "";
end.s = "";
vis.clear();
while(! q.empty()) q.pop();
} void move(int posx,int posy, int x,int y, char c) {
string s = "";
for(int i=0; i<4; i++) {
for(int j=0; j<4; j++) {
if(i == posx && j == posy) {
s += c;
} else if(i == x && j == y) {
s += '*';
} else s += mp[i][j];
}
}
pos[cnt++].s = s;
} void trans(int x,int y,char c) {
int posx = x;
int posy = y;
for(int i=x; i>=0; i--) {
posx = i;
if(i - 1 < 0 || mp[i-1][y] == 'b' || mp[i-1][y] == 'w') break;
}
move(posx,posy,x,y,c); posx = x;
posy = y;
for(int i=x; i<4; i++) {
posx = i;
if(i + 1 >= 4 || mp[i+1][y] == 'b' || mp[i+1][y] == 'w') break;
}
move(posx,posy,x,y,c); posx = x;
posy = y;
for(int i=y; i<4; i++) {
posy = i;
if(i + 1 >= 4 || mp[x][i+1] == 'b' || mp[x][i+1] == 'w') break;
}
move(posx,posy,x,y,c); posx = x;
posy = y;
for(int i=y; i>=0; i--) {
posy = i;
if(i - 1 < 0 || mp[x][i-1] == 'b' || mp[x][i-1] == 'w') break;
}
move(posx,posy,x,y,c);
} void solve() {
cnt = 0;
for(int i=0; i<4; i++) {
for(int j=0; j<4; j++) {
if(mp[i][j] == 'b' || mp[i][j] == 'w') {
trans(i,j,mp[i][j]);
}
}
}
} int bfs() {
vis[st.s] = 1;
st.step = 0;
q.push(st) ;
while(! q.empty()) {
node t = q.front();
q.pop();
//cout << t.s << ' ' << t.step << endl;
if(t.s == end.s) {
return t.step;
}
for(int i=0; i<4; i++) mp[i].clear();
for(int i=0; i<4; i++) {
for(int j=0; j<4; j++) {
mp[i] += t.s[i * 4 + j];
}
}
solve();
node tt;
for(int i=0; i<cnt; i++) {
if(vis[pos[i].s] == 1) continue;
vis[pos[i].s] = 1;
tt.s = pos[i].s;
tt.step = t.step + 1;
q.push(tt);
}
}
return -1;
} int main() {
int T;
cin >> T;
while(T--) {
init();
for(int i=0; i<4; i++) {
scanf("%s",tmp);
st.s += tmp;
}
for(int i=0; i<4; i++) {
scanf("%s",tmp);
end.s += tmp;
}
printf("%d\n",bfs());
}
return 0;
}

POJ 2697 A Board Game (bfs模拟)的更多相关文章

  1. poj 2697 A Board Game(bfs+hash)

    Description Dao was a simple two-player board game designed by Jeff Pickering and Ben van Buskirk at ...

  2. POJ 2697 A Board Game(Trie判重+BFS)

    A Board Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 551   Accepted: 373 Descri ...

  3. POJ.3087 Shuffle'm Up (模拟)

    POJ.3087 Shuffle'm Up (模拟) 题意分析 给定两个长度为len的字符串s1和s2, 接着给出一个长度为len*2的字符串s12. 将字符串s1和s2通过一定的变换变成s12,找到 ...

  4. BFS+模拟 ZOJ 3865 Superbot

    题目传送门 /* BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少: BFS分4种情况入队,最后在终点4个方向寻找最小值:) */ #include <cstdio&g ...

  5. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  6. POJ 1426 Find The Multiple --- BFS || DFS

    POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...

  7. hdu_1495_非常可乐(bfs模拟)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 题意:不解释 题解:BFS模拟,不过要细心,把所有情况都列举出来,开一个数组记录状态,代码有点长 ...

  8. POJ.3894 迷宫问题 (BFS+记录路径)

    POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...

  9. Hdu 5336 XYZ and Drops (bfs 模拟)

    题目链接: Hdu 5336 XYZ and Drops 题目描述: 有一个n*m的格子矩阵,在一些小格子里面可能会有一些水珠,每个小水珠都有一个size.现在呢,游戏开始咯,在一个指定的空的小格子里 ...

随机推荐

  1. QQ群开放接口

    http://qun.qq.com/open.html#click http://my.oschina.net/ij2ee/blog/191692

  2. Mysql数据数据[字节、长度、数据范围]一览表

    1.mysql有哪些数据类型: 主要包括以下五大类: 整数类型:BIT.BOOL.TINY INT.SMALL INT.MEDIUM INT. INT. BIG INT 浮点数类型:FLOAT.DOU ...

  3. 保障MySQL安全的14个最佳方法

    MySQL数据库一贯以高性能.高可性和易用性著称,它已经成为世界上最流行的开源数据库.大量的个人.WEB开发者.大型公司等都在其网站.关键系统.软件包中广泛使用MySQL数据库.        通常, ...

  4. libthrift0.9.0解析(一)之TServer

    TServer 属性serverTransport 为TServerTransport类型,类图如下: 构造函数,简单根据args设置几个成员,大部分是工厂类: protected TServer(A ...

  5. [转]C++强制类型转换

    dynamic_cast:   通常在基类和派生类之间转换时使用,run-time   castconst_cast:   主要针对const和volatile的转换. static_cast:    ...

  6. 使用ecshop电子商务系统的100个小问题

    1:如何修改网站"欢迎光临本店" 回答:languages\zh_cn\common.php文件中, $_LANG['welcome'] = '欢迎光临本店';将他修改成你需要的字 ...

  7. 标准SQL语言的用法

    原文链接:http://www.ifyao.com/2015/05/18/%E6%A0%87%E5%87%86%E7%9A%84sql%E8%AF%AD%E8%A8%80%E4%BD%BF%E7%94 ...

  8. golang byte转string 字节数组转字符串的问题

    golang语言本身就是c的工具集,开发c的程序用到的大部分结构体,内存管理,携程等,golang基本都有,他只是在这个基础上又加了一些概念这里说一个很小的问题,就是字节数组转string的问题,网上 ...

  9. 无缝滚动js (手写通俗易懂)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  10. part3

    我就不刁旭概念什么的问题了哈~直接进入工作中所通用的一些有关object的方法和属性 1).for/in 属性的遍历~他遍历的对象没有特定的顺序,而且只能枚举出所有用户自定义的属性,不能枚举出某些预定 ...