C - MaratonIME eats japanese food

VJ 补题 C - MaratonIME eats japanese food
After a long time eating japanese food, the members of MaratonIME are getting really good at it. Nowadays, a massive amount of japanese food is eaten by the group and it’s often difficult to find space for all the plates in the table, since two plates can touch but never overlap. The fact that there is so much food in each plate makes this even worse, because they can’t move the plates on the table until there is no more food in them.

For that reason, everytime a plate arrives, they need to try lots of different positions for that plate on the table. You need to help them by creating a program that will aid on that rampant gluttony. Your program starts off with an empty table and has to respond to various queries, each of one of two types:

A x y r - Place a circular plate centered at (x, y) with radius r.
R x y r - Remove the circular plate centered at (x, y) with radius r.
For each plate, its center position (x, y) and its radius are given in centimeters (the plate’s located x centimeters away from the left border and y centimeters away form the upper border). The table always has 10 meters both in width and height and a plate is allowed to hang off the table, as long as it’s center is on the table (or on its border).

For each query of type A, you must determine if it was possible to place the given plate on the given position (if that plate does not overlap any other, note that it is allowed for it to touch other plates) and place it if possible. For each query of type R, you must determine if it was possible to remove that plate on the given position from the table (if there is a plate with those coordinates and radius on that position) and remove it if possible.

Input
On the first line of the input an integer Q ≤ 5000 is given, the amount of queries that need to be answered.

On each one of the Q following lines the queries are described. Each of them is given by a character , the type of the query, and three integers x, y and r, the coordinates and radius of the plate, 0 ≤ x, y ≤ 1000 and 1 ≤ r ≤ 1000.

Output
For each query, print a line with “Ok” (without quotes) if it is possible to perform such query (add the plate on type A and remove the plate on type R) and “No” otherwise.

Example
Input
8
A 20 25 15
A 25 25 5
A 20 55 15
A 35 40 7
A 35 40 6
R 20 25 15
A 20 25 10
R 20 25 15
Output
Ok
No
Ok
No
Ok
Ok
Ok
No
原本我是用链表来做这个题的,不过好麻烦,虽然给出的例子都能通过,但是还是wrong, 没找到原因在哪里,有大佬指教吗
代码奉上:
#include<bits/stdc++.h>
#include

using namespace std;
struct node
{
struct node next;
int x;
int y;
int r;
};
int main()
{
int t;
char ch;
struct node head, tail, p, q, r;
head = (node
)malloc(sizeof(node));
head->next = NULL;
tail = head;
cin>>t;
while(t–)
{
int flag = 0;
int a, b, c;
cin>>ch;
if(ch == ‘A’)
{
cin>>a>>b>>c;
if(head->next == NULL)
{
cout<<“Ok”<<endl;
p = (node
)malloc(sizeof(node));
p->x=a;
p->y=b;
p->r=c;
p->next = NULL;
tail->next = p;
tail = tail->next;
}
else
{
q = head->next;
while(q!=NULL)
{
if((((a-q->x)
(a-q->x)+(b-q->y)
(b-q->y))<(c+q->r)
(c+q->r))||(aq->x&&bq->y&&c==q->r))
{
cout<<“No”<<endl;
flag = 1;
break;
}
q = q->next;
}
if(flag == 0)
{
cout<<“Ok”<<endl;
p = (node
)malloc(sizeof(node));
p->x=a;
p->y=b;
p->r=c;
p->next = NULL;
tail->next = p;
tail = tail->next;
}
}

    }

    else if(ch == 'R')
    {flag = 0;
        cin>>a>>b>>c;
            if(head->next==NULL)
                cout<<"No"<<endl;
            else
            {
                r = head;
                q = head->next;
                while(q!=NULL)
                {
                    if(q->x == a&&q->y==b&&q->r==c)
                    {
                        flag = 1;
                        r->next=q->next;
                        q=q->next;
                        cout<<"Ok"<<endl;
                        break;
                    }
                    else
                    {
                        r=r->next;
                        q=q->next;
                    }
                }
                if(flag == 0)
                    cout<<"No"<<endl;
            }
    }
}
return 0;

}
然后我又借鉴其他博主的方法,重新解了一遍,显然这种方法更加简便, 代码奉上:
#include<bits/stdc++.h>
#include
#define N 5005
using namespace std;
int vis[1005][1005];
int rr[1005][1005];
struct node
{
char c;
int x;
int y;
int r;
}a[N];
int main()
{
memset(vis,0,sizeof(vis));
memset(rr,0,sizeof(rr));
int t, j;
cin>>t;
for(int i = 1;i<=t;i++)
{
cin>>a[i].c>>a[i].x>>a[i].y>>a[i].r;
if(a[i].c==‘R’)
{
if(vis[a[i].x][a[i].y]!=0&&rr[a[i].x][a[i].y]a[i].r)
{
cout<<“Ok”<<endl;
vis[a[i].x][a[i].y]=0;
rr[a[i].x][a[i].y]=0;
}
else
cout<<“No”<<endl;
}
else if(a[i].c
’A’)
{
if(vis[a[i].x][a[i].y]1)
cout<<“No”<<endl;
else
{
for(j=1;j<=i-1;j++)
{
if(a[j].c
’A’&&vis[a[j].x][a[j].y]1)
{
if((a[j].r+a[i].r)(a[j].r+a[i].r)>((a[j].x-a[i].x)(a[j].x-a[i].x)+(a[j].y-a[i].y)*(a[j].y-a[i].y)))
{
cout<<“No”<<endl;
break;
}
}
}
if(j
i)
{
cout<<“Ok”<<endl;
vis[a[i].x][a[i].y]=1;
rr[a[i].x][a[i].y]=a[i].r; }
}
}
}
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值