题目:



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\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【递归+分治】的更多相关文章

  1. 递归分治算法之二维数组二分查找(Java版本)

    [java] /** * 递归分治算法学习之二维二分查找 * @author Sking 问题描述: 存在一个二维数组T[m][n],每一行元素从左到右递增, 每一列元素从上到下递增,现在需要查找元素 ...

  2. <算法竞赛入门经典> 第8章 贪心+递归+分治总结

    虽然都是算法基础,不过做了之后还是感觉有长进的,前期基础不打好后面学得很艰难的,现在才慢慢明白这个道理. 闲话少说,上VOJ上的专题训练吧:http://acm.hust.edu.cn/vjudge/ ...

  3. 递归&分治&贪心

    递归 Recursion:通过函数体来进行的循环. 思路简单但效率低(建立函数的副本,消耗大量时间和内存).能用迭代就不用递归.递推公式+递推终止条件. 计算n阶乘,递归实现 def Factoria ...

  4. 递归 & 分治算法深度理解

    首先简单阐述一下递归,分治算法,动态规划,贪心算法这几个东西的区别和联系,心里有个印象就好. 递归是一种编程技巧,一种解决问题的思维方式:分治算法和动态规划很大程度上是递归思想基础上的(虽然实现动态规 ...

  5. CF448C [Painting Fence]递归分治

    题目链接:http://codeforces.com/problemset/problem/448/C 题目大意:用宽度为1的刷子刷墙,墙是一长条一长条并在一起的.梳子可以一横或一竖一刷到底.求刷完整 ...

  6. HDU.5909.Tree Cutting(树形DP FWT/点分治)

    题目链接 \(Description\) 给定一棵树,每个点有权值,在\([0,m-1]\)之间.求异或和为\(0,1,...,m-1\)的非空连通块各有多少个. \(n\leq 1000,m\leq ...

  7. JSTree下的模糊查询算法——树结构数据层次遍历和递归分治地深入应用

    A表示区域节点,S表示站点结点 问题描述:现有jstree包含左图中的所有结点信息(包含区域结点和站点结点),需要做到输入站点名称模糊查询,显示查询子树结果如右图 解决策略: 1.先模糊查询所得站点所 ...

  8. QuickSort 递归 分治

    QuickSort 参考<算法导论>,<C程序设计语言> #include<stdio.h> void swap(int v[], int i, int j); v ...

  9. [置顶] 2013_CSUST暑假训练总结

    2013-7-19 shu 新生训练赛:母函数[转换成了背包做的] shuacm 题目:http://acm.hdu.edu.cn/diy/contest_show.php?cid=20083总结:h ...

随机推荐

  1. 面试题:Add Two Numbers(模拟单链表)

    题干: You are given two non-empty linked lists representing two non-negative integers. The digits are ...

  2. 2017.8.5 VMware的介绍与安装

    1 VMware简介 官网地址:http://www.vmware.com VMware的功能: 是一个虚拟PC的软件,可以在现有的操作系统上虚拟出一个新的硬件环境,相当于模拟出一台新的PC,以此来实 ...

  3. C++ 利用文件流复制文件

    bool CopyFile(const std::string &src, const std::string &dest) { std::ifstream fin(src.c_str ...

  4. Metadata publishing for this service is currently disabled.

    Reason 1: In your web.config <service name="A.B.C"> but your class is: namespace A.B ...

  5. S5:桥接模式 Bridge

    将抽象的部分与实现的部分分离,使它们都可以独立变化.抽象与实现的分离,指的是抽象类和派生类用来实现自己的对象.实现系统可能有多角度分类,每一种分类都有可能变化,那么就把这种多角度分离出来,让他们独立变 ...

  6. HTML 5 音频Audio

    在HTML5标准网页里面,我们能够运用audio标签来完毕我们对声音的调用及播放. 下面是最常常见到的运用HTML5三种基本格式: 1.最少的代码 <audio src="song.o ...

  7. .Net 异步调用

    .NET异步编程之新利器——Task与Await.Async   一.  FrameWork 4.0之前的线程世界    在.NET FrameWork 4.0之前,如果我们使用线程.一般有以下几种方 ...

  8. AIX 安装标准

    文件夹 一.网卡需求 二.光纤卡需求 三.磁盘需求 四.主机文件系统需求 五.主机名命名规范 六.安装设置规范 七.參数改动规范 八.时钟同步设置 九.rootvg做镜像 十.AIX系统安全加固 一. ...

  9. $on、$emit和$broadcast的使用

    $emit只能向parent controller传递event与data( $emit(name, args) ) $broadcast只能向child controller传递event与data ...

  10. 初识C++之虚函数

    1.什么是虚函数 在基类中用virtual关键字修饰.并在一个或多个派生类中被又一次定义的成员函数.使用方法格式为: virtual 函数返回类型 函数名(參数表) { 函数体 } 虚函数是实现多态性 ...