比较水的一道题,在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. Flask挺好

    很久没写东西了,寒假比较低效,几乎没写代码.只在慕课网上刷完了linux系列课程,现在用linux熟了很多以及看了大部分<鸟叔-linux服务器架设>那本书,虽然对于写代码并没有什么卵用, ...

  2. share js 分享代码

    (function(){ var $doc = $(document); var shareHandlers = { 'twitter': function(prop,shareUrl){ var D ...

  3. 【转】Qt之模型/视图

    [本文转自]http://blog.sina.com.cn/s/blog_a6fb6cc90101hh20.html   作者: 一去丶二三里 关于Qt中MVC的介绍与使用,助手中有一节模型/视图编程 ...

  4. uva 10154 贪心+dp

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  5. PHP+mysql统计排名第几位

    正在开发积分系统!其中有一项数据是显示用户积分排名?一下子想不到太好的办法! 最简的情况是统一某一字段的积分数据排名?比如积分字段,里面存的整数! 如何排名?或者说如何获得他在排序中的序列位次呢? s ...

  6. 查看文件系统类型的Linux命令

    不需挂载就能查看的命令:   1. file  [root@localhost dev]# file -s /dev/sda1 /dev/sda1: Linux rev 1.0 ext4 filesy ...

  7. Project: Individual Project - Word frequency program-11061160顾泽鹏

    一.预计用时: (1)明确要求:15min: (2)文件的遍历:1h: (3)Simple mode 词频统计:0.5h: (4)extend mode 词频统计:1h: (5)对单词词频排序输出:0 ...

  8. 对PHP安全有帮助的一些函数

    安全一直是一个在编程语言中非常值得去关注的方面.在任何一种成熟的编程语言中都有合适的办法来保证程序的安全性,在现代的 WEB 开发中 安全一直是一个在编程语言中非常值得去关注的方面.在任何一种成熟的编 ...

  9. 创建Windows服务简单流程

    1.首先打开VS2010(或者其他版本),创建Windows服务项目 2.创建完成后切换到代码视图,代码中默认有OnStart和OnStop方法执行服务开启和服务停止执行的操作,下面代码是详细解释: ...

  10. QuerryRunner Tools

    package com.ydbg.gis.utils; import java.sql.SQLException; import java.util.List; import java.util.Ma ...