题目:



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. Myeclipse 编译等级

    1.Java compiler level does not match the version of the installed Java project facet. 问题描述:编译等级不匹配 解 ...

  2. iOS 执行时

    一.什么是执行时(Runtime)? 执行时是苹果提供的纯C语言的开发库(执行时是开发中经经常使用到的底层技术) 二.执行时的作用? 能获得某个类的全部成员变量 能获得某个类的全部属性 能获得某个类的 ...

  3. hive bucket

    转载:https://www.cnblogs.com/end/archive/2013/01/09/2852413.html hive中table可以拆分成partition,table和partit ...

  4. Storm sql 简单测试

    准备工作: 1.安装Kafka,启动,以及创建相应的topic 1.启动kafka bin/kafka-server-start.sh config/server.properties > /d ...

  5. Gentoo:startx出现Failed to load module问题

    安装完xorg-server后,startx启动桌面环境,出现缺少模块错误. 查看log: cat /var/log/Xorg.0.log | grep EE [75.403] (EE) Failed ...

  6. [Tools] Create a Simple CLI Tool in Node.js with CAC

    Command-line tools can help you with all sorts of tasks. This lesson covers the very basics of setti ...

  7. Android中IntentService详解

    简单说,IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service ...

  8. npm -D -S -g -i 以及安装技巧

    繁杂:npm install webpack 简洁:npm i webpack 重复性操作: npm i webpack npm i babel-core .... 简洁性操作: npm i webp ...

  9. 关于ionic中几个问题

    第一.每个页面的独立样式style标签不能写在ion-view外面,否则会出现路由问题,建议写在ion-content后面,例如下面的例子中,如果style但在ion-view中的话会出想路由问题,显 ...

  10. Android 软键盘的监听(监听高度,是否显示)

    Android官方本身没有提供一共好的方法来对软键盘进行监听,但我们实际应用时.非常多地方都须要针对软键盘来对UI进行一些优化. 下面是整理出来的一个不错的方法.大家能够使用. public clas ...