codeforces 1196E Connected Component on a Chessboard 构造

http://codeforces.com/problemset/problem/1196/E
You are given two integers b and w. You have a chessboard of size 109×109 with the top left cell at (1;1), the cell (1;1) is painted white.

Your task is to find a connected component on this chessboard that contains exactly b black cells and exactly w white cells. Two cells are called connected if they share a side (i.e. for the cell (x,y) there are at most four connected cells: (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 C1 and C2 from this set, there exists a sequence of cells c1, c2, …, ck such that c1=C1, ck=C2, all ci from 1 to k are belong to this set of cells and for every i∈[1,k−1], cells ci and ci+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 q independent queries.

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

The only line of the query contains two integers b and w (1≤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⋅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+w lines print coordinates of cells of your component in any order. There should be exactly b black cells and w 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].

Example
Input
3
1 1
1 4
2 5
Output
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
题目大意:找一个连通分量,有 b b b个黑色棋子和 w w w个白色棋子,若无输出"NO"。
思路:容易发现, ( 1 , 1 ) , ( 1 , 3 ) , ( 1 , 5 ) … … ( 2 , 2 ) , ( 2 , 4 ) , ( 2 , 6 ) … … ( 3 , 1 ) ( 3 , 3 ) … … (1,1),(1,3),(1,5)……(2,2),(2,4),(2,6)……(3,1)(3,3)…… (1,1),(1,3),(1,5)(2,2),(2,4),(2,6)(3,1)(3,3)都是白色棋子,同理 ( 1 , 2 ) , ( 1 , 4 ) , ( 1 , 6 ) … … ( 2 , 1 ) , ( 2 , 3 ) , ( 2 , 5 ) … … ( 3 , 2 ) , ( 3 , 4 ) … … (1,2),(1,4),(1,6)……(2,1),(2,3),(2,5)……(3,2),(3,4)…… (1,2),(1,4),(1,6)(2,1),(2,3),(2,5)(3,2),(3,4)都是黑色棋子。看一下数据范围,我们完全可以只选同一列的嘛。下面为了方便说明,设 w &gt; b w&gt;b w>b。为了保证连通性,即题目有解,必须保证 w &lt; = 3 ∗ b + 1 w&lt;=3*b+1 w<=3b+1。因为第一个黑色棋子周围可以选 4 4 4个白色棋子,为了保证连通性,剩余的黑色棋子每个周围最多选 3 3 3个白色棋子,因此上界就是 3 ∗ ( b − 1 ) + 4 = 3 ∗ b + 1 3*(b-1)+4=3*b+1 3(b1)+4=3b+1。由此我们也得到了一个构造方法:首先选定一个黑色棋子 ( x , y ) (x,y) (x,y)作为起点,(1)若 w − b &gt; = 3 w-b&gt;=3 wb>=3,把起点周围的四个白色棋子都选了,同时令 y + = 2 y+=2 y+=2;(2)选择当前 ( x , y ) (x,y) (x,y)上、左、右三侧的白色棋子,同时令 y + = 2 y+=2 y+=2,重复(2)直到 w = b w=b w=b。(3)选择当前 ( x , y ) (x,y) (x,y)上侧的白色棋子,同时令 y + = 2 y+=2 y+=2,重复(3)直到 w = 0 w=0 w=0。对坐标的修改具体见代码。

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
#define pr pair<int,int>
#define INF 1e5
using namespace std;

int n,a,b;
int d[4][2]={{0,-1},{0,1},{-1,0},{1,0}};

int main()
{
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d%d",&a,&b);
		if(a>3*b+1||b>3*a+1)
			printf("NO\n");
		else
		{
			printf("YES\n");
			int ax=3,ay=2;
			int bx=6,by=2;
			if(a<b)
			{
				if(b-a>=3)
				{
					printf("%d %d\n",ax,ay);
					for(int i=0;i<4;i++)
						printf("%d %d\n",ax+d[i][0],ay+d[i][1]);
					a-=1;
					b-=4;
					ay+=2;
				}
				while(a<b)
				{
					--a;
					printf("%d %d\n",ax,ay);
					for(int i=1;i<4&&a<b;i++)
						--b,printf("%d %d\n",ax+d[i][0],ay+d[i][1]);
					ay+=2;
				}
				while(a==b&&a)
				{
					printf("%d %d\n",ax,ay);
					printf("%d %d\n",ax,ay+1);
					ay+=2;
					--a,--b;
				}
			}
			else if(b<a)
			{
				if(a-b>=3)
				{
					printf("%d %d\n",bx,by);
					for(int i=0;i<4;i++)
						printf("%d %d\n",bx+d[i][0],by+d[i][1]);
					a-=4;
					b-=1;
					by+=2;
				}
				while(b<a)
				{
					--b;
					printf("%d %d\n",bx,by);
					for(int i=1;i<4&&b<a;i++)
						--a,printf("%d %d\n",bx+d[i][0],by+d[i][1]);
					by+=2;
				}
				while(a==b&&a)
				{
					printf("%d %d\n",bx,by);
					printf("%d %d\n",bx,by+1);
					by+=2;
					--a,--b;
				}
			}
			else
			{
				while(a==b&&a)
				{
					printf("%d %d\n",ax,ay);
					printf("%d %d\n",ax,ay+1);
					ay+=2;
					--a,--b;
				}
			}
		}
	}
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值