Codeforces Round #575 (Div. 3) E - Connected Component on a Chessboard

题目链接:

http://codeforces.com/problemset/problem/223/C

E. Connected Component on a Chessboard

You are given two integers bb and ww. You have a chessboard of size 109×109109×109 with the top left cell at (1;1)(1;1), the cell (1;1)(1;1) is painted white.

Your task is to find a connected component on this chessboard that contains exactly bb black cells and exactly ww white cells. Two cells are called connected if they share a side (i.e. for the cell (x,y)(x,y) there are at most four connected cells: (x−1,y),(x+1,y),(x,y−1),(x,y+1)(x−1,y),(x+1,y),(x,y−1),(x,y+1)). A set of cells is called a connected component if for every pair of cells C1C1 and C2C2 from this set, there exists a sequence of cells c1c1, c2c2, ..., ckck such that c1=C1c1=C1, ck=C2ck=C2, all cici from 11 to kk are belong to this set of cells and for every i∈[1,k−1]i∈[1,k−1], cells cici and ci+1ci+1 are connected.

Obviously, it can be impossible to find such component. In this case print "NO". Otherwise, print "YES" and any suitable connected component.

You have to answer qq independent queries.

Input

The first line of the input contains one integer qq (1≤q≤1051≤q≤105) — the number of queries. Then qq queries follow.

The only line of the query contains two integers bb and ww (1≤b,w≤1051≤b,w≤105) — the number of black cells required and the number of white cells required.

It is guaranteed that the sum of numbers of cells does not exceed 2⋅1052⋅105 (∑w+∑b≤2⋅105∑w+∑b≤2⋅105).

Output

For each query, print the answer to it.

If it is impossible to find the required component, print "NO" on the first line.

Otherwise, print "YES" on the first line. In the next b+wb+w lines print coordinates of cells of your component in any order. There should be exactly bb black cells and ww white cells in your answer. The printed component should be connected.

If there are several answers, you can print any. All coordinates in the answer should be in the range [1;109][1;109].

Example

input

Copy

3
1 1
1 4
2 5

output

Copy

YES
2 2
1 2
YES
2 3
1 3
3 3
2 2
2 4
YES
2 3
2 4
2 5
1 3
1 5
3 3
3 5 

 题目大意:

q次询问,有b个黑色块,w个白色块,问能否组成连通块,并任意输出一种联通块的坐标

要求:白块和黑块交替存在,下图是最优排列

 

This is the code:

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x3f3f3f3f      //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]= {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]= {-1, 1, 0, 0, -1, 1, -1, 1};
inline int read()//输入外挂
{
    int ret=0, flag=0;
    char ch;
    if((ch=getchar())=='-')
        flag=1;
    else if(ch>='0'&&ch<='9')
        ret = ch - '0';
    while((ch=getchar())>='0'&&ch<='9')
        ret=ret*10+(ch-'0');
    return flag ? -ret : ret;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int w,b;
        scanf("%d%d",&b,&w);//b黑色  w白色
        if(max(w,b)>min(w,b)*3+1)
        {
            printf("NO\n");
            continue;
        }
        if(w==b)
        {
            printf("YES\n");
            for(int i=3; i<=w+b+2; ++i)
                printf("5 %d\n",i);
        }
        else if(b<w)
        {
            printf("YES\n");
            w=w-b-1;//将在一行的减掉
            for(int i=4; i<=b*2+4; i++)
            {
                printf("4 %d\n",i);
                if(w>0 && i&1)
                {
                    printf("3 %d\n",i);
                    w--;
                }
                if(w>0  && i&1 )
                {
                    printf("5 %d\n",i);
                    w--;
                }
            }
        }
        else if(b>w)
        {
            swap(b,w);
            printf("YES\n");
            w=w-b-1;//将在一行的减掉
            for(int i=4; i<=b*2+4; i++)
            {
                printf("3 %d\n",i);
                if(w>0 && i&1)
                {
                    printf("2 %d\n",i);
                    w--;
                }
                if(w>0  && i&1 )
                {
                    printf("4 %d\n",i);
                    w--;
                }
            }
        }
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值