AOJ プログラミング入門 トピック#11

#トピック#11
##[ITP1_11_A](https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/11/ITP1_11_A)
###サイコロ I

```C++:C++
#include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<utility>
#include<iomanip>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cctype>

#define rep(i,n) for(int i=0; i<(n); ++i)
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9

using namespace std;
using ll =long long;
using P = pair<int,int>;

struct Dice{
private:
    int n_, w_, s_, e_, t_, u_;
    
public:
    Dice(int t, int s, int e, int w, int n, int u){
        t_ = t; s_ = s; e_ = e; w_ = w; n_ = n; u_ = u;
    }
    
    void TurnN(){
        int tmp = t_; t_ = s_; s_ = u_; u_ = n_; n_ = tmp;
    }
    
    void TurnW(){
        int tmp = t_; t_ = e_; e_ = u_; u_ = w_; w_ = tmp;
    }
    
    void TurnS(){
        int tmp = t_; t_ = n_; n_ = u_; u_ = s_; s_ = tmp;
    }
    
    void TurnE(){
        int tmp = t_; t_ = w_; w_ = u_; u_ = e_; e_ = tmp;
    }
    
    int Top(){
        return t_;
    }
};

void solve(int t, int s, int e, int w, int n, int u, string str){
    
    Dice d(t, s, e, w, n, u);
    
    rep(i, str.length()){
        char ch = str[i];
        if(ch == 'N') d.TurnN();
        else if(ch == 'W') d.TurnW();
        else if(ch == 'S') d.TurnS();
        else if(ch == 'E') d.TurnE();
    }
    
    cout << d.Top() << endl;
}

int main(int argc, const char * argv[]) {
    int t, s, e, w, n, u;
    cin >> t >> s >> e >> w >> n >> u;
    string str;
    cin >> str;
    
    solve(t, s, e, w, n, u, str);
    
    return 0;
}
```

##[ITP1_11_B](https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/11/ITP1_11_B)
###サイコロ II

```C++:C++
#include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<utility>
#include<iomanip>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cctype>

#define rep(i,n) for(int i=0; i<(n); ++i)
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9

using namespace std;
using ll =long long;
using P = pair<int,int>;

struct Dice{
private:
    int n_, w_, s_, e_, t_, u_;
    
public:
    Dice(int t, int s, int e, int w, int n, int u){
        t_ = t; s_ = s; e_ = e; w_ = w; n_ = n; u_ = u;
    }
    
    void TurnN(){
        int tmp = t_; t_ = s_; s_ = u_; u_ = n_; n_ = tmp;
    }
    
    void TurnW(){
        int tmp = t_; t_ = e_; e_ = u_; u_ = w_; w_ = tmp;
    }
    
    void TurnS(){
        int tmp = t_; t_ = n_; n_ = u_; u_ = s_; s_ = tmp;
    }
    
    void TurnE(){
        int tmp = t_; t_ = w_; w_ = u_; u_ = e_; e_ = tmp;
    }
    
    void TurnR(){
        int tmp = n_; n_ = w_; w_ = s_; s_ = e_; e_ = tmp;
    }
    
    int GetTop(){
        return t_;
    }
    
    int GetN(){
        return n_;
    }
    
    int GetE(){
        return e_;
    }
    
    int GetW(){
        return w_;
    }
};

void solve(int t, int s, int e, int w, int n, int u, vector<P> vec){
    
    Dice d(t, s, e, w, n, u);
    
    rep(i, vec.size()){
        P p = vec[i];
        
        bool is_true = false;
        rep(i, 4){
            if(d.GetTop() == p.second){
                is_true = true;
                break;
            }
            else d.TurnN();
        }
        if(!is_true){
            d.TurnW();
            if(d.GetTop() != p.second){
                d.TurnE();
                d.TurnE();
            }
        }

        rep(i, 4){
            if(d.GetN() == p.first){
                cout << d.GetE() << endl;
                break;
            }
            else d.TurnR();
        }
    }
}

int main(int argc, const char * argv[]) {
    int t, s, e, w, n, u;
    cin >> t >> s >> e >> w >> n >> u;
    int c=0;
    cin >> c;
    vector<P> vec;
    rep(i, c){
        P p;
        cin >> p.first >> p.second;
        vec.push_back(p);
    }
    
    solve(t, s, e, w, n, u, vec);
    
    return 0;
}
```

##[ITP1_11_C](https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/11/ITP1_11_C)
###サイコロ III

