Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 11631

C++ Finding if two cubes represented as a massive are the same

$
0
0

So i have a task,is given a file,on the first line is a number n and on the next n lines are represented cubes with 6 digits,each digit is representing a cube face, if it is noted with 1 this face is red, if 0 then white, maximum 4 faces can be colored in red.

Digits represent the faces in that order : bottom,front,left,back,right,top side;

And so i need to find the number of unique cubes and the greatest number of similarities.

Consider that two cubes look the same and are of the same type if there is a way to arrange each of them so that from whatever side we look at them their faces are colored the same.

I also attached the example and the result i should get, first line of answer is number of unique cubes, second line the greatest number of similarities.

Input file:
6
1 0 1 0 0 0
1 0 1 0 1 0
1 1 0 0 0 1
0 0 1 0 1 0
0 0 0 0 0 1
0 1 1 0 0 0

Output file:
4
2

I realised the programm fully except the function that returns whether the cubes are identical or not,i find the 3 obvious cases:1)If the massives are equal2)If the reversed massives are equal3)If the summ of elements are not equal(because red is 1,and with summ we can check the number of red faces)

#include <iostream>#include <fstream>#include <vector>#include <algorithm>using namespace std;ifstream fin("cubes.in");ofstream fout("cubes.out");bool asem(vector<int>& a, vector<int>& b) {    //If the massives are equal    if (a == b)        return true;    //If the reversed massives are equal    reverse(b.begin(), b.end());    if (a == b)        return true;    reverse(b.begin(), b.end());    //If the summ of elements are not equal    int sum_a = 0, sum_b = 0;    for (int i = 0; i < a.size(); ++i) {        sum_a += a.at(i);        sum_b += b.at(i);    }    if (sum_a != sum_b)        return false;    return false;}int main(){    int n, x;    fin >> n;    vector<int> b;    vector<vector<int>> cubes;    for (int i = 0; i < n; ++i) {        b.clear();        for (int j = 0; j < 6; ++j) {            fin >> x;            b.push_back(x);        }        cubes.push_back(b);    }    int pos = 0, uniq = 0;    vector<int> count;//vector that stores the number of repetitions    while (cubes.size() != 0) {        count.push_back(0);        uniq++;        for (int i = pos + 1; i < cubes.size(); ++i) {            if (asem(cubes.at(pos), cubes.at(i))) {                count.at(pos)++;                cubes.erase(cubes.begin() + i);                i--;            }        }        cubes.erase(cubes.begin());    }    sort(count.begin(), count.end());    fout << uniq << endl << count.at(count.size() - 1);    return 0;}

Viewing all articles
Browse latest Browse all 11631

Trending Articles