Ural 1181 cutting a painted polygon [recursion + partitioning]

Source: Internet
Author: User
Question: http://acm.timus.ru/problem.aspx? Space = 1 & num = 1181 http://acm.hust.edu.cn/vjudge/contest/view.action? Cid = 27048 # Problem/a1181. cutting a painted polygontime limit: 1.0 second
Memory limit: 64 mbthere 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 wowould have all vertices of different colors: one red, one green and one blue vertex. point out a possible way of the cutting if
Cutting is possible. inputthe first line contains a number NOf the polygon vertices (4≤ N≤ 1000). There are NSymbols of the set {'R', 'G', 'B'} in the second line that specify a color for the correspondent
Vertex. outputthe first line shocould 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 shocould
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
7RBGBRGB
41 33 75 75 3

Reprinted from: cxb: http://blog.csdn.net/cxb569262726/article/details/7845369

/*************************************** * ********** Aaccepted144 kb31 msvisual C ++ 20103410 B: here is a multi-deformation with N (4 <n <1000) points. Each point has a color 'R', 'G', and 'B '. ask if you can divide this polygon into N-3 triangles so that the vertices of the triangle are colored differently. if no: Output 0 otherwise: Output N-3 and these edge algorithms: recursion + divide the idea: see the first chapter of the black book when each color remaining points> 1 time: find the three adjacent points of different colors to form a triangle that matches the meaning of the question, delete the intermediate point, and then find the new polygon. Once the point is deleted, only one point is left after the point is deleted, then, divide them from the left, and then from the right. **************************************** * *******/# 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 ++;}/** the color of two adjacent points is not the same. */If (r = 1) {for (INT I = 0; I <Len; I ++) {if (a [I] = 'R') {/** three points for a triangle */For (Int J = I + 2; j <= (I = 0? Len-2: len-1); j ++) {printf ("% d \ n", index [I], index [J]);} For (Int J = I-2; j> = (I = (len-1 )? 1: 0); j --) {printf ("% 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 \ n", index [I], index [J]);} For (Int J = I-2; j> = (I = (len-1 )? 1: 0); j --) {printf ("% 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 \ n", index [I], index [J]);} For (Int J = I-2; j> = (I = (len-1 )? 1: 0); j --) {printf ("% 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 \ n", index [I], index [I + 2]); for (Int J = I + 1; j <len-1; j ++)/** Delete vertex I + 1 */{A [J] = A [J + 1]; index [J] = index [J + 1];} A [len-1] = '\ 0';/** note */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]) /** data water, no such situation */{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;/** Record Number */} If (r = 0 | G = 0 | B = 0) /** data water, no such situation */{printf ("0 \ n"); continue;} For (INT I = 0; I <n-1; I ++) /** data water, no such situation */{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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.