C - Robot Breakout

n robots have escaped from your laboratory! You have to find them as soon as possible, because these robots are experimental, and their behavior is not tested yet, so they may be really dangerous!

Fortunately, even though your robots have escaped, you still have some control over them. First of all, you know the location of each robot: the world you live in can be modeled as an infinite coordinate plane, and the i-th robot is currently located at the point having coordinates (xi, yi). Furthermore, you may send exactly one command to all of the robots. The command should contain two integer numbers X and Y, and when each robot receives this command, it starts moving towards the point having coordinates (X, Y). The robot stops its movement in two cases:

either it reaches (X, Y);
or it cannot get any closer to (X, Y).
Normally, all robots should be able to get from any point of the coordinate plane to any other point. Each robot usually can perform four actions to move. Let’s denote the current coordinates of the robot as (xc, yc). Then the movement system allows it to move to any of the four adjacent points:

the first action allows it to move from (xc, yc) to (xc−1, yc);
the second action allows it to move from (xc, yc) to (xc, yc+1);
the third action allows it to move from (xc, yc) to (xc+1, yc);
the fourth action allows it to move from (xc, yc) to (xc, yc−1).
Unfortunately, it seems that some movement systems of some robots are malfunctioning. For each robot you know which actions it can perform, and which it cannot perform.

You want to send a command so all robots gather at the same point. To do so, you have to choose a pair of integer numbers X and Y so that each robot can reach the point (X, Y). Is it possible to find such a point?

Input
The first line contains one integer q (1≤q≤105) — the number of queries.

Then q queries follow. Each query begins with one line containing one integer n (1≤n≤105) — the number of robots in the query. Then n lines follow, the i-th of these lines describes the i-th robot in the current query: it contains six integer numbers xi, yi, fi,1, fi,2, fi,3 and fi,4 (−105≤xi,yi≤105, 0≤fi,j≤1). The first two numbers describe the initial location of the i-th robot, and the following four numbers describe which actions the i-th robot can use to move (fi,j=1 if the i-th robot can use the j-th action, and fi,j=0 if it cannot use the j-th action).

It is guaranteed that the total number of robots over all queries does not exceed 105.

Output
You should answer each query independently, in the order these queries appear in the input.

To answer a query, you should do one of the following:

if it is impossible to find a point that is reachable by all n robots, print one number 0 on a separate line;
if it is possible to find a point that is reachable by all n robots, print three space-separated integers on the same line: 1 X Y, where X and Y are the coordinates of the point reachable by all n robots. Both X and Y should not exceed 105 by absolute value; it is guaranteed that if there exists at least one point reachable by all robots, then at least one of such points has both coordinates not exceeding 105 by absolute value.
Example
Input
4
2
-1 -2 0 0 0 0
-1 -2 0 0 0 0
3
1 5 1 1 1 1
2 5 0 1 0 1
3 5 1 0 0 0
2
1337 1337 0 1 1 1
1336 1337 1 1 0 1
1
3 5 1 1 1 1
Output
1 -1 -2
1 2 5
0
1 -100000 -100000
此题含义为,先给出n个点的坐标,再给出该点可以移动的方向(前,后,左,右)判断这几个点可以到达的公共点
解题思路:判断每个点可以到达的x轴最大点或最小点,y轴最大点或最小点,然后进行判断,找出x轴最小距离的最大值和最大距离的最小值,y轴最小距离的最大值和最大距离的最小值,判断当不满足条件时输出0,其他输出x最大,y最大

#include<iostream>
using namespace std;
#define INF 1e5
int main()
{
	int q;
	cin>>q;
	while(q--)
	{
		int n;
		int x,y,f1,f2,f3,f4;
		int maxx=INF,maxy=INF,minx=-INF,miny=-INF;
		cin>>n;
		for(int i=0;i<n;i++)
		{
			cin>>x>>y>>f1>>f2>>f3>>f4;
			if(f1!=1)
				minx=max(minx,x);
			if(f2!=1)
				maxy=min(maxy,y);
			if(f3!=1)
				maxx=min(maxx,x);
			if(f4!=1)
				miny=max(miny,y);
		}
		if(maxy<miny||maxx<minx)
			cout<<"0"<<endl;
		else
		{
			cout<<"1"<<" "<<maxx<<" "<<maxy<<endl;
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值