Easy billiards (zoj 3761 并查集+DFS)

原创 2015年07月08日 22:43:44

Easy billiards

Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge

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.

Input

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)

Output

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).

Sample Input

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

Sample output

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;
}



相关文章推荐

ZOJ 3761 Easy billiards(并查集+树遍历)

E - Easy billiards Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Su...

zoj 3761 Easy billiards(dfs搜索树)

题意:平面有n个台球,每次可以选择一个台球,让它向着水平或垂直的方向,遇到其它台球它就会停在这个位置,而被击中的球继续前进,不能让一个球向着没有球的方向运动,最外面的球会被打出去。问最后最少能剩多少球...
  • qian99
  • qian99
  • 2014年03月02日 17:38
  • 1053

ZOJ 3761 Easy billiards[dfs]

给n个球的坐标,你每次需要选一个球,沿着x轴或y轴去撞别的球,选的球停在被撞球位置,被撞球继续前进。选球不能沿着没有球的方向去。最外面的球会被撞出去。问最后最少能剩下多少个球,还要输出选择的方案。...

ZOJ 3811 dfs&并查集

点击打开链接 题意:给n个城镇,有的城镇会发光,给出发光的城镇,然后下面有一个人走的发光的城镇的顺序,每个城镇只会发一次光,问这个人按这个顺序是否合法并且所有的城镇都被访问到 思路:可以用dfs去搜索...
  • Dan__ge
  • Dan__ge
  • 2016年05月28日 14:32
  • 517

zoj 3641 并查集+set

题意:有一个教室 然后每个同学会知道一些信息 然后老师监视着他们 给出mei c...

ZOJ2334 Monkey King 左偏树+并查集

有一n个猴子,每个猴子有一个战斗力,如果x和y不认识那么x会找他认识的猴子中战斗力最高的猴子与y认识的猴子中战斗力最高的猴子打一架,然后两只打架的猴子战斗力减半,然后x认得所有猴子就和y认识的所有猴子...

ZOJ 3649 Social Net 最大生成树 + 并查集维护

题意:给定一个n(2v路径上顺序          得到的ci cj(i 题解:kruskal搞掉最大生成树,并查集维护up[] dw[] ma[] mi[] 分别表示从当前点走到lca的最优值...

Zoj 3563 Connections in Galaxy War 逆向并查集

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3563

ZOJ 3659 Conquer a New Region【并查集】

ZOJ 3659 Conquer a New Region

zoj3261 并查集---略有变形

题意:有n个星球,每个星球有一定的power值,某些星球是直接或间接相连的。。。当某个星球想求助时会找到相连的里面的power值最大而且大于自己的一个星球。。。先在给定这些power值并给定两两相连的...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Easy billiards (zoj 3761 并查集+DFS)
举报原因:
原因补充:

(最多只允许输入30个字)