Easy billiards (zoj 3761 并查集+DFS)
2015-07-08 22:43
447 查看
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.
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)
you hit. (LEFT,RIGHT,UP,DOWN).
题意:一个平面上有n个点的坐标,现在用一个球a去撞另外一个球b,b被撞到无穷远,a停在b的位置,问怎样撞可以使最后平面上的球最少。
思路:先用并查集将在同一行或者同一列上的球连成一个集合,用dfs搜索输出。
代码:
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 integerN, 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 directionyou 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; }
相关文章推荐
- C#接口的理解
- Single Number
- 我心中的核心组件(可插拔的AOP)~大话开篇及目录
- 5只猴子分桃子问题
- TCP/IP协议
- Android odex文件反编译
- 黑马程序员——Java基础--IO(一)
- Xamarin.Android开发实践(十七)
- Section 1.2.3 Name That Number 水
- 第十五章 单点登录——《跟我学Shiro》
- logback配置日志文件
- 【Android 应用开发】 自定义组件 宽高适配方法, 手势监听器操作组件, 回调接口维护策略, 绘制方法分析 -- 基于 WheelView 组件分析自定义组件
- 图文解释XCode常用快捷键的使用
- LV的几种适配器特点
- Gas Station
- html5本地存储
- 6.1-2
- 如何应对SparkSQL DataFrame保存到hdfs时出现的过多小文件问题
- com.example.firstservice.FirstService cannot be cast to android.app.Activity
- 消除启动欢迎页时的黑屏问题