CodeForces 626B
2016-03-07 22:06
211 查看
Description
Catherine has a deck of n cards, each of which is either red, green, or blue. As long as there are at least two cards left, she can do one of two actions:
take any two (not necessarily adjacent) cards with different colors and exchange them for a new card of the third color;
take any two (not necessarily adjacent) cards with the same color and exchange them for a new card with that color.
She repeats this process until there is only one card left. What are the possible colors for the final card?
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 200) — the total number of cards.
The next line contains a string s of length n — the colors of the cards. s contains
only the characters 'B', 'G', and 'R', representing blue, green, and red, respectively.
Output
Print a single string of up to three characters — the possible colors of the final card (using the same symbols as the input) in alphabetical order.
Sample Input
Input
Output
Input
Output
Input
Output
B
解体思路:分别统计出三种颜色的总个数,然后分成六种情况遍历一便,简单的bfs()
代码如下:
Catherine has a deck of n cards, each of which is either red, green, or blue. As long as there are at least two cards left, she can do one of two actions:
take any two (not necessarily adjacent) cards with different colors and exchange them for a new card of the third color;
take any two (not necessarily adjacent) cards with the same color and exchange them for a new card with that color.
She repeats this process until there is only one card left. What are the possible colors for the final card?
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 200) — the total number of cards.
The next line contains a string s of length n — the colors of the cards. s contains
only the characters 'B', 'G', and 'R', representing blue, green, and red, respectively.
Output
Print a single string of up to three characters — the possible colors of the final card (using the same symbols as the input) in alphabetical order.
Sample Input
Input
2 RB
Output
G
Input
3
GRG
Output
BR
Input
5 BBBBB
Output
B
解体思路:分别统计出三种颜色的总个数,然后分成六种情况遍历一便,简单的bfs()
代码如下:
#include<stdio.h> #include<queue> #include<string.h> #include<algorithm> using namespace std; struct stu{ int b,g,r;//分别代表不同字母的个数 }; stu star; int vist[202][202][202]; int num[5]; char s[5]; int cmp(char a , char b){ return a<b; } void bfs(){ int i,j,k; k=0; stu temp; memset(vist,0,sizeof(vist)); queue<stu>q; vist[star.b][star.g][star.r]=1; while(!q.empty()){ q.pop(); } q.push(star); while(!q.empty()){ star=q.front(); q.pop(); for(i=1;i<=6;i++){ if(i==1){ if(star.r>0&&star.g>0){ temp.r=star.r-1; temp.g=star.g-1; temp.b=star.b+1; } else continue; }//表示rg else if(i==2){ if(star.b>0&&star.g>0){ temp.b=star.b-1; temp.g=star.g-1; temp.r=star.r+1; } else continue; } //表示bg else if(i==3){ if(star.r>0&&star.b>0){ temp.r=star.r-1; temp.b=star.b-1; temp.g=star.g+1; } else continue; }//表示rb else if(i==4){ if(star.r>=2){ temp.r=star.r-1; temp.g=star.g; temp.b=star.b; } else continue; }//表示rr else if(i==5){ if(star.b>=2){ temp.b=star.b-1; temp.r=star.r; temp.g=star.g; } else continue; } //表示bb else if(i==6){ if(star.g>=2){ temp.g=star.g-1; temp.b=star.b; temp.r=star.r; } else continue; }//表示gg if(vist[temp.b][temp.g][temp.r])continue; if(temp.b+temp.g+temp.r==1){ if(temp.b==1){ s[k]='B'; } else if(temp.r==1){ s[k]='R'; } else s[k]='G'; k++; } else{ q.push(temp); } vist[temp.b][temp.g][temp.r]=1; } } sort(s,s+k,cmp); for(i=0;i<k;i++){ printf("%c",s[i]); } printf("\n"); } int main(){ int n,i; char c; while(scanf("%d",&n)!=EOF){ getchar(); num[1]=num[2]=num[3]=0; for(i=0;i<n;i++){ scanf("%c",&c); if(c=='B'){ num[1]++; } else if(c=='G'){ num[2]++; } else if(c=='R'){ num[3]++; } } if(n==1){ printf("%c\n",c); continue; } star.b=num[1]; star.g=num[2]; star.r=num[3]; bfs(); } return 0; }
相关文章推荐
- 100. Same Tree
- Python与硬件学习笔记:蓝牙(一)
- Javascript与ECMAScript
- 2016-03-07
- 第二周上机实践项目——项目4-用循环求
- 负数的二进制和十进制之间的转换
- socket tcp/ip udp通信
- 利用Runtime实现自动化归档
- Exynos4412 文件系统制作(一)—— 文件系统的启动过程分析
- 软件项目管理作业(一)
- 软件工程作业1
- android eclipse 下制作像素鸟
- HDU1850 Being a Good Boy in Spring Festival(NIM)
- HDU 1166 线段树(点更新)
- Pacific Trails Resort案例分析(第二章)
- keepalived入门
- [转]一份优秀的前端开发工程师简历是怎么样的?
- 怎么有效寻找电影资源
- 1037 - Agent 47(状压DP)
- SQL查询优化