题目:



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. selenium实现失败重运行

    UI自动化脚本执行过程中存在非常多的不稳定性,例如网络的不稳定,浏览器无响应等等,这些失败往往并不是产品中的错误.那么这时我们往往需要对执行失败的测试用例进行多次重跑,确认其是否确实失败. 那么失败重 ...

  2. Solidworks如何在装配图中保存单独的一个零件

    如下图所示,我想要保存装配体的一个单独的零部件   选中该零件后点击编辑零部件   然后点击顶部的文件-另存为,弹出"解决模糊情形"对话框,询问你要保存装配体还是零部件   点击确 ...

  3. Java Applet 基础

    Java Applet 基础 Applet 是一种 Java 程序.它一般运行在支持 Java 的 Web 浏览器内.因为它有完整的 Java API支持,所以Applet 是一个全功能的 Java ...

  4. Activity返回数据给上一个活动

    1.在A这个Activity中以startActivityForResult(intent, requestCode)启动B这个Activity 2.在B这个Activity中setResult(re ...

  5. Hadoop最大值的算法中出现的错误(strToDouble)

    错误信息: Exception in thread "main" java.lang.NumberFormatException: For input string: " ...

  6. PHP结巴程序实现

    <?php $str="我...我要要要...学学学..编编程"; $str=preg_replace('/\./','',$str);//我我要要要学学学编编程 $str= ...

  7. Cache和Buffer的区别(转载)

    1. Cache:缓存区,是高速缓存,是位于CPU和主内存之间的容量较小但速度很快的存储器,因为CPU的速度远远高于主内存的速度,CPU从内存中读取数据需等待很长的时间,而  Cache保存着CPU刚 ...

  8. R语言初识

    # 创建数据集&基本数据管理1.向量 创建函数 c() a <- c(1,2,3,4) a[c(i,j)] :[]给定元素所处位置的数值,即向量a中第i和第j个元素,a[2]第二个元素即 ...

  9. 企业级分布式存储应用与实战MogileFS、FastDFS

    项目实战9—企业级分布式存储应用与实战MogileFS.FastDFS   目录 实战一:企业级分布式存储应用与实战 mogilefs 实现 原理 1.环境准备 2.下载安装,每个机器都一样 3.数据 ...

  10. Android下Fragment使用(全集)

    1 http://blog.csdn.net/niu_gao/article/details/7163263  思路清晰,讲解详细,代码偏少,推荐高手阅读 2 http://blog.csdn.net ...