题目:

http://acm.timus.ru/problem.aspx?space=1&num=1181
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27048#problem/A

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
转载来自:cxb:http://blog.csdn.net/cxb569262726/article/details/7845369
/**************************************************
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. URAL 1181 Cutting a Painted Polygon(解题报告)

    转载请注明出处:http://blog.csdn.net/cxb569262726/article/details/7845369 题目链接:http://acm.timus.ru/problem.a ...

  2. Ural 1181 Cutting a Painted Polygon

    这道题我一开始思路错了,在结束递归的时候用的是顶点数减到2,虽然也是每次去掉一个角,但是处理不了特殊情况. 看了书上的解法,这个递归结束条件就巧妙很多了,原来递归可以这样,找到一种特殊情况,当递归到这 ...

  3. [Virtual Judge]URAL1181:Cutting a Painted Polygon

    点击打开题目链接 1181. Cutting a Painted Polygon Time limit: 1.0 second Memory limit: 64 MB There is a conve ...

  4. [剑指offer][JAVA]面试题第[33]题[二叉搜索树的后序遍历][单调栈][递归分治]

    [问题描述][中等] 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果.如果是则返回 true,否则返回 false.假设输入的数组的任意两个数字都互不相同.参考以下这颗二叉搜索树:5/ ...

  5. 递归分治问题之找出两个有序序列的中间值

    问题描述: You are interested in analyzing some hard-to-obtain data from two separate databases. Each dat ...

  6. 递归/分治:归并排序

    前言 分治算法: 将一个规模为N的问题分解为K个规模较小的子问题,这些子问题相互独立且与原问题性质相同.求出 子问题的解后进行合并,就可得到原问题的解. 步骤如下: 分解,将要解决的问题划分成若 干规 ...

  7. Algorithms_算法思想_递归分治

    文章目录 引导案例 递归的定义 什么样的问题可以用递归算法来解决 递归如何实现以及包含的算法思 递归的公式 斐波那契数列代码实现 递归的时间复杂度和空间复杂度 递 与 归 递归的优化 优化方式一:不使 ...

  8. 算法思想之递归分治回溯

    参考文档 递归思想 思想 描述 递归 当需要重复地多次计算相同的问题,通常可以采用递归或循环.递归是在一个函数内部调用这个函数自身. 递归的本质是把一个问题分解成两个或多个小问题.(注:当多个小问题存 ...

  9. P1010 幂次方 (递归+分治)

    题目描述 任何一个正整数都可以用2的幂次方表示.例如 137=2^7+2^3+2^0 同时约定方次用括号来表示,即a^b可表示为a(b). 由此可知,137可表示为: 2(7)+2(3)+2(0) 进 ...

最新文章

  1. ajax 文件数据流,Ajax如何读取数据流中的xml文件?
  2. leetcode 438:Find All Anagrams in a String 找变位子串
  3. scanf在c语言中的作用是什么?
  4. LeetCode 1403. 非递增顺序的最小子序列(排序)
  5. linux system V IPC 信号灯和共享内存实例
  6. 运行adb devices命令后 显示 List of devices attached 无法获取设备解决方法
  7. 项目入口_住宅小区入口就该这么设计,说得好仔细!
  8. [HDU5739]Fantasia(圆方树DP)
  9. linux 把数字传给bc,linux – 在Bash中使用bc舍入数字
  10. NRF24L01+在K60单片机中的具体实现
  11. TI am3352 gpio 驱动
  12. Android 视频和图片轮播控件,仿淘宝商品详情页
  13. 常见报错:RuntimeError: expected scalar type Long but found Float
  14. 幼儿拼图识字 v1.0 官网
  15. python爬虫(13)爬取百度贴吧帖子
  16. java bitset类_Java Bitset类 - Java 教程 - 自强学堂
  17. Windows安装IDEA详细步骤
  18. HTML accesskey属性详解
  19. 基于Android的校园闲置物品交易平台的设计与实现(二手交易平台)
  20. 零基础怎么学好人工智能?

热门文章

  1. mipi 转HDMI
  2. 基于 CFSFDP 聚类算法的电信客户价值分析
  3. linux 安装office
  4. Kubernete(k8s)—资源清单
  5. 精品慕课资源推荐线性代数
  6. python的网络请求
  7. html网页怎么两边留白,网页的页面留白的2个原因,知道原因才能合理设置
  8. sql 添加列、删除列、修改列属性以及大小
  9. 【Linux】Ubuntu下制作windows U盘启动盘
  10. 安装A93WebService,[SC] OpenService 失败报错解决