Rectangle

来源:互联网 发布:全能数据人生txt 编辑:程序博客网 时间:2024/06/05 06:20

Give you N rectangles.If you can pick exactly three pieces of those rectangles to form a larger rectangle?

input:

There are several testcases.

The first line is an integer T, indicating the number of testcases.

For each testcase:

The first line is a integer N and N is no more than 10.

The second line contains 2*N integers describing N rectangles.Each rectangle is described by 2 integers indicating as width and height.

All these integers in the second line are between [1,10000]

output

If you can do it, print Yes.Otherwise, print No instead.

sample input

2
4
1 1 1 1 1 2 2 2
4
1 1 2 2 10 10 20 20

sample output

Yes
No
tips:这题是我期中考试的一道题,考试的时候比较简单粗暴,直接循环,反正数据太水。。不过,还是也有更好的方法的
         首先,判断标准仅有两个:
1.三个矩阵的一边都相等
  2.有两个矩阵的一边相等,且存在不属于这两个矩阵的矩阵有一边等于这两个矩阵的另两个边之和
     分两步,读入数据的时候就可以判断条件1,如果成立,利用“短路”即可,条件2的判断也只需要O(n)
    {表达能力好差。。。}
#include <stdio.h>#include <string.h>int sum_data[10050];//每个边的个数int index_data[10050][3];//每个边对应的矩阵下标int data[20][2];//每个矩阵的数据int readdata(){    int M,i=0,flag=0;    scanf("%d",&M);    while (M--)    {        int a,b,tmp=0;        scanf("%d%d",&a,&b);        data[i][0]=a;        data[i][1]=b;        sum_data[a]++;        if (sum_data[a]==3) flag=1;   //之前用return。。。绝对不要这样!。。。不然数据读入会出问题。。。找了好久。。        while (index_data[a][tmp]!=0) tmp++;        index_data[a][tmp]=i;        if (a!=b)        {            sum_data[b]++;            if (sum_data[b]==3) flag=1;            tmp=0;            while (index_data[b][tmp]!=0) tmp++;            index_data[b][tmp]=i;        }        i++;    }    if (flag==1) return 1;    return 0;}int finddata(){    for (int n=1;n<=10000;n++)    {        if (sum_data[n]==2)        {            int index_a=index_data[n][0],index_b=index_data[n][1];            int  a,b,another;            if (data[index_a][0]!=n) a=data[index_a][0];            else a=data[index_a][1];            if (data[index_b][0]!=n) b=data[index_b][0];            else b=data[index_b][1];            another=a+b;            if (sum_data[another]!=0)            {                for (int i=0;i<sum_data[another];i++)                    if (index_data[another][i]!=index_a&&index_data[another][i]!=index_b) return 1;            }        }    }    return 0;}void cleardata(){    memset(sum_data,0,sizeof(sum_data));    memset(index_data,0,sizeof(index_data));    memset(data,0,sizeof(data));}int main(){    int N;    scanf("%d",&N);    while (N--)    {        cleardata();        if (readdata()||finddata()) printf("Yes\n");        else printf("No\n");    }    return 0;}
0 0
原创粉丝点击