标签:
如果最后的运算结果可以等于23,输出Yes,不能的话输出No 1 1 1 1 1 1 2 3 4 5 2 3 5 7 11
No Yes Yes 比赛的时候弱都没看出用BFS,看的提交的人数也不多,就没有再审题。。。- -! 赛后知道了用BFS,但写的时候也遇到了困难,刚开始的时候直接先传进去了per[0],但是感觉不对,总是第一个数和其他的数进行,运算 并且,标记的话也是,有问题的。问了同学才知道,要,每个数和其他的未标记的数进行运算。并且将,计算完的数进行标记。#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> #include<math.h> #include<queue> #define LL long long #define inf 0x3f3f3f3f using namespace std; int per[6]; bool vis[6]; struct node { int sum,z; bool vis[6]; }; bool bfs(int pos) { queue<node>q; while(!q.empty()) q.pop(); node f1,f2; f1.z=1; f1.sum=per[pos]; for(int i=0;i<5;i++) f1.vis[i]=false; f1.vis[pos]=true; q.push(f1); while(!q.empty()) { f1=q.front(); q.pop(); if(f1.sum==23&&f1.z==5)//注意所有的数必须都用到 { return true; } int i,j; for(i=0;i<5;i++) { for(j=0;j<3;j++) { f2=f1; if(!f2.vis[i]) { if(j==0) { f2.sum+=per[i]; f2.z++; f2.vis[i]=true; q.push(f2); } else if(j==1) { f2.sum-=per[i]; f2.vis[i]=true; f2.z++; q.push(f2); } else { f2.sum*=per[i]; f2.vis[i]=true; f2.z++; q.push(f2); } } } } } return false; } int main() { int n,m,i,j,k; while(~scanf("%d",&per[0])) { bool bj=false; for(i=1;i<5;i++) scanf("%d",&per[i]); for(i=0;i<5;i++) { bool p=bfs(i); if(p) { bj=true; break; } } if(bj) printf("Yes\n"); else printf("No\n"); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/grit_icpc/article/details/48085649