Edward think a game of billiards is too long and boring. So he invented a new game called Easy billiards.
Easy billiards has N balls on a brimless rectangular table in the beginning, and your goal is try to make the number of balls on the table as least as possible by several hit under the following rules:
1: The direction you hit the balls should parallel to the tables border.
2: If ball A crashed into ball B, ball B will moves in the same direction of ball A before the crashing, and ball A will stop in the place of ball B before the crashing.
3: If ball C is moving and there are no balls in front of ball C, it will runs out of the tables border, that means ball C is out of the table.
4: You can choose arbitrary ball on the table to hit, but on a hit, you can't let the ball you choose to hit runs out of the tables border. In another word, a ball could runs out of the table if and only if it was crashed by another ball in a hitting.
Now, Edward wants to know the least number of balls remained on the table after several hits, and how.
There are multiple test cases. For each test cases, in the first line, there is an integer N, which means the number of the balls on the table. There are following N lines, each line contains two integers Xi and Yi, which means the coordinate of ball I. (0<=N<=2000, 0<=Xi, Yi<=10^8)
For each test cases, you should output the least number of balls on the first line.And you should output several lines to show the order of hits following the first line, each line should contains the coordinate of the ball you choose to hit and the direction you hit. (LEFT,RIGHT,UP,DOWN).
4 0 0 2 0 4 0 2 2 9 1 1 2 1 3 1 1 2 2 2 3 2 1 3 2 3 3 3
1 (2, 2) DOWN (4, 0) LEFT (2, 0) LEFT 1 (1, 3) DOWN (1, 2) DOWN (2, 3) DOWN (2, 2) DOWN (3, 3) DOWN (3, 2) DOWN (3, 1) LEFT (2, 1) LEFT
题意:一个平面上有n个点的坐标,现在用一个球a去撞另外一个球b,b被撞到无穷远,a停在b的位置,问怎样撞可以使最后平面上的球最少。
思路:先用并查集将在同一行或者同一列上的球连成一个集合,用dfs搜索输出。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 2005
#define MAXN 2000005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef long long ll;
using namespace std;
struct Node
{
int x,y;
}node[maxn];
struct Edge
{
int u,v,next;
}edge[MAXN];
int head[maxn];
int father[maxn];
int num,n;
bool vis[maxn];
void init()
{
num=0;
mem(head,-1);
for (int i=0;i<=n;i++)
{
father[i]=i;
vis[i]=false;
}
}
void addedge(int u,int v)
{
edge[num]={u,v,head[u]};
head[u]=num++;
edge[num]={v,u,head[v]};
head[v]=num++;
}
int find_father(int x)
{
if (x!=father[x])
father[x]=find_father(father[x]);
return father[x];
}
void Union(int x,int y,int cnt)
{
int i,j;
for (i=0;i<cnt;i++)
{
int u=node[i].x,v=node[i].y;
if (x==u||y==v)
{
addedge(i,cnt);
int fu=find_father(i);
int fv=find_father(cnt);
if (fu!=fv) father[fu]=fv;
}
}
}
void print(int i,int j)
{
printf("(%d, %d) ",node[i].x,node[i].y);
if (node[i].x==node[j].x)
{
if (node[i].y<node[j].y)
pf("UP\n");
else
pf("DOWN\n");
return ;
}
if (node[i].x<node[j].x)
pf("RIGHT\n");
else
pf("LEFT\n");
return ;
}
void dfs(int u,int pre)
{
vis[u]=true;
for (int i=head[u];i+1;i=edge[i].next)
{
int v=edge[i].v;
if (!vis[v])
dfs(v,u);
}
if (pre!=-1)
print(u,pre);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("C:/Users/asus1/Desktop/IN.txt","r",stdin);
#endif
int i,j,x,y;
while (~sf(n))
{
init();
for (i=0;i<n;i++)
{
sff(node[i].x,node[i].y);
Union(node[i].x,node[i].y,i);
}
int ans=0;
for (i=0;i<n;i++)
if (father[i]==i) ans++;
pf("%d\n",ans);
for (i=0;i<n;i++)
if (father[i]==i)
dfs(i,-1);
}
return 0;
}