uestc oj 1276 Chiara’s “Beiju”
来源:互联网 发布:原版优化9拜占庭 编辑:程序博客网 时间:2024/04/28 15:22
Chiara’s “Beiju”
这一题采用广度优先搜索解题,由于杯子没有顺序,只需要关注的是向上杯子的数量,那么的话也就一共10000种情况,直接广度
搜索就行了。向上的杯子记为1,在每一次翻转杯子之前,要注意求出可以翻转杯子的种数,要满足三个条件
1,向上翻转不能超过现在可以向上杯子的数量
2. 向下翻转的不能超过可以向下翻转的杯子的数量
3。翻转之后的情况不能在广搜队列中出现过,以保证都是最段路径
当num达到n时 step就是所求结果。
否则输出Poor Girl
这一题采用广度优先搜索解题,由于杯子没有顺序,只需要关注的是向上杯子的数量,那么的话也就一共10000种情况,直接广度
搜索就行了。向上的杯子记为1,在每一次翻转杯子之前,要注意求出可以翻转杯子的种数,要满足三个条件
1,向上翻转不能超过现在可以向上杯子的数量
2. 向下翻转的不能超过可以向下翻转的杯子的数量
3。翻转之后的情况不能在广搜队列中出现过,以保证都是最段路径
当num达到n时 step就是所求结果。
否则输出Poor Girl
#include<cstdio>#include<cstring>#define N 10005int vis[N];int T,n,m,num,k,head,tail;struct node{ int num; int step;}que[N];int main(){ //freopen("1.txt","r",stdin); scanf("%d",&T); while(T--) { int flag = 0; memset(vis,0,sizeof(vis)); num = 0; scanf("%d\n%d",&n,&m); for(int i = 0;i<n;i++) { scanf("%d ",&k); num+=k; } tail = head = 0; que[tail].num = num; que[tail++].step = 0; vis[num]=1; while(head < tail) { node front = que[head]; head++; if(front.num==0) { printf("%d\n",front.step); flag = 1; break; } int min; if(front.num<m) { min = front.num; } else min = m; for(int i = 0;i<=min;i++) { if(n-front.num>=m-i&&!vis[front.num+m-i-i]) { node new_node; new_node.num = front.num+m-i-i; new_node.step = front.step+1; que[tail++]=new_node; vis[new_node.num]=1; } } } if(!flag) { printf("Poor Girl\n"); } } return 0;}
- uestc oj 1276 Chiara’s “Beiju”
- uestc oj 1793 Kalindrome
- uestc oj 1227 Hotel
- uestc oj 1500 AreYouBusy
- uestc oj 1484 STAMPS
- uestc oj 1272 Sticks
- uestc oj 1230 Picture
- uestc oj 1223 Islands
- uestc oj 1001 DotNotation
- 吐槽 uestc oj
- uestc oj 1876 聚会
- UESTC OJ 1830
- UESTC OJ Windy 数
- BeiJu
- uestc oj 1796 Flash Mob
- uestc oj 1221 Sliding Window
- uestc oj 1868 Condorcet Winners
- uestc oj 1690 最少花费
- uestc oj 1490 Eight Puzzle
- C 内存分配(转载)
- uestc oj 1269 Children of the Candy Corn
- uestc oj 1002 解救小Q
- Struts2 contentType属性列表
- uestc oj 1276 Chiara’s “Beiju”
- Android倚天剑之Notification之亮剑IOS
- 扑克牌24点游戏JAVA实现
- uestc oj 1272 Sticks
- uestc oj 1488 Flip Game
- struts2文件下载出现Can not find a java.io.InputStream with the name [imageStream] in the invocation stack.
- uestc oj 1670 积木
- java 对一单词的找出全部组合
- uestc oj 1230 Picture