点击打开题目链接

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

=====================================题目大意=====================================

给定一个顶点颜色为红绿蓝三者之一且相邻顶点颜色不同的N边形。

输出任意一种通过不交叉的对角线将该多边形划分为各个顶点颜色不同的三角形的方案。

如果不存在这样的方案则输出0。

=====================================算法分析=====================================

分治算法(此题即黑书P20:三色多边形)。

本来就想写上面那一句话,结果最后忍不住又写了下面一大堆证明。。。

引入定义:1、将一个有C(C>=2)种不同颜色顶点且相邻顶点颜色不同的凸N(N>=3)边形称为C色顶点相间N边形,记为Polygon(C,N)。

2、将Polygon(C,N1)通过不交叉的对角线划分为Polygon(C,N2)(N1>N2>=4)的过程称为C色顶点相间多边形的N2至N1划分,

记为Divide(C,N1_N2)。

那么就有:本题即求给定Polygon(3,N)的Divide(3,N_3)。

定理证明:1、当N=4时,显然对于任意的Polygon(3,N)总存在有Divide(3,N_3)。

2、令N=K时,对于任意的Polygon(3,K)总存在有Divide(3,K_3)。

因为任意一个Polygon(3,K+1)总可以通过一个Polygon(3,K)与一个Polygon(3,3)组合得到。

所以任意一个Polygon(3,K+1)总存在Divide(3,K+1_K),进而任意一个Polygon(3,K+1)总存在Divide(3,K+1_3)。

综合可得(数学归纳法):任意一个Polygon(3,N)总存在Divide(3,N_3)。

从而可知:解决问题的方法就是将Polygon(3,N)进行Divide(3,N_N-1)得到Polygon(3,N-1),循环直到N==3。

ByTheWay:Divide(3,N_N-1)的具体方法是找到连续的三个颜色各不相同的顶点,将边上的两个顶点的连线作为划分线。

当Polygon(3,N)的某个顶点颜色唯一时,由于Divide(3,N_N-1)后得到的Polygon(3,3)和Polygon(3,N-1)都必须包含此顶

点(不然哪还有三色),

所以此顶点必须位于划分线上,而其后的所有划分也如此。也就如同黑书所说,此时通过该颜色唯一的顶点向其他各顶点

做划分线即可。

举例说明:测试样例1的划分过程如下所示。

=======================================代码=======================================

#include<stdio.h>
#include<string.h>int N,RGBVS[3];                                            //Red Green Blue Vertice Sumstruct VERTICE { int colour; int next; } Memory[1005];void DivideAndConquer()
{int Cur=0;while((RGBVS[0]-1)&&(RGBVS[1]-1)&&(RGBVS[2]-1))        //循环直到某种颜色的顶点数为1{int Next1=Memory[Cur].next;                        //当前顶点的下一个顶点int Next2=Memory[Next1].next;                      //当前顶点的下下个顶点if(Memory[Cur].colour!=Memory[Next2].colour)       //在相邻顶点颜色不同的条件下,只需P与Next2颜色不同即得P,Next1,Next2颜色均不同{printf("%d %d\n",Cur+1,Next2+1);               //选取分割线(P,Next2)Memory[Cur].next=Next2;                        //删除顶点Next1--RGBVS[Memory[Next1].colour];                 //相应颜色的顶点数减少}Cur=Memory[Cur].next;}while(RGBVS[Memory[Cur].colour]-1)                     //找到颜色唯一的顶点{Cur=Memory[Cur].next;}while(1){int Next1=Memory[Cur].next;                        //当前顶点的下一个顶点int Next2=Memory[Next1].next;                      //当前顶点的下下个顶点if(Cur==Memory[Next2].next) { break; }             //循环直到多边形成为三角形printf("%d %d\n",Cur+1,Next2+1);                   //选取分割线(P,Next2)Memory[Cur].next=Next2;                            //删除顶点Next1}
}void ReaData()
{char Polygon[1005];scanf("%s",Polygon);memset(RGBVS,0,sizeof(RGBVS));for(int i=0;Polygon[i];++i){if(Polygon[i]=='R')      { Memory[i].colour=0; ++RGBVS[0]; }else if(Polygon[i]=='G') { Memory[i].colour=1; ++RGBVS[1]; }else if(Polygon[i]=='B') { Memory[i].colour=2; ++RGBVS[2]; }Memory[i].next=(i+1==N?0:i+1);}
}int main()
{while(scanf("%d%*c",&N)==1){ReaData();printf("%d\n",N-3);DivideAndConquer();}return 0;
}

