2009年2月11日 星期三

Q555: Bridge Hands

/*
用gets(str)才會對
用scanf("%c%c", &a, &b);會Time limit exceeded
Run time: 0.060
*/
#include <stdio.h>
#define N 0
#define E 1
#define S 2
#define W 3

typedef struct {
int dir;
char suit;
char rank;
} Card;
Card card[52];
int arr[4][13];

void assign(Card p);
int index(char ch);
void print(int turn);
char reverse(int index);

int main()
{
char ch;
int i, j;
int aspect;

while (1)
{
scanf("%c\n", &ch);
if (ch == '#') return 0;

if (ch == 'N')
aspect = N;
else
if (ch == 'E')
aspect = E;
else
if (ch == 'S')
aspect = S;
else
aspect = W;

char str[53];

gets(str);


for ( i = 0, j = 0; i < 52; i+=2)
{
card[j].suit = str[i];
card[j].rank = str[i+1];
card[j].dir = (aspect+j+1)%4;
j++;
}
gets(str);
for ( i = 0; i < 52; i+=2)
{
card[j].suit = str[i];
card[j].rank = str[i+1];
card[j].dir = (aspect+j+1)%4;
j++;
}

for ( i = 0; i < 52; i++)
assign(card[i]);

printf("S:"); print(S); printf("\n");
printf("W:"); print(W); printf("\n");
printf("N:"); print(N); printf("\n");
printf("E:"); print(E); printf("\n");

}
return 0;
}

void assign(Card p)
{
int x = index(p.rank);
if (p.suit == 'C')
arr[0][x] = p.dir;
else
if (p.suit == 'D')
arr[1][x] = p.dir;
else
if (p.suit == 'S')
arr[2][x] = p.dir;
else
if (p.suit == 'H')
arr[3][x] = p.dir;
}

int index(char ch)
{
if (ch >= '2' && ch <= '9')
return ch-'0'-2;
if (ch == 'T')
return 8;
if (ch == 'J')
return 9;
if (ch == 'Q')
return 10;
if (ch == 'K')
return 11;
if (ch == 'A')
return 12;
}
void print(int turn)
{
int i, j;
for ( i = 0; i < 4; i++)
for ( j = 0; j < 13; j++)
if (arr[i][j] == turn)
{
if (i == 0)
printf(" C");
else
if (i == 1)
printf(" D");
else
if (i == 2)
printf(" S");
else
if (i == 3)
printf(" H");
printf("%c", reverse(j));
}
}

char reverse(int index)
{
if (index >= 0 && index <= 7) return index+'2';
if (index == 8) return 'T';
if (index == 9) return 'J';
if (index == 10) return 'Q';
if (index == 11) return 'K';
if (index == 12) return 'A';
}

沒有留言: