题目:



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. Java高级特性—JVM

    1).java监控工具使用 jconsole是一种集成了上面所有命令功能的可视化工具,可以分析jvm的内存使用情况和线程等信息 visualvm 提供了和jconsole的功能类似,提供了一大堆的插件 ...

  2. dmz主机就是DNAT功能的体现

    端口映射和DMZ是提供内网和外网映射的,具体各自如下:DMZ:就相当于DNAT(Destination NAT),只对目的IP地址做地址转换.也就是说,收到目的IP为自己WAN口的包,统统转发给内网的 ...

  3. Angular 学习笔记——$http

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  4. Effective C++ 条款17

    以独立语句将newed对象置入智能指针 本节我们须要学习的知识核心是注意编译器在同一语句中,调用次序具有不确定性,不同语句中,调用次序确定. 上面的话什么意思? 请看下面代码: int priorit ...

  5. 正则表达式---A bytes of python

    正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑.   给定一个正则表达式和另一个 ...

  6. Archlinux: 优化触摸板配置

    在逛 Archlinuxcn BBS 时看到这个帖子: fcitx 输入法看不到选词,上面键盘也不见了! 等待妹子的 依云 提到了 infinality, 并且给出了这个链接: fix-infinal ...

  7. MySql RESTRICT CASCADE SET NULL

    主表,从表[MySql] //http://my.oschina.net/cart/blog/277624 空.RESTRICT.NO ACTION 删除:从表记录不存在时,主表才可以删除.删除从表, ...

  8. Install RabbitMQ server in CentOS 7

    About RabbitMQ RabbitMQ is an open source message broker software, also sometimes known as message-o ...

  9. centos安装pip Xvfb Selenium

    pip安装命令: python -V wget https://bootstrap.pypa.io/get-pip.py python get-pip.py Xvfb安装命令: yum install ...

  10. Android下Sqlite的使用(9.7)

    1 http://blog.csdn.net/liuhe688/article/details/6715983 2 http://www.eoeandroid.com/thread-170715-1- ...