ZOJ
題目鏈接
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3761
題意
在一個(gè)桌面上,給出一些球 如果在A球的某個(gè)方向的前方有B球 那個(gè)A球就可以朝那個(gè)方向滾 然后 滾到B球的位置后 ,B球往前滾,A球停在B球的位置上
求這樣操作后,最后最少剩下多少球,然后要輸出操作的方式
思路
其實(shí)可以發(fā)現(xiàn),一個(gè)球經(jīng)過(guò)一次操作之后,相當(dāng)于這個(gè)球消失了 因?yàn)樗娲怂胺侥莻€(gè)球的位置 這個(gè)操作又迭代下去
最后發(fā)現(xiàn) 其實(shí)只有這個(gè)球本身的位置是沒(méi)有球了
所以 如果一個(gè)球的四周有球 這個(gè)球就可以消失
那么 我們可以把一個(gè)球的四個(gè)方向上的球都并起來(lái),然后并起來(lái)的球的四個(gè)方向的球又可以并起來(lái),最后剩下的球的個(gè)數(shù) 其實(shí)就是連通塊的個(gè)數(shù)
這個(gè) 并 我們只需要指向父節(jié)點(diǎn)就可以了 而不需要指向祖先節(jié)點(diǎn),因?yàn)椴僮鞣绞街?就是將這個(gè)球打向父節(jié)點(diǎn),而不是祖先節(jié)點(diǎn),因?yàn)楹妥嫦裙?jié)點(diǎn)并不一定是在同一個(gè)方向上的
AC代碼
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
#define bug puts("***bug***");
#define fi first
#define se second
//#define bug
//#define gets gets_s
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <string, int> psi;
typedef pair <string, string> pss;
typedef pair <double, int> pdi;
const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int maxn = 2e3 + 10;
const int MOD = 1e9 + 7;
int pre[maxn];
int tot[maxn];
int find(int x)
{
while (x != pre[x])
x = pre[x];
return x;
}
void join(int x, int y)
{
int fx = find(x), fy = find(y);
if (fx != fy)
pre[fx] = fy;
}
int n;
int G[maxn][maxn];
int x[maxn], y[maxn];
bool judge(int a, int b)
{
if (x[a] == x[b] || y[a] == y[b])
return true;
return false;
}
void dfs(int root, int vis)
{
for (int i = 1; i <= n; i++)
{
if (G[root][i] == 1 && pre[i] == i && root != i && i != vis)
{
pre[i] = root;
dfs(i, vis);
}
}
}
void clear()
{
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
G[i][j] = 0;
for (int i = 1; i <= n; i++)
{
pre[i] = i;
tot[i] = 0;
}
}
void print(int root, int son)
{
if (x[root] == x[son])
puts(y[root] > y[son] ? "UP" : "DOWN");
else
puts(x[root] > x[son] ? "RIGHT" : "LEFT");
}
int main()
{
while (~scanf("%d", &n))
{
clear();
for (int i = 1; i <= n; i++)
scanf("%d%d", &x[i], &y[i]);
for (int i = 1; i <= n; i++)
{
G[i][i] = 1;
for (int j = i + 1; j <= n; j++)
{
if (judge(i, j))
G[i][j] = G[j][i] = 1;
}
}
//for (int i = 1; i <= n; i++)
// for (int j = 1; j <= n; j++)
// printf("%d%c", G[i][j], j == n ? '
' : ' ');
for (int i = 1; i <= n; i++)
{
for (int j = i + 1; j <= n; j++)
{
if (G[i][j] && pre[j] == j)
{
pre[j] = i;
dfs(j, i);
}
}
}
map <int, int> m;
for (int i = 1; i <= n; i++)
{
int u = find(i);
m[u]++;
}
for (int i = 1; i <= n; i++)
tot[pre[i]]++;
printf("%d
", m.size());
int pos = 1;
int len = n - m.size();
while (pos <= len)
{
for (int i = 1; i <= n && pos <= len; i++)
{
int root = pre[i];
if (i != root && tot[i] == 0)
{
printf("(%d, %d) ", x[i], y[i]);
print(root, i);
tot[root]--;
tot[i]--;
pos++;
}
}
}
}
}
總結(jié)
- 上一篇: 黑枸杞泡水有什么功效 黑枸杞泡水喝的禁忌
- 下一篇: Excel如何隔列求和电脑如何求和