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.现在呢,游戏开始咯,在一个指定的空的小格子里 ...
随机推荐
- FFMPEG 视频旋转设置
fmpeg -i inputfile.mp4 -vf "transpose=1" outputfile.mp4 0=90CounterCLockwise and Vertical ...
- String or binary data would be truncated
在使用Typed Dataset进行数据的插入时,会报这样的错:String or binary data would be truncated. 我碰到的原因是 数据库中字段的长度过段,插入时内容被 ...
- 武汉科技大学ACM:1002: 华科版C语言程序设计教程(第二版)例题6.6
Problem Description 明天就要英语考试了,小明明正在挑灯夜 战背单词.小明明发现单词很难背,背一个忘一个.经过仔细研究,小明明发现单词难背的原因是因为某个字符的出现,破坏了整个单词的 ...
- 狗狗40题~ (Volume C)
A - Triangles 记忆化搜索呗.搜索以某三角形为顶的最大面积,注意边界情况. #include <stdio.h> #include <cstring> #inclu ...
- Memcache的基本应用
$mc = new Memcache(); $mc->connect('127.0.0.1', 11211); $sql = "select * from user where id= ...
- PHP获取指定年份指定月份的天数
最近写接口的时候突然发现的非常实用的php函数,在这儿分享一下: cal_days_in_month(calender,$month,$year): calender:历法,常量,如CAL_GREGO ...
- JGraph
php中JPGraph入门配置与应用 什么是PHP JPGraph?专门提供图表的类库.它使得作图变成了一件非常简单的事情.生成非美工人士生成的图表.二维码算法. 到官方网站下载.docportal ...
- JS判断字符串是否为空、过滤空格、查找字符串位置等函数集
这是一个由网上收集的JS代码段,用于判断指定字符串是否为空,过滤字符串中某字符两边的空格.查找指定字符串开始的位置.使用IsFloat函数判断一 个字符串是否由数字(int or long or fl ...
- Array and its point.
a is the array name. &a is the ponit of 2-D array which contains a[5]. the type of &a should ...
- OSTaskCreateExt() 建立任务
OSTaskCreateExt()建立任务 NT8U OSTaskCreateExt (void (*task)(void *pd), void *pdata, OS_STK *ptos, ...