ZOJ 1504(POJ 1244)  Slots of Fun…

来源:互联网 发布:yii2.0框架源码下载 编辑:程序博客网 时间:2024/05/21 09:06
Slots of Fun

Time Limit: 2 Seconds    Memory Limit: 65536 KB

The International Betting Machine company has just issued a newtype of slot machine. The machine display consists of a set ofidentical circles placed in a triangular shape. An example withfour rows is shown below. When the player pulls the lever, themachine places a random letter in the center of each circle. Themachine pays off whenever any set of identical letters form thevertices of an equilateral triangle. In the example below, theletters 'a' and 'c' satisfy this condition.

ZOJ <wbr>1504(POJ <wbr>1244) <wbr> <wbr>Slots <wbr>of <wbr>Fun <wbr> <wbr>(鈥︹) <wbr>狗狗四十题

In order to prevent too many payoffs, the electronics in themachine ensures that no more than 3 of any letter will appear inany display configuration.

IBM is manufacturing several models of this machine, with varyingnumber of rows in the display, and they are having trouble writingcode to identify winning configurations. Your job is to write thatcode.


Input

Input will consist of multiple problem instances. Each instancewill start with an integer n indicating the number of rows in thedisplay. The next line will contain n(n + 1)/2 letters of thealphabet (all lowercase) which are to be stored in the displayrow-wise, starting from the top. For example, the display abovewould be specified as

4
abccddadca

The value of n will be between 1 and 12, inclusive. A line with asingle 0 will terminate input.


Output

For each problem instance, output all letters which formequilateral triangles on a single line, in alphabetical order. Ifno such letters exist, output "LOOOOOOOOSER!".


Sample Input

4
abccddadca
6
azdefccrhijrrmznzocpq
2
abc
0


Sample Output

ac
crz
LOOOOOOOOSER!


Source: East Central North America 2002

 

纠结了,这题我也说不清用了啥算法……

 

题意,给一个正三角形,一共有n行,第i行有i个字母(小写的哦),其中的三个元素有可能构成一个正三角形,并且这三个点的字母相同,求这样的字母,按字典序排列……

 

最多12行,不解释,暴力没商量,这里求两个点的距离的时候用到了一个小知识,用三维坐标记录每个点的位置,这样两个点的距离就是三维之间查最大的那个的值(max(map[a].x-map[b].x,map[a].y-map[b].y,map[a].z-map[b].z))

 

代码:

C语言: 高亮代码由发芽网提供
#include<stdio.h>
#include<math.h>
#include<stdlib.h>

structnode{
    int x,y,z;
    char c;
}map[80];
intcmp(const void *a,const void *b)
{
    return *(int *)a- *(int *)b;
}
intmatch(int a,int b)
{
    if(map[a].c!=map[b].c)return 0;
    return 1;
}
intdistance(int a,int b)
{
    int c,d,max;
    max=abs(map[a].x-map[b].x);
    c=abs(map[a].y-map[b].y);
    if(max<c)max=c;
    d=abs(map[a].z-map[b].z);
    if(max<d)max=d;
    return max;
}
intmain()
{
    int n,i,j,k,num,q,a[80];
    int dis[80][80];
    char str[80];
    while(scanf("%d",&n),n)
    {
       scanf("%s",str);
       k=0;
       for(i=1;i<=n;i++)
           for(j=1;j<=i;j++)
           {
               map[k].x=i;
               map[k].y=i+1-j;
               map[k].z=j;
               map[k].c=str[k];
               k++;
           }
           num=k;
           for(i=0;i<num;i++)
               for(j=0;j<num;j++)
                   dis[i][j]=distance(i,j);
               q=0;
               for(i=0;i<=num;i++)
                   for(j=i+1;j<num;j++)
                       if(match(i,j))
                       {
                           for(k=j+1;k<num;k++)
                               if(match(i,k)&&match(j,k))
                                   if(dis[i][j]==dis[i][k]&&dis[i][k]==dis[j][k])
                                       a[q++]=map[i].c;
                       }
                       if(q==0)
                           puts("LOOOOOOOOSER!");
                       else
                       {
                           qsort(a,q,sizeof(a[0]),cmp);
                           for(i=0;i<q;i++)
                               printf("%c",a[i]);
                           printf("\n");
                       }
    }
    return 0;
}
原创粉丝点击