Easy billiards (zoj 3761 并查集+DFS)
来源:互联网 发布:天刀iu捏脸数据 编辑:程序博客网 时间:2024/05/21 20:24
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 integerN, which means the number of the balls on the table. There are followingN 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
40 02 04 02 291 12 13 11 22 23 21 32 33 3
Sample output
1(2, 2) DOWN(4, 0) LEFT(2, 0) LEFT1(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 并查集+dfs
- Easy billiards (zoj 3761 并查集+DFS)
- ZOJ 3761——Easy billiards(DFS,并查集)
- ZOJ 3761 Easy billiards(并查集+树遍历)
- ZOJ 3761 Easy billiards[dfs]
- Zoj 3761 Easy billiards dfs
- ZOJ 3761 —— Easy billiards(并查集+深搜)
- ZOJ 3761 —— Easy billiards(并查集+深搜)
- zoj 3761 Easy billiards(dfs搜索树)
- zoj 3761 Easy billiards(建图+贪心+dfs)
- [贪心+dfs] ZOJ 3761 Easy billiards
- ZOJ 3761 Easy billiards (DFS性质)
- zoj 3761 Easy billiards
- zoj 3761 Easy billiards
- zoj 3761 Easy billiards 乱搞
- ZOJ 3811 dfs&并查集
- Easy billiards
- ZOJ 3811 Untrusted Patrol(dfs+并查集)
- 图文解释XCode常用快捷键的使用
- Android odex文件反编译
- TCP/IP协议
- 黑马程序员——Java基础--IO(一)
- 给你的WordPress实现利用自动Ping将文章实时推送到百度
- Easy billiards (zoj 3761 并查集+DFS)
- Event Machine
- phpcms 导航调用
- apt-get install or remove
- 【数学水题】【TOJ4113】【 Determine X】
- LCD5110
- 开源库
- 比较常用的linux命令
- ext-mcrypt error while installing laravel on ubuntu14.04 / cents 7.0