Algoogle

Algorithm for Programming Contest

AOJ 1181 Biased Dice

Category: AOJ Tag: implementation

Biased Dice

問題概要


解法


サイコロの各位置に対応するように大きさ6の配列に数字を入れる.
フィールドにその位置の高さと数字を入れて, サイコロがそれ以上動けなくなったら更新.
その時のトップの数字のカウントを増やして, その下の数字のカウントを減らす.

コード


(1181.cpp) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <bits/stdc++.h>
#define repi(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,a) repi(i,0,a)
#define fst first
#define snd second
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;

const int offset = 128;

// f r b l
int dx[4] = {-1,0,1,0};
int dy[4] = {0,-1,0,1};

void rot(vi &dice, int dir){
    int tmp = dice[5];
    dice[5] = dice[(dir+2)%4]; dice[(dir+2)%4] = dice[4];
    dice[4] = dice[dir]; dice[dir] = tmp;
    return;
}

int main(){
    int n;
    while(cin >> n, n){
        pii field[256][256];
        int ans[7] = {0};
        rep(i,256)rep(j,256) field[i][j] = pii(0,0);
        while(n--){
            vi dice(4);
            int t, f; cin >> t >> f;
            dice[5] = t;
            dice[4] = 7 - t;
            dice[0] = f;
            dice[2] = 7 - f;
            if(t == 1){
                if(f == 2) dice[1] = 3;
                if(f == 3) dice[1] = 5;
                if(f == 5) dice[1] = 4;
                if(f == 4) dice[1] = 2;
            }
            if(t == 2){
                if(f == 1) dice[1] = 4;
                if(f == 4) dice[1] = 6;
                if(f == 6) dice[1] = 3;
                if(f == 3) dice[1] = 1;
            }
            if(t == 3){
                if(f == 1) dice[1] = 2;
                if(f == 2) dice[1] = 6;
                if(f == 6) dice[1] = 5;
                if(f == 5) dice[1] = 1;
            }
            if(t == 4){
                if(f == 1) dice[1] = 5;
                if(f == 5) dice[1] = 6;
                if(f == 6) dice[1] = 2;
                if(f == 2) dice[1] = 1;
            }
            if(t == 5){
                if(f == 1) dice[1] = 3;
                if(f == 3) dice[1] = 6;
                if(f == 6) dice[1] = 4;
                if(f == 4) dice[1] = 1;
            }
            if(t == 6){
                if(f == 2) dice[1] = 4;
                if(f == 4) dice[1] = 5;
                if(f == 5) dice[1] = 3;
                if(f == 3) dice[1] = 2;
            }
            dice[3] = 7 - dice[1];
            int x = 0 + offset, y = 0 + offset;
            while(true){
                int dir = -1, hight = field[x][y].fst, maxa = 0;
                rep(i,4){
                    if(dice[i] >= 4 && dice[i] > maxa && field[x+dx[i]][y+dy[i]].fst < hight) {
                        maxa = dice[i];
                        dir = i;
                    }
                }
                if(dir == -1)break;
                rot(dice, dir);
                x += dx[dir]; y += dy[dir];
            }
            ans[dice[5]]++;
            ans[field[x][y].snd]--;
            field[x][y].fst++;
            field[x][y].snd = dice[5];
        }
        repi(i,1,7) cout << ans[i] << (i==6? '\n': ' ');
    }
    return 0;
}

Comments