UVa 810 - A Dicey Problem

#include <bits/stdc++.h>
using namespace std;
const int maxn = 20;
struct Die {
    int x, y, tp, ft;
    Die() {}
    Die(int _x, int _y, int _tp, int _ft):
        x(_x), y(_y), tp(_tp), ft(_ft){}
};
int dis[maxn][maxn][7][7];
int maze[maxn][maxn];
int top_front[maxn][maxn];
int r,c,beg,ed,btp,bft;
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};//下上右左
Die father[maxn][maxn][7][7];
void prepare() {
    top_front[1][5] = 3, top_front[1][3] = 2, top_front[1][2] = 4, top_front[1][4] = 5;
    top_front[2][1] = 3, top_front[2][3] = 6, top_front[2][6] = 4, top_front[2][4] = 1;
    top_front[3][1] = 5, top_front[3][5] = 6, top_front[3][6] = 2, top_front[3][2] = 1;
    top_front[4][1] = 2, top_front[4][2] = 6, top_front[4][6] = 5, top_front[4][5] = 1;
    top_front[5][1] = 4, top_front[5][4] = 6, top_front[5][6] = 3, top_front[5][3] = 1;
    top_front[6][2] = 3, top_front[6][3] = 5, top_front[6][5] = 4, top_front[6][4] = 2;
}

bool read() {
    string str;
    cin >> str;
    if(str=="END") return false;
    cout << str;
    memset(maze, 0, sizeof(maze));
    memset(dis, -1, sizeof(dis));
    memset(father, 0, sizeof(father));
    cin >> r >> c >> beg >> ed >> btp >> bft;
    for(int i = 1; i <= r; i++) {
        for(int j = 1; j <= c; j++)
            cin >> maze[i][j];
    }
    return true;
}

Die rotate(Die cur,int i) {
    Die next = cur;
    next.x += dir[i][0], next.y += dir[i][1];
    if(i==0) {//上
        next.ft = 7 - next.ft; swap(next.ft, next.tp); }
    else if(i==1) {//下
        next.tp = 7 - next.tp; swap(next.ft, next.tp); }
    else if(i==2)//右
        next.tp = top_front[cur.tp][cur.ft];
    else //左
        next.tp = 7-top_front[cur.tp][cur.ft];
    return next;
}

bool inside(Die next) {
    return next.x>0 && next.x<=r && next.y>0 && next.y<=c;
}

bool feasible(Die next, Die cur) {
    if(maze[next.x][next.y]==-1) return true;
    else if(maze[next.x][next.y]==cur.tp) return true;
    return false;
}

void print(Die u) {
    vector<Die> vec;
    while(true) {
        vec.push_back(u);
        if(dis[u.x][u.y][u.tp][u.ft]==0) break;
        u = father[u.x][u.y][u.tp][u.ft];
    }
    int cnt = 0;
    bool flag = false, f = false;
    for(int i = (int)vec.size()-1; i >= 0; i--) {
        if(cnt++%9==0) {
            if(!f) {
                cout << endl << "  ";
                f = true;
            }
            else cout << "," << endl << "  ";
            flag = false;
        }
        if(!flag) {
            cout << "(" << vec[i].x << "," << vec[i].y << ")";
            flag = true;
        }
        else cout << ",(" << vec[i].x << "," << vec[i].y << ")";
    }
    cout << ",(" << beg << "," << ed << ")";
    cout << endl;
}

void bfs() {
    queue<Die> queue1;
    queue1.push(Die(beg,ed,btp,bft));
    dis[beg][ed][btp][bft] = 0;
    while(!queue1.empty()) {
        Die cur = queue1.front(); queue1.pop();
        for(int i = 0; i < 4; i++) {
            Die next = rotate(cur, i);
            cout << next.x << " " << next.y << " " << next.tp << " " << next.ft << endl;
            if(!inside(next) || !feasible(next, cur)) continue;
            if(next.x==beg && next.y==ed) {
                print(cur);
                return;
            }
            if(dis[next.x][next.y][next.tp][next.ft]<0) {
                queue1.push(next);
                dis[next.x][next.y][next.tp][next.ft] = dis[cur.x][cur.y][cur.tp][cur.ft]+1; 
                father[next.x][next.y][next.tp][next.ft] = cur;
            }
        }
    }
    cout << "\n  No Solution Possible"<<endl;
    return;
}

int main() {
    freopen("i.txt","r",stdin);;
    freopen("o.txt","w",stdout); 
    prepare();
    while(read()) {
        bfs();
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值