UVA810 筛子难题 A Dicey Problem(紫书刷题记录)

· · 题解

先看题意,骰子面朝上的点数与上下左右格子的点数相同即可移动,从骰子走出去之后绕一圈能回来的话就输出路径,回不来了就输出否。 仔细想一下,这不就是用bfs求最短路嘛,板子题,直接硬模拟就完事了,就是骰子的状态表示稍微有点难,这里写了个上下左右移动的函数来模拟骰子移动(多了个turnRound函数是用来模拟骰子原地旋转的,因为只靠上下左右滚动是无法设置骰子的初始状态的)。

以下是AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#define _for(i, a, b) for(int i = a; i < b; i++)
#define _rep(i, a, b) for(int i = a; i <= b; i++)
#define x first
#define y second
using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

const int MAXN = 10 + 10;

struct dice {   //骰子的状态表示
    int top, bottom, left, right, front, behind, r, c;
};

int buf[MAXN][MAXN], r, c, rs, cs, t, f;    //buf是地图, 后面为输入内容。

dice from[MAXN][MAXN][7][7], dicey, final;  //from用来记录骰子是从哪里滚来的。

string s;   //标题。

bool isok = false;  //是否能滚回原点。
//以下为模拟滚动的函数。
dice turnLeft(dice u) {
    int t = u.left;
    u.left = u.top, u.top = u.right, u.right = u.bottom, u.bottom = t;
    u.c--;
    return u;
}

dice turnRight(dice u) {
    int t = u.right;
    u.right = u.top, u.top = u.left, u.left = u.bottom, u.bottom = t;
    u.c++;
    return u;
}

dice turnDown(dice u) {
    int t = u.bottom;
    u.bottom = u.front, u.front = u.top, u.top = u.behind, u.behind = t;
    u.r++;
    return u;
}

dice turnUp(dice u) {
    int t = u.behind;
    u.behind = u.top, u.top = u.front, u.front = u.bottom, u.bottom = t;
    u.r--;
    return u;
}

dice turnRound(dice u) {
    int t = u.left;
    u.left = u.front, u.front = u.right, u.right = u.behind, u.behind = t;
    return u;
}

dice init(dice w) { //初始化骰子状态。
    isok = false;
    memset(from, 0, sizeof from);
    w.top = 3, w.bottom = 4, w.left = 2, w.right = 5, w.front = 6, w.behind = 1;
    while(w.top != t && w.top != f) w = turnLeft(w);
    if(w.top == f) {
        w = turnDown(w);
        while(w.top != t) w = turnLeft(w);
    } else while(w.front != f) w = turnRound(w);
    w.r = rs, w.c = cs;
    return w;
}

void bfs() {    //类似bfs求最短路的写法。
    queue<dice> q;
    bool isfir = true;
    q.push(dicey);
    while(!q.empty()) {
        dice t = q.front(); q.pop();
        if(t.r == rs && t.c == cs && !isfir) {isok = true; final = t; break;}
        if(isfir) isfir = false;
        dice v = turnUp(t);
        if(v.r + 1 <= r && (buf[v.r][v.c] == t.top || buf[v.r][v.c] == -1) && from[v.r][v.c][v.front][v.top].left == 0) {
            from[v.r][v.c][v.front][v.top] = t;
            q.push(v);
        }
        v = turnDown(t);
        if(v.r - 1 >= 1 && (buf[v.r][v.c] == t.top || buf[v.r][v.c] == -1) && from[v.r][v.c][v.front][v.top].left == 0) {
            from[v.r][v.c][v.front][v.top] = t;
            q.push(v);
        }
        v = turnRight(t);
        if(v.c <= c && (buf[v.r][v.c] == t.top || buf[v.r][v.c] == -1) && from[v.r][v.c][v.front][v.top].left == 0) {
            from[v.r][v.c][v.front][v.top] = t;
            q.push(v);
        }
        v = turnLeft(t);
        if(v.c >= 1 && (buf[v.r][v.c] == t.top || buf[v.r][v.c] == -1) && from[v.r][v.c][v.front][v.top].left == 0) {
            from[v.r][v.c][v.front][v.top] = t;
            q.push(v);
        }
    }
}

void print() {  //打印函数, 记得每行结尾也是得有 “,” 符号的, 万恶的UVa, 每天刷题都要和它那输出格式斗智斗勇。
    vector<dice> ans;
    ans.push_back(dicey);
    ans.push_back(from[final.r][final.c][final.front][final.top]);
    while(ans.back().r != rs || ans.back().c != cs) {
        dice t = ans.back();
        ans.push_back(from[t.r][t.c][t.front][t.top]);
    }
    int cnt = 0;
    for(int i = ans.size() - 1; i >= 0; i--) {
        if(cnt % 9 == 0) cout << "  ";
        printf("(%d,%d)", ans[i].r, ans[i].c);
        if(++cnt % 9 == 0 && cnt != ans.size()) cout << ',' << endl;
        else if(cnt != ans.size()) cout << ',';
    }
    cout << endl;
}

int main() {
    while(cin >> s && s != "END") {
        cin >> r >> c >> rs >> cs >> t >> f;
        _rep(i, 1, r) _rep(j, 1, c) cin >> buf[i][j];
        cout << s << endl;
        dicey = init(dicey);
        bfs();
        if(isok) print();
        else cout << "  No Solution Possible\n";
    }
    return 0;
}

第一次写题解,完结撒花。