```C++:C++
#include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<utility>
#include<iomanip>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cctype>

#define rep(i,n) for(int i=0; i<(n); ++i)
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9

using namespace std;
using ll =long long;
using P = pair<int,int>;

struct Dice{
private:
    int n_, w_, s_, e_, t_, u_;
    
public:
    Dice(){
    }
    Dice(int t, int s, int e, int w, int n, int u){
        t_ = t; s_ = s; e_ = e; w_ = w; n_ = n; u_ = u;
    }
    
    void TurnN(){
        int tmp = t_; t_ = s_; s_ = u_; u_ = n_; n_ = tmp;
    }
    
    void TurnW(){
        int tmp = t_; t_ = e_; e_ = u_; u_ = w_; w_ = tmp;
    }
    
    void TurnS(){
        int tmp = t_; t_ = n_; n_ = u_; u_ = s_; s_ = tmp;
    }
    
    void TurnE(){
        int tmp = t_; t_ = w_; w_ = u_; u_ = e_; e_ = tmp;
    }
    
    void TurnR(){
        int tmp = n_; n_ = w_; w_ = s_; s_ = e_; e_ = tmp;
    }
    
    bool Equals(Dice d){
        if(d.GetTop() == t_ && d.GetN() == n_ && d.GetW() == w_ && d.GetS() == s_ && d.GetE() == e_ && d.GetU() == u_) return true;
        return false;
    }
    
    void SetPrem(int t, int s, int e, int w, int n, int u){
        t_ = t; s_ = s; e_ = e; w_ = w; n_ = n; u_ = u;
    }
    
    int GetTop(){
        return t_;
    }
    int GetN(){
        return n_;
    }
    int GetW(){
        return w_;
    }
    int GetS(){
        return s_;
    }
    int GetE(){
        return e_;
    }
    int GetU(){
        return u_;
    }
};

void solve(Dice d[]){
    bool is_true=false;
    rep(i, 4){
        rep(j, 4){
            if(d[0].Equals(d[1])){
                is_true=true;
            }else{
                d[1].TurnR();
            }
        }
        if(is_true) break;
        else d[1].TurnN();
    }
    if(!is_true){
        rep(i, 4){
            rep(j, 4){
                if(d[0].Equals(d[1])){
                    is_true=true;
                }else{
                    d[1].TurnR();
                }
            }
            if(is_true) break;
            else d[1].TurnW();
        }
    }
    if(is_true) cout << "Yes" << endl;
    else cout << "No" << endl;
}

int main(int argc, const char * argv[]) {

    Dice d[2];
    rep(i, 2){
        int t, s, e, w, n, u;
        cin >> t >> s >> e >> w >> n >> u;
        d[i].SetPrem(t, s, e, w, n, u);
    }
    
    solve(d);
    
    return 0;
}
```

##[ITP1_11_D](https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/11/ITP1_11_D)
###サイコロ IV

```C++:C++
#include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<utility>
#include<iomanip>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cctype>

#define rep(i,n) for(int i=0; i<(n); ++i)
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9

using namespace std;
using ll =long long;
using P = pair<int,int>;

struct Dice{
private:
    int n_, w_, s_, e_, t_, u_;
    
public:
    Dice(){
    }
    Dice(int t, int s, int e, int w, int n, int u){
        t_ = t; s_ = s; e_ = e; w_ = w; n_ = n; u_ = u;
    }
    
    void TurnN(){
        int tmp = t_; t_ = s_; s_ = u_; u_ = n_; n_ = tmp;
    }
    
    void TurnW(){
        int tmp = t_; t_ = e_; e_ = u_; u_ = w_; w_ = tmp;
    }
    
    void TurnS(){
        int tmp = t_; t_ = n_; n_ = u_; u_ = s_; s_ = tmp;
    }
    
    void TurnE(){
        int tmp = t_; t_ = w_; w_ = u_; u_ = e_; e_ = tmp;
    }
    
    void TurnR(){
        int tmp = n_; n_ = w_; w_ = s_; s_ = e_; e_ = tmp;
    }
    
    bool Equals(Dice d){
        if(d.GetTop() == t_ && d.GetN() == n_ && d.GetW() == w_ && d.GetS() == s_ && d.GetE() == e_ && d.GetU() == u_) return true;
        return false;
    }
    
    void SetPrem(int t, int s, int e, int w, int n, int u){
        t_ = t; s_ = s; e_ = e; w_ = w; n_ = n; u_ = u;
    }
    
    int GetTop(){
        return t_;
    }
    int GetN(){
        return n_;
    }
    int GetW(){
        return w_;
    }
    int GetS(){
        return s_;
    }
    int GetE(){
        return e_;
    }
    int GetU(){
        return u_;
    }
};

void solve(vector<Dice> d){
    bool is_true=false;
    for(int k=0; k<d.size(); k++){
        for(int l=k+1; l<d.size(); l++){
            rep(i, 4){
                rep(j, 4){
                    if(d[k].Equals(d[l])){
                        is_true=true;
                    }else{
                        d[l].TurnR();
                    }
                }
                if(is_true) break;
                else d[l].TurnN();
            }
            if(!is_true){
                rep(i, 4){
                    rep(j, 4){
                        if(d[k].Equals(d[l])){
                            is_true=true;
                        }else{
                            d[l].TurnR();
                        }
                    }
                    if(is_true) break;
                    else d[l].TurnW();
                }
            }
            if(is_true) break;
        }
        if(is_true) break;
    }
    
    if(is_true) cout << "No" << endl;
    else cout << "Yes" << endl;
}

int main(int argc, const char * argv[]) {

    int n;
    cin >> n;
    vector<Dice> d(n);
    rep(i, n){
        int t, s, e, w, n, u;
        cin >> t >> s >> e >> w >> n >> u;
        d[i].SetPrem(t, s, e, w, n, u);
    }
    
    solve(d);
    
    return 0;
}
```