日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 综合教程 >内容正文

综合教程

ZOJ

發(fā)布時(shí)間:2024/5/24 综合教程 23 生活家
生活随笔 收集整理的這篇文章主要介紹了 ZOJ 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目鏈接

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

以上是生活随笔為你收集整理的ZOJ的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。