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;
}


 

 

posted @ 2013-09-30 21:54  pangbangb  阅读(174)  评论(0编辑  收藏  举报