zoukankan      html  css  js  c++  java
  • gear gym 思维题

    题目:https://vj.69fa.cn/1fc993e7e0e1e6fa7ce4640b8d46ef8d?v=1552762626

    这个题目,之前有一点思路,但是呢,后来又不知道怎么去执行,然后就没有了耐心就去网上找了题解,这个去遍历每一个数,如果和他挨在一起就把它标记为相同的数d。

    然后之后判断如果又有标记相同的齿轮挨在一起则可以说明是三个齿轮紧紧扣在一起,就返回假,这个标记d也有特殊含义,这个标记按照正负去标记,这样最后一个和第一个符号进行判断就可以

    知道是不是顺时针,刚刚就在想可不可以用bfs呢?然后就去试了一下,发现可以用bfs写。。。。

    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <queue>
    #include <algorithm>
    #define inf 0x3f3f3f3f
    using namespace std;
    const int maxn = 1100;
    int dir[maxn] , n;
    struct node
    {
    	int x, y, r;
    	//node(int x = 0, int y = 0) :x(x), y(y){}
    }exa[maxn];
    struct heapnode
    {
    	int id, d;
    	heapnode(int id=0,int d=0):id(id),d(d){}
    };
    
    int gcd(int a,int b)
    {
    	return b == 0 ? a : gcd(b, a%b);
    }
    
    bool fill(int i,int d)
    {
    	dir[i] = d;
    	for(int j=1;j<=n;j++)
    	{
    		int dx = (exa[i].x - exa[j].x)*(exa[i].x - exa[j].x);
    		int dy = (exa[i].y - exa[j].y)*(exa[i].y - exa[j].y);
    		int R = (exa[i].r + exa[j].r)*(exa[i].r + exa[j].r);
    		if(i!=j&&dx+dy==R)
    		{
    			if (dir[i] == dir[j]) return false;
    			if (dir[j]) continue;
    			if (!fill(j, -d)) return false;
    		}
    	}
    	return true;
    }
    
    bool bfs()
    {
    	queue<heapnode>que;
    	que.push(heapnode(1,1));
    	dir[1] = 1;
    	while(!que.empty())
    	{
    		heapnode a = que.front(); que.pop();
    		int u = a.id;
    		int d = a.d;
    		for(int i=1;i<=n;i++)
    		{
    			int dx = (exa[u].x - exa[i].x)*(exa[u].x - exa[i].x);
    			int dy = (exa[u].y - exa[i].y)*(exa[u].y - exa[i].y);
    			int R = (exa[u].r + exa[i].r)*(exa[u].r + exa[i].r);
    			if(i!=u&&dx+dy==R)
    			{
    				if (dir[u] == dir[i]) return false;
    				if (dir[i]) continue;
    				dir[i] = -d;
    				que.push(heapnode(i, -d));
    			}
    		}
    	}
    	return true;
    }
    
    int main()
    {
    	
    	cin >> n;
    	memset(dir, 0, sizeof(dir));
    	for(int i=1;i<=n;i++)
    	{
    		scanf("%d %d %d", &exa[i].x, &exa[i].y, &exa[i].r);
    	}
    	if (!bfs()) printf("The input gear cannot move.
    ");
    	else if (!dir[n]) printf("The input gear is not connected to the output gear.
    ");
    	else
    	{
    		int g = gcd(exa[1].r, exa[n].r);
    		dir[1] = dir[1] * dir[n];
    		printf("%d:%d
    ", dir[1] * exa[1].r/g, exa[n].r/g);
    	}
    	return 0;
    }
    

      

    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <algorithm>
    #define inf 0x3f3f3f3f
    using namespace std;
    const int maxn = 1100;
    int dir[maxn] , n;
    struct node
    {
    	int x, y, r;
    }exa[maxn];
    
    int gcd(int a,int b)
    {
    	return b == 0 ? a : gcd(b, a%b);
    }
    
    bool fill(int i,int d)
    {
    	dir[i] = d;
    	for(int j=1;j<=n;j++)
    	{
    		int dx = (exa[i].x - exa[j].x)*(exa[i].x - exa[j].x);
    		int dy = (exa[i].y - exa[j].y)*(exa[i].y - exa[j].y);
    		int R = (exa[i].r + exa[j].r)*(exa[i].r + exa[j].r);
    		if(i!=j&&dx+dy==R)
    		{
    			if (dir[i] == dir[j]) return false;
    			if (dir[j]) continue;
    			if (!fill(j, -d)) return false;
    		}
    	}
    	return true;
    }
    
    int main()
    {
    	
    	cin >> n;
    	memset(dir, 0, sizeof(dir));
    	for(int i=1;i<=n;i++)
    	{
    		scanf("%d %d %d", &exa[i].x, &exa[i].y, &exa[i].r);
    	}
    	if (!fill(1, 1)) printf("The input gear cannot move.
    ");
    	else if (!dir[n]) printf("The input gear is not connected to the output gear.
    ");
    	else
    	{
    		int g = gcd(exa[1].r, exa[n].r);
    		dir[1] = dir[1] * dir[n];
    		printf("%d:%d
    ", dir[1] * exa[1].r/g, exa[n].r/g);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    打卡规则小模块设计
    json和ajax学习
    求两个数的百分比&时间格式化&ajax返回json
    spring security运行流程图(转)
    DelegatingFilterProxy作用浅析
    网站URL重写(Java UrlRewrite 的使用)
    JVM的常用的调优策略和垃圾回收算法及Tomcat的常用调优参数
    JQuery 的Ajax的使用
    servlet程序HTTP Status 500
    CanvasRenderingContext2D.drawImage()无效,not working
  • 原文地址:https://www.cnblogs.com/EchoZQN/p/10558973.html
Copyright © 2011-2022 走看看