10400 game show math
来源:互联网 发布:js classname赋值 编辑:程序博客网 时间:2024/06/06 03:18
第一次直接搜,20s的时限T了,没看题目说明。。。。。看了看别人的代码找到了一个很有效的剪枝。。。。1.835s过了。。。
legal函数是剪枝。
/**********************
* author:crazy_石头
* Pro:Problem H Game Show Math
* algorithm:dfs
* Time:1.835s
* Judge Status:Accepted
***********************/
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <set>
#include <vector>
using namespace std;
#define rep(i,h,n) for(int i=(h);i<=(n);i++)
const int maxn=100+5;
bool ok;
int n,ret;
int a[maxn];
bool vis[maxn][32000<<1+5];
string ans;
inline bool legal(int x,int y)
{
if (y>=-32000&&y<=32000&&!vis[x][y+32000])
{
vis[x][y+32000]=true;
return true;
}
return false;
}
inline void dfs(int cur,int num,string s)//cur是搜到的层数,num存储当前计算结果,s为运算符
{
if(ok)
return;
if(cur==n)
{
if(num==ret)
{
ok=1;
rep(i,0,n-2)
{
printf("%d%c",a[i],s[i]);
}
printf("%d=%d\n",a[n-1],ret);
}
return ;
}
else
{
if(legal(cur+1,num+a[cur+1]))
dfs(cur+1,num+a[cur+1],s+'+');
if(legal(cur+1,num-a[cur+1]))
dfs(cur+1,num-a[cur+1],s+'-');
if(legal(cur+1,num*a[cur+1]))
dfs(cur+1,num*a[cur+1],s+'*');
if(a[cur+1]!=0&&num%a[cur+1]==0&&legal(cur+1,num/a[cur+1]))
dfs(cur+1,num/a[cur+1],s+'/');
}
}
int main()
{
int test;
scanf("%d",&test);
while(test--)
{
memset(a,0,sizeof(a));
memset(vis,0,sizeof(vis));
scanf("%d",&n);
rep(i,0,n-1)
scanf("%d",&a[i]);
scanf("%d",&ret);
ok=0;
dfs(0,a[0],ans);
if(!ok)
printf("NO EXPRESSION\n");
}
return 0;
}
* author:crazy_石头
* Pro:Problem H Game Show Math
* algorithm:dfs
* Time:1.835s
* Judge Status:Accepted
***********************/
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <set>
#include <vector>
using namespace std;
#define rep(i,h,n) for(int i=(h);i<=(n);i++)
const int maxn=100+5;
bool ok;
int n,ret;
int a[maxn];
bool vis[maxn][32000<<1+5];
string ans;
inline bool legal(int x,int y)
{
if (y>=-32000&&y<=32000&&!vis[x][y+32000])
{
vis[x][y+32000]=true;
return true;
}
return false;
}
inline void dfs(int cur,int num,string s)//cur是搜到的层数,num存储当前计算结果,s为运算符
{
if(ok)
return;
if(cur==n)
{
if(num==ret)
{
ok=1;
rep(i,0,n-2)
{
printf("%d%c",a[i],s[i]);
}
printf("%d=%d\n",a[n-1],ret);
}
return ;
}
else
{
if(legal(cur+1,num+a[cur+1]))
dfs(cur+1,num+a[cur+1],s+'+');
if(legal(cur+1,num-a[cur+1]))
dfs(cur+1,num-a[cur+1],s+'-');
if(legal(cur+1,num*a[cur+1]))
dfs(cur+1,num*a[cur+1],s+'*');
if(a[cur+1]!=0&&num%a[cur+1]==0&&legal(cur+1,num/a[cur+1]))
dfs(cur+1,num/a[cur+1],s+'/');
}
}
int main()
{
int test;
scanf("%d",&test);
while(test--)
{
memset(a,0,sizeof(a));
memset(vis,0,sizeof(vis));
scanf("%d",&n);
rep(i,0,n-1)
scanf("%d",&a[i]);
scanf("%d",&ret);
ok=0;
dfs(0,a[0],ans);
if(!ok)
printf("NO EXPRESSION\n");
}
return 0;
}
* This source code was highlighted byYcdoiT. ( style: Desert )
- 10400 - Game Show Math
- 10400 game show math
- 10400 - Game Show Math
- 10400 -Game Show Math
- UVa 10400 - Game Show Math
- uva 10400 - Game Show Math
- uva 10400 - Game Show Math
- 10400 - Game Show Math------水题
- UVa 10400 - Game Show Math
- UVA 10400 - Game Show Math
- uva 10400 Game Show Math
- uva 10400 Game Show Math
- UVA 10400 Game Show Math
- uva 10400 Game Show Math
- UVa 10400 - Game Show Math
- uva 10400 - Game Show Math
- UVA - 10400 Game Show Math
- UVa 10400 - Game Show Math
- sudo 出现unable to resolve host 解决方法
- 图形化Git客户端大汇总
- ios 控件的动画隐藏
- 淘宝双十一销售额
- 网页访问时页面错误代码含义
- 10400 game show math
- 编译android4.0出现jdk版本问题
- C语言函数集(四)
- uml实现图
- windows下vim配置
- c/c++目标文件中_ZN****符号是什么意思
- fusionCharts 单系图实体类封装
- 模拟QQ(Socket编程)
- php操作xml