zoukankan      html  css  js  c++  java
  • URAL 1181 Cutting a Painted Polygon【递归+分治】

    题目:



    1181. Cutting a Painted Polygon

    Time limit: 1.0 second
    Memory limit: 64 MB
    There is a convex polygon with vertices painted in three colors: Red (R), Green (G) and Blue (B). It is known that all the colors are present and any two neighbor vertices have different colors. You are to find out whether it is possible to cut this polygon with noncrossing diagonals so that each of the obtained triangles would have all vertices of different colors: one red, one green and one blue vertex. Point out a possible way of the cutting if the cutting is possible.

    Input

    The first line contains a number N of the polygon vertices (4 ≤ N ≤ 1000). There are N symbols of the set {'R', 'G', 'B'} in the second line that specify a color for the correspondent vertex.

    Output

    The first line should contain either a number of drawn diagonals in case the required cutting is possible or the number 0 otherwise (cutting is impossible). In the first case the following lines should contain a description of the drawn diagonals. The description of a diagonal takes one line and consists of diagonal vertices numbers. The numbers are separated with a space. If there are several possible cuttings that satisfy the requirements you may output any of them.

    Sample

    input output
    7
    RBGBRGB
    
    4
    1 3
    3 7
    5 7
    5 3
    




    /**************************************************
    A	Accepted	144 KB	31 ms	Visual C++ 2010	3410 B
    题意:给你一个有 N (4 < N < 1000)个点的多变形,
          每个点有一个颜色'R','G','B'.
          问是否能把这个多边形划分成 N-3 个三角形,
          使得三角形的各个顶点颜色不同.
          如果没有:输出 0
          否则:输出 N-3和这些边
    
    算法:递归+分治
    
    思路:见黑书第一章
           当每种颜色剩下的都点 > 1的时候:
                找到相邻的不同颜色的三点,构成满足题意的三角形,删除中间的点,然后再从新找这个多边形
           一旦删点 ,删到某种颜色只剩一个点,那么依次从左划分,再依次从右划分即可。
    ************************************************/
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    using namespace std;
    
    const int maxn = 1000+10;
    char str[maxn];
    int index[maxn];
    
    int dfs(char a[])
    {
        int r,g,b;
        r = g = b = 0;
        int len = strlen(a);
        for(int i = 0; i < len; i++)
        {
            if(a[i] == 'R') r++;
            else if(a[i] == 'G') g++;
            else if(a[i] == 'B') b++;
        } /** 前面已经保证了相邻两点颜色不会相同*/
        if(r == 1)
        {
            for(int i = 0; i < len; i++)
            {
                if(a[i] == 'R')
                {/**三角形要求三点*/
                    for(int j = i+2; j <= (i == 0 ? len-2 : len-1); j++)
                    {
                        printf("%d %d
    ",index[i], index[j]);
                    }
                    for(int j = i-2; j >= (i == (len-1) ? 1 : 0); j--)
                    {
                        printf("%d %d
    ", index[i], index[j]);
                    }
                    return 1;
                }
            }
        }
        if(g == 1)
        {
            for(int i = 0; i < len; i++)
            {
                if(a[i] == 'G')
                {
                    for(int j = i+2; j <= (i == 0 ? len-2 : len-1); j++)
                    {
                        printf("%d %d
    ",index[i], index[j]);
                    }
                    for(int j = i-2; j >= (i == (len-1) ? 1 : 0); j--)
                    {
                        printf("%d %d
    ", index[i], index[j]);
                    }
                    return 1;
                }
            }
        }
        if(b == 1)
        {
            for(int i = 0; i < len; i++)
            {
                if(a[i] == 'B')
                {
                    for(int j = i+2; j <= (i == 0 ? len-2 : len-1); j++)
                    {
                        printf("%d %d
    ",index[i], index[j]);
                    }
                    for(int j = i-2; j >= (i == (len-1) ? 1 : 0); j--)
                    {
                        printf("%d %d
    ", index[i], index[j]);
                    }
                    return 1;
                }
            }
        }
        else
        {
            for(int i = 0; i < len-3; i++)
            {
                if(a[i] != a[i+1] && a[i+1] != a[i+2] && a[i] != a[i+2])
                {
                    printf("%d %d
    ", index[i], index[i+2]);
                    for(int j = i+1; j < len-1; j ++) /**删掉点 i+1 */
                    {
                        a[j] = a[j+1]; index[j] = index[j+1];
                    }
                    a[len-1] = ''; /**注意*/
                    break;
                }
            }
        }
        if(dfs(a)) return 1;
        return 0;
    
    }
    int main()
    {
        int n;
        while(scanf("%d", &n) != EOF)
        {
            scanf("%s",&str);
            int len = strlen(str);
            if(str[0] == str[len-1]) /**数据水,没有这种情况*/
            {
                printf("0
    ");
                continue;
            }
    
            int r, g, b;
            r = g = b = 0;
            for(int i = 0; i < n; i++)
            {
                if(str[i] == 'R') r++;
                else if(str[i] == 'G') g++;
                else if(str[i] == 'B') b++;
                index[i] = i+1; /**记录编号*/
            }
            if(r == 0 || g == 0 || b == 0) /**数据水,没有这种情况*/
            {
                printf("0
    "); continue;
            }
            for(int i = 0; i < n-1; i++) /**数据水, 没有这种情况*/
            {
                if(str[i] == str[i+1])
                {
                    len = -1;
                    printf("0
    ");
                    break;
                }
            }
    
            if(len == -1) continue;
    
            printf("%d
    ", n-3);
            dfs(str);
        }
        return 0;
    }
    





  • 相关阅读:
    集群、分布式与微服务概念和区别理解
    博弈论的入门——nim游戏&&sg函数浅谈
    csp-2020 初赛游记
    洛谷 P2340 [USACO03FALL]Cow Exhibition G 题解
    P5687 [CSP-SJX2019]网格图 题解
    HBase 数据迁移/备份方法
    mac远程连接服务上传下载命令实例
    Redis安装详细步骤
    VMware虚拟机中的CentOS服务安装Nginx后本机无法访问的解决办法
    开发业务逻辑处理之策略模式场景使用
  • 原文地址:https://www.cnblogs.com/freezhan/p/3219038.html
Copyright © 2011-2022 走看看