POJ 2697 A Board Game (bfs模拟)
比较水的一道题,在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模拟)的更多相关文章
- 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 ...
- POJ 2697 A Board Game(Trie判重+BFS)
A Board Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 551 Accepted: 373 Descri ...
- POJ.3087 Shuffle'm Up (模拟)
POJ.3087 Shuffle'm Up (模拟) 题意分析 给定两个长度为len的字符串s1和s2, 接着给出一个长度为len*2的字符串s12. 将字符串s1和s2通过一定的变换变成s12,找到 ...
- BFS+模拟 ZOJ 3865 Superbot
题目传送门 /* BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少: BFS分4种情况入队,最后在终点4个方向寻找最小值:) */ #include <cstdio&g ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- POJ 1426 Find The Multiple --- BFS || DFS
POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...
- hdu_1495_非常可乐(bfs模拟)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 题意:不解释 题解:BFS模拟,不过要细心,把所有情况都列举出来,开一个数组记录状态,代码有点长 ...
- POJ.3894 迷宫问题 (BFS+记录路径)
POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...
- Hdu 5336 XYZ and Drops (bfs 模拟)
题目链接: Hdu 5336 XYZ and Drops 题目描述: 有一个n*m的格子矩阵,在一些小格子里面可能会有一些水珠,每个小水珠都有一个size.现在呢,游戏开始咯,在一个指定的空的小格子里 ...
随机推荐
- rmi rpc restful soa 区别
rmi rpc restful soa 区别 rmi vs rpc 参考文档:http://stackoverflow.com/questions/2728495/what-is-the-differ ...
- java沙箱机制原理
参考文档如下: http://www.2cto.com/kf/201012/79578.html
- ADSL拨号连接
dotras是一个提供远程访问服务的组件,使用它可以方便的 进行宽带拨号连接 由于不同的系统中dotras调用的底层api可能不同,所以使用时要根据不同的系统使用不同的dotras. dotras目前 ...
- android studio adb 打不开
1.cmd-->C:\Users\Administrator>adb start-serveradb server is out of date. killing...error: cou ...
- php学习之路
1.php拼接字符串+查询 $floor_id = M('house_floor_input')->where($map1)->field('id')->select(); $flo ...
- web项目环境搭建(1):建立一个maven项目
一.maven简介以及常用概念 1.Maven是一个项目管理和整合的工具.Maven为开发者提供了一套完整的构建生命周期框架.开发团队基本不用花多少时间就能自动完成工程的基础构建配置,因为Maven使 ...
- Oracle 错误码
Oracle作为一款比较优秀同时也比较难以掌握的大型数据库,在我们学习使用的过程中,不可避免的会遇到一些错误,为此 Oracle 给出了一套完备的错误消息提示机制 我们可以根据Oracle给出的消息提 ...
- 单点登录CAS使用记(七):关于服务器超时以及客户端超时的分析
我的预想情况 一般情况下,当用户登录一个站点后,如果长时间没有发生任何动作,当用户再次点击时,会被强制登出并且跳转到登录页面, 提醒用户重新登录.现在我已经为站点整合了CAS,并且已经实现了单点登录以 ...
- 【转载】经典10道c/c++语言经典笔试题(含全部所有参考答案)
经典10道c/c++语言经典笔试题(含全部所有参考答案) 1. 下面这段代码的输出是多少(在32位机上). char *p; char *q[20]; char *m[20][20]; int (*n ...
- 3.1 as86汇编器
在开始讲述as86汇编器前,这本书引用内核中bootsect.s框架程序汇编代码来解释,记录下这一小段代码中不理解的地方,下面是这段实例代码: .globl begtext, begdata, beg ...