POJ 2697 A Board Game (bfs模拟)
来源:互联网 发布:ubuntu tty u盘 编辑:程序博客网 时间:2024/04/27 19:20
比较水的一道题,在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
- POJ 2697 - A Board Game
- poj 2697 A Board Game-dfs
- POJ 2697. A Board Game
- POJ2697-A Board Game
- 【POJ 1691】 Painting A Board(dfs)
- Board Silly(poj 1558)模拟
- HDU3295An interesting mobile game(BFS +模拟)
- POJ 1691 Painting A Board
- poj 1691 Painting A Board
- poj 1691 Painting A Board
- POJ 1691 Painting A Board
- POJ 1691 Painting A Board
- 【poj 1691】Painting A Board
- POJ-1691 Painting A Board
- poj 1691 Painting A Board
- POJ-1691-Painting A Board
- photoshop 快捷键学习笔记
- 百度笔试题(长沙站)
- 字符串切割
- 马士兵java视频学习笔记第二章:递归调用
- java读取.properties文件
- POJ 2697 A Board Game (bfs模拟)
- Qt学习笔记之一,登录界面。
- linux设备驱动中的并发控制
- Big-M And Convex Hulls
- android 简单调用 系统分享
- 【JAVA资料免费下载】
- 2012Android开发热门资料
- HDU 2923 Einbahnstrasse (最短路,3级)
- Linux设备驱动中的并发控制总结