码迷,mamicode.com
首页 > 其他好文 > 详细

Uva 1572 自组合

时间:2017-08-29 14:32:13      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:connect   ace   string   elf   nec   col   memset   nsf   源码   

贴个源码
//
UVa1572 Self-Assembly // Rujia Liu #include<cstdio> #include<cstring> #include<vector> using namespace std; int ID(char a1, char a2) { return (a1-A)*2 + (a2 == + ? 0 : 1); } int G[52][52]; // connect(A+, B-) means a border labeled with A- can be transformed to B- void connect(char a1, char a2, char b1, char b2) { if(a1 == 0 || b1 == 0) return; int u = ID(a1, a2)^1, v = ID(b1, b2); G[u][v] = 1; } int c[52]; // returns true iff there is a cycle reachable from u bool dfs(int u) { c[u] = -1; for(int v = 0; v < 52; v++) if(G[u][v]) { if(c[v] < 0) return true; else if(!c[v] && dfs(v)) return true; } c[u] = 1; return false; } bool find_cycle() { memset(c, 0, sizeof(c)); for(int i = 0; i < 52; i++) if(!c[i]) if(dfs(i)) return true; return false; } int main() { int n; while(scanf("%d", &n) == 1 && n) { memset(G, 0, sizeof(G)); while(n--) { char s[10]; scanf("%s", s); for(int i = 0; i < 4; i++) for(int j = 0; j < 4; j++) if(i != j) connect(s[i*2], s[i*2+1], s[j*2], s[j*2+1]); } if(find_cycle()) printf("unbounded\n"); else printf("bounded\n"); } return 0; }

 

Uva 1572 自组合

标签:connect   ace   string   elf   nec   col   memset   nsf   源码   

原文地址:http://www.cnblogs.com/Jadon97/p/7447536.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!