题目:



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. linux文件测试操作

    1.文件测试操作 返回 true 如果... -e 文件存在 -a 文件存在 这个选项的效果与-e 相同.但是它已经被弃用了,并且不鼓励使用 -f file 是一个 regular 文件(不是目录或者 ...

  2. UI_UITableView_搭建

    创建 tableView UITableViewStyle 有两种选择 #pragma mark - 创建 tableView - (void)createTableView { // 枚举类型共同拥 ...

  3. react-native AsyncStorage 数据持久化方案

    1,AsyncStorage介绍 AsyncStorage 是一个简单的.异步的.持久化的 Key-Value 存储系统,它对于 App 来说是全局性的.它用来代替 LocalStorage. 由于它 ...

  4. [转]Tomcat处理一个HTTP请求的过程

    1.Tomcat Server的组成部分 1.1 - Server A Server element represents the entire Catalina servlet container. ...

  5. Mysql:使用workbeanch导出数据库

    MySQL Workbench导出数据库   步骤: 1. 打开mysql workbench,进入需要导出的数据库,点击左侧栏的[Management]tab键. 2. 点选要输出的数据库 点击[D ...

  6. 原生JS实现的h5小游戏-植物大战僵尸

    代码地址如下:http://www.demodashi.com/demo/12755.html 项目介绍 本项目是利用原生js实现的h5小游戏-植物大战僵尸,主要结合了一下自己对于h5小游戏的理解,结 ...

  7. SpringBoot 框架整合

    代码地址如下:http://www.demodashi.com/demo/12522.html 一.主要思路 使用spring-boot-starter-jdbc集成Mybatis框架 通过sprin ...

  8. zabbix监控sockets连接数

    配置zabbix客户端配置文件 vim /etc/zabbix/zabbix_agentd.conf 添加  Include=/etc/zabbix/zabbix_agentd.d/ 添加脚本对soc ...

  9. MVC组件分析

    MVC组件分析   2 System.Web.Mvc V 4.0.0.0 组件分析 2.1 Routing组件(路由选择) Routing的作用就是负责分析Url Action的要求• 必须是一个公有 ...

  10. DelphiXe 中静态数组TByteArray和动态数组TBytes /array of byte 的区别

    在应用中发现静态数组和动态数组是有区别的: procedure TForm1.Button1Click(Sender: TObject);var  RsltStream: TMemoryStream; ...