[Virtual Judge]URAL1181:Cutting a Painted Polygon相关推荐

  1. URAL 1181 Cutting a Painted Polygon【递归+分治】

    题目: http://acm.timus.ru/problem.aspx?space=1&num=1181 http://acm.hust.edu.cn/vjudge/contest/view ...

  2. URAL 1181 Cutting a Painted Polygon(解题报告)

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

  3. Ural 1181 Cutting a Painted Polygon

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

  4. Ubuntu 14.04 下 Virtual Judge 的搭建

    前期准备工作 1.1 一个Linux系统 因为现场赛的缘故,我一直使用的都是ubuntu. 这里我测试用的是Ubuntu14.04 Desktop 64bit ,当然选择Server会更好一些. 系统 ...

  5. 2022/7/7/[CCSU Summer Training Contest 2 - Virtual Judge (vjudge.net)](https://vjudge.net/contest/49

    2022/7/7/CCSU Summer Training Contest 2 - Virtual Judge (vjudge.net)题解 A题:(https://atcoder.jp/contes ...

  6. 2022/7/4/题解[CCSU Summer Training Contest 1 - Virtual Judge (vjudge.net)](https://vjudge.net/contest/

    2022/7/4/题解CCSU Summer Training Contest 1 - Virtual Judge (vjudge.net) A题:Problem - 1594A - Codeforc ...

  7. Virtual Judge使用介绍

    Virtual Judge介绍 在上一篇文章中,我们介绍了许多Online Judge系统(在线判题系统).而Virtual Judge更像是一个OJ系统的集合,他通过爬取其他OJ的题目,让我们可以直 ...

  8. Virtual Judge 注册

    网址:Virtual Judgehttps://vjudge.net 搜索网址后,转到该界面. x 信息填写完成,点击 Register .

  9. 【Virtual Judge】The 2019 China Collegiate Programming Contest Harbin Site-Keeping Rabbits

    Keeping Rabbits DreamGrid is the keeper of n rabbits. Initially, the i-th (1≤i≤n) rabbit has a weigh ...

最新文章

  1. Python的re模块 --- 正则表达式操作
  2. 从TXT文本文档向Sql Server中批量导入数据
  3. vue接入萤石云_智能家居不香吗?萤石转型:或者臣妾做不到,或者费力不讨好...
  4. java进入下一个_在进入下一个循环迭代之前执行setTImeout操作
  5. 算法笔记--数列分块
  6. html的选择器child,css child选择器妙用
  7. 解决问题Can’t connect to local MySQL server through socket
  8. CentOS 下安装JDK
  9. 2. 怎么根据nagios报警做出调整的
  10. Join()--用法
  11. Kafka的常用命令(包括:下载安装、后台启动)
  12. react+ts 实现类组件 父子组件传值
  13. 怎么在WORD里给文字“框”起来,干货在这里,WORD文档中文字加边框的独家教程
  14. 库存盘点后的盘点差异解决方法
  15. matlab内置函数subs使用方法
  16. 从 WWDC17 看苹果图形技术的革新
  17. 计算机竖版桌面,电脑桌面竖屏了怎么办
  18. 【高精度】高精度除以高精度 C++题解
  19. 《每天五分钟冲击python基础之函数参数》(十八)
  20. PyCrypto安装和使用示例

热门文章

  1. Win10安装msi程序报错2503和2502错误解决方案
  2. FlexAir 开源版-多人视频聊天室,网络远程多人视频会议系统((Flex,Fms3联合开发))视频聊天,会议开发实战
  3. word文档信息提取的完整实例
  4. python中zeros用法_python中numpy.zeros(np.zeros)的使用方法
  5. python爬取股吧评论_神级的爬虫工程师用Python教你爬取全站股票评论!买哪只有底呢!...
  6. 8080端口被占用,关闭方法
  7. windows常用新建文件命令
  8. linux下制作windows u盘启动盘,Ubuntu下制作windows U盘启动盘
  9. MSSQL2005_x64位标准版安装问题解决整理
  10. Docker启动服务报错Job for docker.service failed because the control process exited with error code