ZOJ 3761 Easy billiards (DFS性質),zojbilliards
題意 桌上有n個球 a球撞擊b球時 a球停在b球位置 b球保持a球運動 若b球前面再沒有球 b球就會掉下桌子 給你n個球的座標 你可以多次選擇某個撞擊方向前面還有球的球撞擊 問最後桌上至少還剩多少球 並輸出你的撞擊過程
可以把x座標或y座標相同的點當作是連通的 因為可以通過撞擊一個球使另一個球掉下案頭
那麼容易發現 一個連通塊內的m個球總可以經過m-1次撞擊後變成只剩一個球 這個球可以是當前連通塊中的任意一個 有多少個連通塊最後就最少剩下多少球 然後就可以通過一次dfs解決問題 這裡利用了dfs的性質 在搜尋樹中子節點的球總可以通過一次朝父節點撞擊使自己位置沒有球 撞擊方向只用比較座標就行了 然後從葉子到根輸出就行了
#include <bits/stdc++.h>using namespace std;const int N = 2005;int x[N], y[N], vis[N];int dir[N], p[N], n, m;void dfs(int i){ vis[i] = 1; for(int j = 0; j < n; ++j) { if(!vis[j] && (x[j] == x[i] || y[j] == y[i])) { dfs(j); //j和i屬於一個連通塊且j還沒被訪問 對j深搜 if(x[j] == x[i]) //記錄路徑 上下左右分別用0,1,2,3表示 dir[j] = y[j] < y[i] ? 0 : 1; else dir[j] = x[j] < x[i] ? 3 : 2; p[m++] = j; } }}int main(){ while(~scanf("%d", &n)) { for(int i = 0; i < n; ++i) scanf("%d%d", &x[i], &y[i]); memset(vis, 0, sizeof(vis)); int cnt = m = 0; for(int i = 0; i < n; ++i) if(!vis[i]) ++cnt, dfs(i); printf("%d\n", cnt); char sdir[][10] = {"UP", "DOWN", "LEFT", "RIGHT"}; for(int i = 0; i < m; ++i) { int j = p[i]; printf("(%d, %d) %s\n", x[j], y[j], sdir[dir[j]]); } } return 0;}//Last modified : 2015-07-10 08:26
Easy billiardsTime 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
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
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。