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.现在呢,游戏开始咯,在一个指定的空的小格子里 ...
随机推荐
- css3 3D变换和动画
3D变换和动画 建立3D空间,transform-style: preserve-3d perspective: 100px; 景深 perspective-origin:center center ...
- 显示推送数据到mq成功,但是mq管理器中消息数量没增长
看服务器上的mq配置,看看mq_log,是不是存储满了?
- GridView、Repeater获取当前行号
GridView: <%# Container.DataItemIndex+1 %> Repeater:<%# Container.ItemIndex+1%>
- iOS 网络与多线程--7.Performselector消息处理方法
创建一个IOSApp类 IOSApp.h文件 #import <Foundation/Foundation.h> @interface IOSApp : NSObject // 1.添加一 ...
- Android Fragment用法之给Activity创建事件回调
在某些案例中,可能需要Fragment与Activity共享事件.在Fragment内部定义一个回调接口是一个好方法,并且规定由持有它的Activity实现这个回调方法.当Activity通过接口接受 ...
- Java泛型的基本应用
一.泛型概述 jdk1.5版本以后出现的新特性,用于解决安全问题,是一个安全机制. 好处: 1,将运行时期的问题ClassCastException转到了编译时期. 2,避免了强制转换的麻烦. 什么时 ...
- Python----Tornado安装
Tornado安装,环境准备: 1.python安装包及安装 2.Tornado安装包 Python包安装 Linux下安装 如果使用的是 Linux系统 或 Mac OS X ,系 ...
- codevs 1220 数字三角形
1220 数字三角形 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 如图所示的数字三角形,从顶部出发,在每 ...
- 父Prefab与子prefab问题
PrefabRevolution 原文:http://framebunker.com/blog/poor-mans-nested-prefabs/ (溜还是老外溜啊) 有些时候需要在Prefab里 ...
- 将Android 工程从Windows导入到Ubuntu 下java类中的中文在ADT中乱码
解决方法: 右键工程-->Properties->Resource->Text file encoding->Other 手动输入GBK ->点击OK (Other 中是 ...