zoukankan      html  css  js  c++  java
  • Uva


    把A+~Z+,A-~Z-看成52个点,正方形看成边,就得到一个有向图,当存在有向图的时候有解。和10129有点像,做拓扑排序即可。

    吐槽:刚开始把循环输入用cin,读取字符串用了scanf,直接WA了,要用还是用成一致的都cin,或者都scanf,这样就没问题了。

    AC代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cctype>
    #include <cstring>
    #include <string>
    #include <sstream>
    #include <vector>
    #include <set>
    #include <map>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <bitset> 
    #include <cassert> 
    
    using namespace std;
    
    const int maxn = 52;
    
    int G[maxn][maxn];
    
    // 把52种字符A+~Z+,A-~Z-,转化成数字
    int ID(char a1, char a2)
    {
    	return (a1 - 'A') * 2 + ((a2 == '+') ? 0 : 1);
    }
    
    void connect(char a1, char a2, char b1, char b2)
    {
    	if (a1 == '0' || b1 == '0') { // 有00边不能连接
    		return;
    	}
    	int u = ID(a1, a2) ^ 1;
    	int v = ID(b1, b2);
    	G[u][v] = 1;
    }
    
    int c[maxn]; // 表示访问状态
    
    // 判断是否从u出发存在有向环
    bool dfs(int u)
    {
    	c[u] = -1; // 表示正在访问
    	for (int v = 0; v < maxn; 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 findCycle()
    {
    	memset(c, 0, sizeof(c));
    	for (int i = 0; i < maxn; i++) {
    		if (!c[i]) {
    			if (dfs(i)) {
    				return true;
    			}
    		}
    	}
    	return false;
    }
    
    int main()
    {
    	ios::sync_with_stdio(false);
    	int n;
    	//while (scanf("%d", &n) == 1 && n) {
    	while (cin >> n && n) {
    		memset(G, 0, sizeof(G));
    		while (n--) {
    			char s[10];
    			//scanf("%s", s);
    			cin >> 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 (findCycle()) {
    			printf("unbounded
    ");
    		}
    		else {
    			printf("bounded
    ");
    		}
    	}
    
    	return 0;
    }




  • 相关阅读:
    盘点开发中那些常用的MySQL优化
    刚入职!就遇到上亿(MySQL)大表的优化
    面试技巧|“唇枪舌剑”之十大招式
    rpc是什么?php中流行的rpc框架有哪些?
    使用 memory_limit 限制 PHP 进程的内存使用
    swoole查看子进程与主进程关系
    使用SecureCRT连接AWS的EC2(Linux系统)亲测可行
    substitute Simple JavaScript Template :
    git 简单图表
    jquery 队列
  • 原文地址:https://www.cnblogs.com/zhangyaoqi/p/4591548.html
Copyright © 2011-2022 走看看