Robot Breakout(CF-1196C)

Problem Description

nn 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 (xcxc, ycyc). 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 XX and YY 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 qq queries follow. Each query begins with one line containing one integer nn (1≤n≤1051≤n≤105) — the number of robots in the query. Then nn lines follow, the ii-th of these lines describes the ii-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 ii-th robot, and the following four numbers describe which actions the ii-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 nn robots, print one number 00 on a separate line;
if it is possible to find a point that is reachable by all nn 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 nn 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.

Examples

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

题意:q 组询问,每组给出 n 个点,对于每个点来说,首先给出点的坐标 x、y,然后再给出 4 个状态 f1、f2、f3、f4,分别代表其能否向左、向上、向右、向下移动,其中 0 代表不能移动,1 代表可以移动,问这 n 个点经过移动后能否汇集到一个点,若能则输出 1 x y,其中 (x,y) 代表任意一个可汇集到的点的坐标,若不能,输出 0

思路:

由于最后输出任意一个可汇集到的点的坐标,因此可以根据每一个点的所给的状态判断其可达的范围

数据范围为 -1E5~1E5,对每个点来说,如果其是某个方向的状态为 0,那么只需要对这个点的这个方向的可达的位置进行更新,最后对于所有点得到的四个状态可达的范围,就是所有点能汇集的坐标范围,任意输出坐标范围内的一个点即可

需要注意的是,对可达范围的合法性判断,如果可达范围的右端小于左端或者上端小于下端,那么可达范围是空的,输出 0

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;

int main(){
    int q;
    scanf("%d",&q);
    while(q--){
        int n;
        scanf("%d",&n);

        int left=-1E5,right=1E5;
        int up=1E5,down=-1E5;
        for(int i=1;i<=n;i++){
            int x,y;
            int fLeft,fUp,fRight,fDown;
            scanf("%d%d%d%d%d%d",&x,&y,&fLeft,&fUp,&fRight,&fDown);

            if(fLeft==0)
                left=max(x,left);
            if(fRight==0)
                right=min(x,right);
            if(fUp==0)
                up=min(y,up);
            if(fDown==0)
                down=max(y,down);
        }

        if(right<left||down>up)
            printf("0\n");
        else{
            printf("1 %d %d\n",left,up);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值