Problem Url: http://acm.timus.ru/problem.aspx?space=1&num=1015
This is a math related problem. First we'll need to determine the total possible scheme of dices, there're 30 schemes. To calculate this, I used the following means:
Totally 720=6! poses(of all schemes' dice in different poses) according to the problem. and each scheme can have 24 different poses. So the scheme count is 720/24 = 30
In the problem, we will need to cache all the detected dice pose and update all those poses of the same scheme(if they haven't been filled in lookup store yet). For the lookup store, so far I have got two possible approaches, one is using a large 5-dim array as hashtable, this will provide very quick lookup, but waste a lot of memory (since among 6^5 entries, only 720(at most) is used). Another approach is using map(or hashmap) which may save a lot of memory since only necessary entries will be used. Also, I'll use a large array to store the scheme id of all the input dice, so far I statically define a large global array. Here are the codes:
1. Use large 5-dim array as lookup table:
AC 0.015 335 KB
*/
#include < cstdio >
#include < iostream >
#define MAXNUM 100000
#define MAXKIND 30
using namespace std;
// global variables
int N = 0 ; // dice number
int inputs[MAXNUM]; // record the input dice's category
int startpos[MAXKIND + 1 ]; // record the start item of each category
int dicehash[ 6 ][ 6 ][ 6 ][ 6 ][ 6 ]; // dice category lookup table, dice[left][right][top][front][bottom](do not need the 6th attribute)
int g_l = 0 , g_r = 0 , g_t = 0 , g_f = 0 , g_bm = 0 , g_bk = 0 ;
void updatehash( int l, int r, int t, int f, int bm, int bk, int idx);
void main()
{
memset(dicehash, 0 , sizeof ( int ) * 6 * 6 * 6 * 6 * 6 );
int i = 0 ;
int curidx = 0 ;
scanf( " %d " , & N);
for (i = 0 ;i < N; ++ i)
{
scanf( " %d " , & g_l); scanf( " %d " , & g_r); scanf( " %d " , & g_t); scanf( " %d " , & g_f); scanf( " %d " , & g_bm); scanf( " %d " , & g_bk);
if ( ! dicehash[g_l - 1 ][g_r - 1 ][g_t - 1 ][g_f - 1 ][g_bm - 1 ])
{
updatehash(g_l,g_r,g_t,g_f,g_bm,g_bk, ++ curidx);
startpos[curidx] = i;
}
inputs[i] = dicehash[g_l - 1 ][g_r - 1 ][g_t - 1 ][g_f - 1 ][g_bm - 1 ];
}
int start = 0 ;
printf( " %d " , curidx);
for (i = 1 ;i <= curidx; ++ i)
{
start = startpos[i];
while (start < N)
{
if (inputs[start] == i) printf( " %d " ,(start + 1 ));
++ start;
}
printf( " " );
}
}
void updatehash( int l, int r, int t, int f, int bm, int bk, int idx)
{
int i = 0 ,j = 0 ,k = 0 ;
int temp = 0 ;
for (i = 0 ;i < 4 ; ++ i)
{
for (j = 0 ;j < 4 ; ++ j)
{
for (k = 0 ;k < 4 ; ++ k)
{
dicehash[l - 1 ][r - 1 ][t - 1 ][f - 1 ][bm - 1 ] = idx;
// rotate in direction 3 (left->top, top->right, right->bottom, bottom->left)
temp = l;
l = bm; bm = r; r = t; t = temp;
}
// rotate in direction 2 (front->top, top->back, back->bottom, bottom->front)
temp = f;
f = bm; bm = bk; bk = t; t = temp;
}
temp = l;
l = f; f = r; r = bk; bk = temp;
// rotate in direction 1 ( left->back, back->right, right->front, front->left)
}
}
2. Use map as lookup table
change to use map for dice scheme lookup
AC: 0.015 220 KB
*/
#include < cstdio >
#include < iostream >
#include < map >
#define MAXNUM 100000
#define MAXKIND 30
// use macro to define the ID calculation statement
#define GETID(left, right, top, front, bottom)
left * 6 + right * 36 + top * 216 + front * 1296 + bottom * 7776
using namespace std;
// global variables
int N = 0 ; // dice number
int inputs[MAXNUM]; // record the input dice's category
int startpos[MAXKIND + 1 ]; // record the start item of each category
map < int , int > dicemap; // use map instead of large hash lookup table
int g_l = 0 , g_r = 0 , g_t = 0 , g_f = 0 , g_bm = 0 , g_bk = 0 ;
void updatemap( int l, int r, int t, int f, int bm, int bk, int idx);
void main()
... {
dicemap.clear();
map<int,int>::const_iterator mapIter;
int i=0;
int curidx = 0;
scanf("%d", &N);
for(i=0;i<N;++i)
...{
//get left, right, top, front, bottom, back values from input
scanf("%d", &g_l); scanf("%d", &g_r); scanf("%d", &g_t); scanf("%d", &g_f); scanf("%d", &g_bm); scanf("%d", &g_bk);
mapIter = dicemap.find(GETID(g_l, g_r, g_t, g_f, g_bm));
if(mapIter == dicemap.end())
...{
updatemap(g_l,g_r,g_t,g_f,g_bm,g_bk,++curidx);
startpos[curidx] = i;
}
inputs[i] = dicemap[GETID(g_l, g_r, g_t, g_f, g_bm)];
}
int start = 0;
printf("%d ", curidx);
for(i=1;i<=curidx;++i)
...{
start = startpos[i];
while(start<N)
...{
if(inputs[start] == i) printf("%d ",(start+1));
++start;
}
printf(" ");
}
}
void updatemap( int l, int r, int t, int f, int bm, int bk, int idx)
... {
int i=0,j=0,k=0;
int temp =0;
for(i=0;i<4;++i)
...{
for(j=0;j<4;++j)
...{
for(k=0;k<4;++k)
...{
if(dicemap.find(GETID(l, r, t, f, bm)) == dicemap.end())
dicemap.insert(pair<int,int>(GETID(l, r, t, f, bm),idx));
//rotate in direction 3 (left->top, top->right, right->bottom, bottom->left)
temp = l;
l = bm; bm = r; r = t; t = temp;
}
//rotate in direction 2 (front->top, top->back, back->bottom, bottom->front)
temp = f;
f = bm; bm = bk; bk = t; t = temp;
}
temp = l;
l = f; f = r; r = bk; bk = temp;
//rotate in direction 1 ( left->back, back->right, right->front, front->left)
}
}