URAL 1181 Cutting a Painted Polygon【递归+分治】

题目:

http://acm.timus.ru/problem.aspx?space=1&num=1181

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27048#problem/A

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



转载来自:cxb:http://blog.csdn.net/cxb569262726/article/details/7845369

/**************************************************
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\n",index[i], index[j]);
                }
                for(int j = i-2; j >= (i == (len-1) ? 1 : 0); j--)
                {
                    printf("%d %d\n", 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\n",index[i], index[j]);
                }
                for(int j = i-2; j >= (i == (len-1) ? 1 : 0); j--)
                {
                    printf("%d %d\n", 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\n",index[i], index[j]);
                }
                for(int j = i-2; j >= (i == (len-1) ? 1 : 0); j--)
                {
                    printf("%d %d\n", 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\n", 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] = '\0'; /**注意*/
                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\n");
            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\n"); continue;
        }
        for(int i = 0; i < n-1; i++) /**数据水, 没有这种情况*/
        {
            if(str[i] == str[i+1])
            {
                len = -1;
                printf("0\n");
                break;
            }
        }

        if(len == -1) continue;

        printf("%d\n", n-3);
        dfs(str);
    }
    return 0;
}





你可能感兴趣的:(URAL 1181 Cutting a Painted Polygon【递归+分治】)