POJ 1244 Slots of Fun

POJ 1244 Slots of Fun

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

样例图片

In order to prevent too many payoffs, the electronics in the machine ensures that no more than 3 of any letter will appear in any display configuration.
IBM is manufacturing several models of this machine, with varying number of rows in the display, and they are having trouble writing code to identify winning configurations. Your job is to write that code.

Input

Input will consist of multiple problem instances. Each instance will start with an integer n indicating the number of rows in the display. The next line will contain n(n + 1)/2 letters of the alphabet (all lowercase) which are to be stored in the display row-wise, starting from the top. For example, the display above would be specified as
4
abccddadca
The value of n will be between 1 and 12, inclusive. A line with a single 0 will terminate input.

Output   

For each problem instance, output all letters which form equilateral triangles on a single line, in alphabetical order. If no such letters exist, output "LOOOOOOOOSER!".

Sample Input

4
abccddadca
6
azdefccrhijrrmznzocpq
2
abc
0

Sample Output

ac
crz
LOOOOOOOOSER!

题目的大意就是要判断输入中字母相同且构成等边三角形三点的字母有哪些,
等边三角形等边三角形,就是三边相等就行了,数据也小,直接暴力就行,最主要的问题是求两点间的距离,这里可以把整个三角形就是一个60°的斜坐标系,推一下斜坐标系两点距离就可以了


坐标系.png

\ |\vec{ab}|=|\vec{a}-\vec{b}|=\sqrt{( (x_1-x2_1)\vec{e_1}+(y_1-y2_1)\vec{e_2})^2}
\quad\quad =\sqrt{(x_1-x_2)^2+(y_1-y_2)^2-(x_1-x_2)(y_1-y_2)}

然后到这里就把ab的距离求出来了,直接暴力判断三点距离相等就行了

#include<iostream>
#include<cstring>
using namespace std;
char record[13][13];
int zmnum[26];
int zm[26][4][2];
int result[26];
int getdis(int x1,int x2,int y1,int y2){
    return (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)-(x2-x1)*(y2-y1);
}
int main(){
    int n;
    while(cin>>n&&n){
        memset(result,0,sizeof(result));
        memset(zm,0,sizeof(zm));
        memset(zmnum,0,sizeof(zmnum));
        for(int i=0;i<n;i++){
            for(int j=0;j<=i;j++){
                cin>>record[i][j];
            }
        }
        if(n==1){
            cout<<"LOOOOOOOOSER!"<<endl;
            continue;
        }
        double layer=((n+1)*n)/2;
         for(int i=0;i<n;i++){
            for(int j=0;j<=i;j++){
                char c=record[i][j];
                zm[c-'a'][zmnum[c-'a']][0]=i;
                zm[c-'a'][zmnum[c-'a']++][1]=j;
            }
        }
        bool loser=true;
        for(int i=0;i<26;i++){
            char c=i+'a';
            if(zmnum[i]<3)continue;
            int l1=getdis(zm[i][0][0],zm[i][1][0],zm[i][0][1],zm[i][1][1]);
            int l2=getdis(zm[i][1][0],zm[i][2][0],zm[i][1][1],zm[i][2][1]);
            int l3=getdis(zm[i][2][0],zm[i][0][0],zm[i][2][1],zm[i][0][1]);
            if(l1==l2&&l2==l3)loser=false,cout<<c;
        }
        if(loser)cout<<"LOOOOOOOOSER!";
        cout<<endl;
    }
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,140评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,420评论 0 23
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    网事_79a3阅读 14,242评论 3 20
  • 这两天,我们一群人来湖南考心理咨询师,昨天上午刚考试结束。我们最初8个人一起的,现在只剩下我们三个人在长沙玩了。 ...
    爱笑的女孩运气好阅读 1,618评论 0 1
  • 儿子十岁了,前天刚过的生日,他很高兴又激动,导致头天晚上晚睡着一个多小时,为什么会这么激动呢,我是没有这样的体验,...
    屰月阅读 2,750评论 1 4