#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _T_ 0 /* 天:Top */
#define _B_ 1 /* 地:Bottom */
#define _E_ 2 /* 東:East */
#define _W_ 3 /* 西:West */
#define _S_ 4 /* 南:South */
#define _N_ 5 /* 北:North */
// サイコロの値の保持
typedef struct {
long x,y,z;
int topside;
int d[6];
} die_t;
// 1行入力
int input(int *n, int *tn, int *fn)
{
// 入力が 0 なら 0 を返す
// 入力が 1文字 なら 1 を返す
// 入力が 3文字 なら 2 を返す
char s[4];
char s2[4];
int x,y;
{
if(0 == x)
{
return 0;
}
else
{
*n = x;
return 1;
}
}
y = s[2] - '0';
*tn = x;
*fn = y;
return 2;
}
// 指定座標にサイコロが存在するか確認する
int exist(long x, long y, long z, int n, die_t *dice)
{
int i;
for(i=0;i<n;i++)
{
if(
(dice[i].x == x) &&
(dice[i].y == y) &&
(dice[i].z == z))
{
return 1;
}
}
return 0;
}
int roll(die_t *dice, int i, int n)
{
int is_roll = 0;
int j,k,e,e2;
long x,y,z,stop;
int *d; // サイコロの目、d = {天,地,東,西,南,北} ※東西南北の北から見ている想定
int _e[6]; // サイコロの目の大きい順に方向をソート
int _x[] = {0,0,1,-1,0,0};
int _y[] = {0,0,0,0,1,-1};
x = dice[i].x;
y = dice[i].y;
z = dice[i].z;
d = dice[i].d;
stop = z; // Z=0では転がることができない!
// 転がす
while(stop){
// 方角ソート
for(j=0;j<6;j++)
{
for(k=0;k<6;k++)
{
if(d[k] == j+1)
{
_e[j] = k;
}
}
}
// 4~6の方向へ転がれるなら転がる。
for(j=5;j>2;j--)
{
e = _e[j]; // 面の方向(0天,1地,2東,3西,4南,5北)
if(e>1) // 3~6の面が天・地でない。
{
if(!exist(x+_x[e], y+_y[e], z-1, i, dice)) // 4~6の面がある方向にサイコロが無く、転がることができる場合。
{
// 転がる。座標処理
x += _x[e];
y += _y[e];
z--;
// 転がる。出目処理
e2 = (e%2==0)?e+1:e-1; // eの反対面
k = d[e];
d[e] = d[_T_];
d[_T_] = d[e2];
d[e2] = d[_B_];
d[_B_] = k;
// 転がり探し、止め。
j = 0;
// 回転フラグ
is_roll = 1;
}
}
}
stop = (0==j)? z:0; // 終了判定
}
dice[i].topside = d[_T_];
dice[i].x = x;
dice[i].y = y;
dice[i].z = z;
return is_roll;
}
// 上面と前面のサイコロの目から、サイコロのデータを登録する。
void setDice(die_t *die, int ti, int fi)
{
int wi;
int w[6][6]={ // 西方の目を天と北から得る。WEST = w[TOP][NORTH]
{0,3,5,2,4,0},
{4,0,1,6,0,3},
{2,6,0,0,1,5},
{5,1,0,0,6,2},
{3,0,6,1,0,4},
{0,6,2,5,3,0}
};
// サイコロの目の設定
wi = w[ti-1][fi-1];
die->d[_T_] = ti; die->d[_B_] = 7-ti;
die->d[_N_] = fi; die->d[_S_] = 7-fi;
die->d[_W_] = wi; die->d[_E_] = 7-wi;
}
// 主となる処理
char* main_proc(int n, int *t, int *f)
{
int i,j;
int ti; // 現在処理の対象としているサイコロにおけるtop,west,front(north)の目
long x,y,z; // 座標
int loop; // 繰り返しroll処理が必要か。
// サイコロのデータ。
die_t *dice;
// 最終結果の格納変数
char* s_res;
int result[6] = {0,0,0,0,0,0};
// サイコロのデータの領域割当。
dice
= (die_t
*)calloc(n
,sizeof(die_t
)); // 落として転がす処理 //
for(i=0;i<n;i++)
{
setDice(&dice[i], t[i], f[i]);
// 落とす。
x = 0; y = 0; z = 0;
while(exist(x,y,z,i,dice)) z++;
dice[i].x = x;
dice[i].y = y;
dice[i].z = z;
dice[i].topside = t[i];
// 転がす。
loop = roll(dice, i, i);
while(loop)
{
loop = 0;
for(j=0;j<i+1;j++) loop = (roll(dice,j,i+1))?1:loop;
}
}
// 表示 //
for(i=0;i<n;i++)
{
x = dice[i].x;
y = dice[i].y;
z = dice[i].z;
ti = dice[i].topside;
if(!exist(x,y,z+1,n,dice)) result[ti-1]++;
}
// 文字列化
s_res
= (char*)calloc(255,sizeof(char)); sprintf(s_res
,"%d %d %d %d %d %d\n",result
[0],result
[1],result
[2],result
[3],result
[4],result
[5]); return s_res;
}
int main(void)
{
int i;
int n, *t, *f;
int nn, tn, fn;
int ret;
char *results;
char *rtmp;
int results_len;
results
= (char*)calloc(1,sizeof(char));
// 入力
ret = input(&nn, &tn, &fn);
while(ret==1)
{
n = nn; // 入力されるサイコロの個数
// 入力データの領域割当
t
= (int *)calloc(n
,sizeof(int)); f
= (int *)calloc(n
,sizeof(int)); // サイコロデータ入力
for(i=0;i<n;i++)
{
if(2 != input(&nn, &tn, &fn)){
return 1;
}
t[i] = tn;
f[i] = fn;
}
// 主処理
rtmp = main_proc(n, t, f);
// 回答文字列連結
results_len
= strlen(results
); results
= (char*)realloc(results
, sizeof(char)*(results_len
+strlen(rtmp
)+1)); strcpy(results
+results_len
, rtmp
); // 入力データ破棄
// 入力
ret = input(&nn, &tn, &fn);
}
//最終出力
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKI2RlZmluZSBfVF8gMCAvKiDlpKnvvJpUb3AgKi8KI2RlZmluZSBfQl8gMSAvKiDlnLDvvJpCb3R0b20gKi8KI2RlZmluZSBfRV8gMiAvKiDmnbHvvJpFYXN0ICovCiNkZWZpbmUgX1dfIDMgLyog6KW/77yaV2VzdCAqLwojZGVmaW5lIF9TXyA0IC8qIOWNl++8mlNvdXRoICovCiNkZWZpbmUgX05fIDUgLyog5YyX77yaTm9ydGggKi8KCi8vIOOCteOCpOOCs+ODreOBruWApOOBruS/neaMgQp0eXBlZGVmIHN0cnVjdCB7CiAgICBsb25nIHgseSx6OwogICAgaW50IHRvcHNpZGU7CiAgICBpbnQgZFs2XTsKfSBkaWVfdDsKCi8vIO+8keihjOWFpeWKmwppbnQgaW5wdXQoaW50ICpuLCBpbnQgKnRuLCBpbnQgKmZuKQp7CiAgICAvLyDlhaXlipvjgYwgMCDjgarjgokgMCDjgpLov5TjgZkKICAgIC8vIOWFpeWKm+OBjCAx5paH5a2XIOOBquOCiSAxIOOCkui/lOOBmQogICAgLy8g5YWl5Yqb44GMIDPmloflrZcg44Gq44KJIDIg44KS6L+U44GZCiAgICBjaGFyIHNbNF07CiAgICBjaGFyIHMyWzRdOwogICAgaW50IHgseTsKICAgIGdldHMocyk7CiAgICBzc2NhbmYocywiJWQiLCZ4KTsKICAgIHNwcmludGYoczIsIiVkIix4KTsKICAgIGlmKHN0cmxlbihzMikgPT0gc3RybGVuKHMpKQogICAgewogICAgICAgIGlmKDAgPT0geCkKICAgICAgICB7CiAgICAgICAgICAgIHJldHVybiAwOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgICAgICAqbiA9IHg7CiAgICAgICAgICAgIHJldHVybiAxOwogICAgICAgIH0KICAgIH0KICAgIHkgPSBzWzJdIC0gJzAnOwogICAgKnRuID0geDsKICAgICpmbiA9IHk7CiAgICByZXR1cm4gMjsKfQoKLy8g5oyH5a6a5bqn5qiZ44Gr44K144Kk44Kz44Ot44GM5a2Y5Zyo44GZ44KL44GL56K66KqN44GZ44KLCmludCBleGlzdChsb25nIHgsIGxvbmcgeSwgbG9uZyB6LCBpbnQgbiwgZGllX3QgKmRpY2UpCnsKICAgIGludCBpOwogICAgZm9yKGk9MDtpPG47aSsrKQogICAgewogICAgICAgIGlmKAogICAgICAgICAgICAoZGljZVtpXS54ID09IHgpICYmIAogICAgICAgICAgICAoZGljZVtpXS55ID09IHkpICYmIAogICAgICAgICAgICAoZGljZVtpXS56ID09IHopKQogICAgICAgIHsKICAgICAgICAgICAgcmV0dXJuIDE7CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIDA7Cn0KCgppbnQgcm9sbChkaWVfdCAqZGljZSwgaW50IGksIGludCBuKQp7CiAgICBpbnQgaXNfcm9sbCA9IDA7CiAgICBpbnQgaixrLGUsZTI7CiAgICBsb25nIHgseSx6LHN0b3A7CiAgICBpbnQgKmQ7IC8vIOOCteOCpOOCs+ODreOBruebruOAgWQgPSB75aSpLOWcsCzmnbEs6KW/LOWNlyzljJd9IOKAu+adseilv+WNl+WMl+OBruWMl+OBi+OCieimi+OBpuOBhOOCi+aDs+WumgogICAgaW50IF9lWzZdOyAvLyDjgrXjgqTjgrPjg63jga7nm67jga7lpKfjgY3jgYTpoIbjgavmlrnlkJHjgpLjgr3jg7zjg4gKICAgIGludCBfeFtdID0gezAsMCwxLC0xLDAsMH07CiAgICBpbnQgX3lbXSA9IHswLDAsMCwwLDEsLTF9OwoKICAgIHggPSBkaWNlW2ldLng7CiAgICB5ID0gZGljZVtpXS55OwogICAgeiA9IGRpY2VbaV0uejsKICAgIGQgPSBkaWNlW2ldLmQ7CgogICAgc3RvcCA9IHo7IC8vIFo9MOOBp+OBr+i7ouOBjOOCi+OBk+OBqOOBjOOBp+OBjeOBquOBhO+8gQogICAgLy8g6Lui44GM44GZCiAgICB3aGlsZShzdG9wKXsKICAgICAgICAvLyDmlrnop5Ljgr3jg7zjg4gKICAgICAgICBmb3Ioaj0wO2o8NjtqKyspCiAgICAgICAgewogICAgICAgICAgICBmb3Ioaz0wO2s8NjtrKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlmKGRba10gPT0gaisxKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIF9lW2pdID0gazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICAvLyA0772eNuOBruaWueWQkeOBuOi7ouOBjOOCjOOCi+OBquOCiei7ouOBjOOCi+OAggogICAgICAgIGZvcihqPTU7aj4yO2otLSkKICAgICAgICB7CiAgICAgICAgICAgIGUgPSBfZVtqXTsgLy8g6Z2i44Gu5pa55ZCR77yIMOWkqSwx5ZywLDLmnbEsM+ilvyw05Y2XLDXljJfvvIkKICAgICAgICAgICAgaWYoZT4xKSAvLyAz772eNuOBrumdouOBjOWkqeODu+WcsOOBp+OBquOBhOOAggogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpZighZXhpc3QoeCtfeFtlXSwgeStfeVtlXSwgei0xLCBpLCBkaWNlKSkgLy8gNO+9njbjga7pnaLjgYzjgYLjgovmlrnlkJHjgavjgrXjgqTjgrPjg63jgYznhKHjgY/jgIHou6LjgYzjgovjgZPjgajjgYzjgafjgY3jgovloLTlkIjjgIIKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAvLyDou6LjgYzjgovjgILluqfmqJnlh6bnkIYKICAgICAgICAgICAgICAgICAgICB4ICs9IF94W2VdOwogICAgICAgICAgICAgICAgICAgIHkgKz0gX3lbZV07CiAgICAgICAgICAgICAgICAgICAgei0tOwogICAgICAgICAgICAgICAgICAgIC8vIOi7ouOBjOOCi+OAguWHuuebruWHpueQhgogICAgICAgICAgICAgICAgICAgIGUyID0gKGUlMj09MCk/ZSsxOmUtMTsgLy8gZeOBruWPjeWvvumdogogICAgICAgICAgICAgICAgICAgIGsgPSBkW2VdOwogICAgICAgICAgICAgICAgICAgIGRbZV0gPSBkW19UX107CiAgICAgICAgICAgICAgICAgICAgZFtfVF9dID0gZFtlMl07CiAgICAgICAgICAgICAgICAgICAgZFtlMl0gPSBkW19CX107CiAgICAgICAgICAgICAgICAgICAgZFtfQl9dID0gazsKICAgICAgICAgICAgICAgICAgICAvLyDou6LjgYzjgormjqLjgZfjgIHmraLjgoHjgIIKICAgICAgICAgICAgICAgICAgICBqID0gMDsKICAgICAgICAgICAgICAgICAgICAvLyDlm57ou6Ljg5Xjg6njgrAKICAgICAgICAgICAgICAgICAgICBpc19yb2xsID0gMTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBzdG9wID0gKDA9PWopPyB6OjA7IC8vIOe1guS6huWIpOWumgogICAgfQogICAgZGljZVtpXS50b3BzaWRlID0gZFtfVF9dOwogICAgZGljZVtpXS54ID0geDsKICAgIGRpY2VbaV0ueSA9IHk7CiAgICBkaWNlW2ldLnogPSB6OwogICAgcmV0dXJuIGlzX3JvbGw7Cn0KCi8vIOS4iumdouOBqOWJjemdouOBruOCteOCpOOCs+ODreOBruebruOBi+OCieOAgeOCteOCpOOCs+ODreOBruODh+ODvOOCv+OCkueZu+mMsuOBmeOCi+OAggp2b2lkIHNldERpY2UoZGllX3QgKmRpZSwgaW50IHRpLCBpbnQgZmkpCnsKICAgIGludCB3aTsKICAgIGludCB3WzZdWzZdPXsgLy8g6KW/5pa544Gu55uu44KS5aSp44Go5YyX44GL44KJ5b6X44KL44CCV0VTVCA9IHdbVE9QXVtOT1JUSF0KICAgICAgICB7MCwzLDUsMiw0LDB9LAogICAgICAgIHs0LDAsMSw2LDAsM30sCiAgICAgICAgezIsNiwwLDAsMSw1fSwKICAgICAgICB7NSwxLDAsMCw2LDJ9LAogICAgICAgIHszLDAsNiwxLDAsNH0sCiAgICAgICAgezAsNiwyLDUsMywwfQogICAgfTsKICAgIC8vIOOCteOCpOOCs+ODreOBruebruOBruioreWumgogICAgd2kgPSB3W3RpLTFdW2ZpLTFdOwogICAgZGllLT5kW19UX10gPSB0aTsgZGllLT5kW19CX10gPSA3LXRpOwogICAgZGllLT5kW19OX10gPSBmaTsgZGllLT5kW19TX10gPSA3LWZpOwogICAgZGllLT5kW19XX10gPSB3aTsgZGllLT5kW19FX10gPSA3LXdpOwp9CgovLyDkuLvjgajjgarjgovlh6bnkIYKY2hhciogbWFpbl9wcm9jKGludCBuLCBpbnQgKnQsIGludCAqZikKewogICAgaW50IGksajsKICAgIGludCB0aTsgLy8g54++5Zyo5Yem55CG44Gu5a++6LGh44Go44GX44Gm44GE44KL44K144Kk44Kz44Ot44Gr44GK44GR44KLdG9wLHdlc3QsZnJvbnQobm9ydGgp44Gu55uuCiAgICBsb25nIHgseSx6OyAvLyDluqfmqJkKICAgIGludCBsb29wOyAvLyDnubDjgorov5TjgZdyb2xs5Yem55CG44GM5b+F6KaB44GL44CCCiAgICAvLyDjgrXjgqTjgrPjg63jga7jg4fjg7zjgr/jgIIKICAgIGRpZV90ICpkaWNlOwogICAgLy8g5pyA57WC57WQ5p6c44Gu5qC857SN5aSJ5pWwCiAgICBjaGFyKiBzX3JlczsKICAgIGludCByZXN1bHRbNl0gPSB7MCwwLDAsMCwwLDB9OwogICAgCiAgICAvLyDjgrXjgqTjgrPjg63jga7jg4fjg7zjgr/jga7poJjln5/libLlvZPjgIIKICAgIGRpY2UgPSAoZGllX3QgKiljYWxsb2MobiAsc2l6ZW9mKGRpZV90KSk7CiAgICAvLyDokL3jgajjgZfjgabou6LjgYzjgZnlh6bnkIYgLy8KICAgIGZvcihpPTA7aTxuO2krKykKICAgIHsKICAgICAgICBzZXREaWNlKCZkaWNlW2ldLCB0W2ldLCBmW2ldKTsKCiAgICAgICAgLy8g6JC944Go44GZ44CCCiAgICAgICAgeCA9IDA7IHkgPSAwOyB6ID0gMDsKICAgICAgICB3aGlsZShleGlzdCh4LHkseixpLGRpY2UpKSB6Kys7CiAgICAgICAgZGljZVtpXS54ID0geDsKICAgICAgICBkaWNlW2ldLnkgPSB5OwogICAgICAgIGRpY2VbaV0ueiA9IHo7CiAgICAgICAgZGljZVtpXS50b3BzaWRlID0gdFtpXTsKCiAgICAgICAgLy8g6Lui44GM44GZ44CCCiAgICAgICAgbG9vcCA9IHJvbGwoZGljZSwgaSwgaSk7CiAgICAgICAgd2hpbGUobG9vcCkKICAgICAgICB7CiAgICAgICAgICAgIGxvb3AgPSAwOwogICAgICAgICAgICBmb3Ioaj0wO2o8aSsxO2orKykgbG9vcCA9IChyb2xsKGRpY2UsaixpKzEpKT8xOmxvb3A7CiAgICAgICAgfQogICAgfQogICAgLy8g6KGo56S6IC8vCiAgICBmb3IoaT0wO2k8bjtpKyspCiAgICB7CiAgICAgICAgeCA9IGRpY2VbaV0ueDsKICAgICAgICB5ID0gZGljZVtpXS55OwogICAgICAgIHogPSBkaWNlW2ldLno7CiAgICAgICAgdGkgPSBkaWNlW2ldLnRvcHNpZGU7CiAgICAgICAgaWYoIWV4aXN0KHgseSx6KzEsbixkaWNlKSkgcmVzdWx0W3RpLTFdKys7CiAgICB9CiAgICAvLyDmloflrZfliJfljJYKICAgIHNfcmVzID0gKGNoYXIqKWNhbGxvYygyNTUsc2l6ZW9mKGNoYXIpKTsKICAgIHNwcmludGYoc19yZXMsIiVkICVkICVkICVkICVkICVkXG4iLHJlc3VsdFswXSxyZXN1bHRbMV0scmVzdWx0WzJdLHJlc3VsdFszXSxyZXN1bHRbNF0scmVzdWx0WzVdKTsKICAgIHNfcmVzID0gKGNoYXIqKXJlYWxsb2Moc19yZXMsc2l6ZW9mKGNoYXIpKihzdHJsZW4oc19yZXMpKzEpKTsKICAgIGZyZWUoZGljZSk7CiAgICByZXR1cm4gc19yZXM7Cn0KCmludCBtYWluKHZvaWQpCnsKICAgIGludCBpOwogICAgaW50IG4sICp0LCAqZjsKICAgIGludCBubiwgdG4sIGZuOwogICAgaW50IHJldDsKICAgIGNoYXIgKnJlc3VsdHM7CiAgICBjaGFyICpydG1wOwogICAgaW50IHJlc3VsdHNfbGVuOwogICAgCiAgICByZXN1bHRzID0gKGNoYXIqKWNhbGxvYygxLHNpemVvZihjaGFyKSk7CgogICAgLy8g5YWl5YqbCiAgICByZXQgPSBpbnB1dCgmbm4sICZ0biwgJmZuKTsKICAgIHdoaWxlKHJldD09MSkKICAgIHsKICAgICAgICBuID0gbm47IC8vIOWFpeWKm+OBleOCjOOCi+OCteOCpOOCs+ODreOBruWAi+aVsAogICAgICAgIC8vIOWFpeWKm+ODh+ODvOOCv+OBrumgmOWfn+WJsuW9kwogICAgICAgIHQgPSAoaW50ICopY2FsbG9jKG4gLHNpemVvZihpbnQpKTsKICAgICAgICBmID0gKGludCAqKWNhbGxvYyhuICxzaXplb2YoaW50KSk7CiAgICAgICAgLy8g44K144Kk44Kz44Ot44OH44O844K/5YWl5YqbCiAgICAgICAgZm9yKGk9MDtpPG47aSsrKQogICAgICAgIHsKICAgICAgICAgICAgaWYoMiAhPSBpbnB1dCgmbm4sICZ0biwgJmZuKSl7CiAgICAgICAgICAgICAgICByZXR1cm4gMTsKICAgICAgICAgICAgfQogICAgICAgICAgICB0W2ldID0gdG47CiAgICAgICAgICAgIGZbaV0gPSBmbjsKICAgICAgICB9CiAgICAgICAgLy8g5Li75Yem55CGCiAgICAgICAgcnRtcCA9IG1haW5fcHJvYyhuLCB0LCBmKTsKICAgICAgICAvLyDlm57nrZTmloflrZfliJfpgKPntZAKICAgICAgICByZXN1bHRzX2xlbiA9IHN0cmxlbihyZXN1bHRzKTsKICAgICAgICByZXN1bHRzID0gKGNoYXIqKXJlYWxsb2MocmVzdWx0cywgc2l6ZW9mKGNoYXIpKihyZXN1bHRzX2xlbitzdHJsZW4ocnRtcCkrMSkpOwogICAgICAgIHN0cmNweShyZXN1bHRzK3Jlc3VsdHNfbGVuLCBydG1wKTsKICAgICAgICBmcmVlKHJ0bXApOwogICAgICAgIC8vIOWFpeWKm+ODh+ODvOOCv+egtOajhAogICAgICAgIGZyZWUodCk7CiAgICAgICAgZnJlZShmKTsKICAgICAgICAvLyDlhaXlipsKICAgICAgICByZXQgPSBpbnB1dCgmbm4sICZ0biwgJmZuKTsKICAgIH0KICAgIC8v5pyA57WC5Ye65YqbCiAgICBwcmludGYoIiVzIiwgcmVzdWx0cyk7CiAgICBmcmVlKHJlc3VsdHMpOwogICAgcmV0dXJuIDA7Cn0K