Hdu 3231 Box Relations(拓扑排序)
来源:互联网 发布:c语言培训班 编辑:程序博客网 时间:2024/05/22 08:01
题目地址: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 debuusing 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
- HDU 3231 Box Relations(拓扑排序)
- HDU 3231 Box Relations(拓扑排序)
- Hdu 3231 Box Relations(拓扑排序)
- HDU 3231Box Relations 拓扑排序
- hdu 3231 Box Relations(拓扑排序)
- 【hdu】3231 Box Relations【拓扑排序三维】
- 杭电hdu 3231 box relations 拓扑排序
- hdu 3231 Box Relations 拓扑排序 详细解题报告
- Box Relations(拓扑排序)
- HDOJ 3231 Box Relations 拓扑排序
- HDU 3231 Box Relations (三维 拓扑)
- HDU3231 Box Relations(拓扑排序)经典
- 【拓扑排序】【HDU3231】【Box Relations】
- hdu 3231 Box Relations(模拟+判断长方体的相交和位置+拓扑排序)
- HDU 3231 Box Relations
- hdu 3231 Box Relations
- Hdu3231 step5.2.7 Box Relations(拓扑排序)
- hdu 5.2.6 3231 box relations
- Codevs 1689 建造高塔
- 《寻求优秀程序员之实战指南》读书笔记--了解优秀的程序员
- 关于opencv高版本中ImImage转Mat的问题(VS2013+opencv3.1)
- java ---servlet
- hdu2553-N皇后问题 DFS
- Hdu 3231 Box Relations(拓扑排序)
- Shell脚本参数
- java ----URL 从网络下载图片
- 华为OJ删除字符串中出现次数最少的字符
- java类设计原则的简单运用实例一
- Android反射获取资源 id通用工具类
- 下列哪些语句关于内存回收的说明是正确的? (b ) A、 程序员必须创建一个线程来释放内存
- HDU 3667 Transportation
- 记录第一次使用android studio签名打包