Hdu 3231 Box Relations(拓扑排序)

58 篇文章 0 订阅
4 篇文章 0 订阅
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3231

思路:拓扑排序。将每个长方体的每对面看成一条线段,将线段看成两个点,则共有3条线段,6个点。

对于一组相交关系,若两个长方体相交,当且仅当每一维中一个长方体的面插入另一个长方体的内部。

将长方体分为左右,上下,前后三维。

例如一长方形设左面为x,右面为x+n,另一长方体左面为y,右面为y+n,则在该维中坐标x<y+n,y<x+n,x<x+n,其他两维同理。

则由大小关系,分别对每一维拓扑排序即可。

#include<stack>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define debu
using namespace std;
const int maxn=2000+50;
char ch;
int n,m,x,y;
queue<int> q;
int d[3][maxn];
int tmp[3][maxn];
int ans[3][maxn];
vector<int> g[3][maxn];
int topsort(int id)
{
    int tot=0;
    while(!q.empty()) q.pop();
    for(int i=1; i<=2*n; i++)
    {
        if(!d[id][i])
        {
            //cout<<id<<" "<<i<<endl;
            tot++;
            q.push(i);
            tmp[id][tot]=i;
        }
    }
    while(!q.empty())
    {
        int now=q.front();
        q.pop();
        for(int i=0; i<g[id][now].size(); i++)
        {
            int nt=g[id][now][i];
            d[id][nt]--;
            if(!d[id][nt])
            {
                tot++;
                q.push(nt);
                tmp[id][tot]=nt;
            }
        }
    }
    return (tot==2*n);
}
void init()
{
    memset(d,0,sizeof(d));
    memset(tmp,0,sizeof(tmp));
    for(int i=0; i<=2*n; i++)
        for(int j=0; j<3; j++)
            g[j][i].clear();
    for(int i=0; i<3; i++)
        for(int j=1; j<=n; j++)
        {
            g[i][j].push_back(j+n);
            d[i][j+n]++;
        }
}
int solve()
{
    for(int i=0; i<3; i++)
        if(!topsort(i)) return 0;
    for(int i=0; i<3; i++)
        for(int j=1; j<=2*n; j++)
            ans[i][tmp[i][j]]=j-1;
    return 1;
}
int main()
{
#ifdef debug
    freopen("in.in","r",stdin);
#endif // debug
    int cas=0;
    while(scanf("%d%d",&n,&m)==2&&(n||m))
    {
        init();
        printf("Case %d: ",++cas);
        for(int i=0; i<m; i++)
        {
            getchar();
            scanf("%c%d%d",&ch,&x,&y);
            if(ch=='I')
            {
                g[0][x].push_back(y+n),d[0][y+n]++;
                g[0][y].push_back(x+n),d[0][x+n]++;
                g[1][x].push_back(y+n),d[1][y+n]++;
                g[1][y].push_back(x+n),d[1][x+n]++;
                g[2][x].push_back(y+n),d[2][y+n]++;
                g[2][y].push_back(x+n),d[2][x+n]++;
            }
            else
            {
                if(ch=='X')
                    g[0][x+n].push_back(y),d[0][y]++;
                else if(ch=='Y')
                    g[1][x+n].push_back(y),d[1][y]++;
                else if(ch=='Z')
                    g[2][x+n].push_back(y),d[2][y]++;
            }
        }
        /*for(int i=0; i<3; i++)
            for(int j=1; j<=2*n; j++)
                cout<<i<<" "<<j<<" "<<d[i][j]<<endl;*/
        if(!solve()) printf("IMPOSSIBLE\n\n");
        else
        {
            printf("POSSIBLE\n");
            for(int i=1; i<=n; i++)
                printf("%d %d %d %d %d %d\n",ans[0][i],ans[1][i],ans[2][i],ans[0][i+n],ans[1][i+n],ans[2][i+n]);
            printf("\n");
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值