题意:题意很简单,就是输入数字,对数字进行加减乘除,然后能不能得到最后的数字.

wa了很多次,memcpy(dst,src,sizeof(dst))才对,最后一个参数写错,最后一个参数是要拷贝的字节数目.

跑了5s多,这应该是我提交的代码运行最长的一次.

#include <string>
#include<iostream>
#include<map>
#include<memory.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<math.h>
#include<iomanip>
#include<bitset>
#include"math.h"
namespace cc
{
using std::cout;
using std::endl;
using std::cin;
using std::map;
using std::vector;
using std::string;
using std::sort;
using std::priority_queue;
using std::greater;
using std::vector;
using std::swap;
using std::stack;
using std::queue;
using std::bitset; constexpr int N = ;
constexpr int MAXN = ;
constexpr int MINN = -; string NO = "NO EXPRESSION";
constexpr int MAXP = ;
int vis[MAXP][N];
int n;
int number[MAXP+]; class Node
{
public:
int level;
int curNum;
int steps[MAXP+];
};
queue<Node>q; int flag = ; Node bfs()
{
while (!q.empty())
{
Node node = q.front();
q.pop();
int level = node.level;
int curNum = node.curNum;
int nextLevel = level+;
int nextNum = ;
for (int i=;i<;i++)
{
if (i == )
{
//+
nextNum = number[nextLevel] + curNum; }
else if (i == )
{
//-
nextNum = curNum - number[nextLevel]; }
else if (i == )
{
//*
nextNum = curNum * number[nextLevel];
}
else if (i==&&number[nextLevel]!=&&curNum % number[nextLevel] == )
{
// /
nextNum = curNum / number[nextLevel];
}
if (nextNum > MAXN || nextNum < MINN)
continue;
if (vis[nextLevel][nextNum + MAXN] == )
continue;
vis[nextLevel][nextNum + MAXN] = ;
Node newNode;
newNode.curNum = nextNum;
newNode.level = nextLevel;
memcpy(&(newNode.steps),&(node.steps),sizeof(node.steps));
newNode.steps[nextLevel] = i;
if (nextLevel == n - )
{
//final number
if (number[n-] == nextNum)
{
flag = ;
return newNode;
}
continue;
}
q.push(newNode);
} }
Node errorNode;
errorNode.curNum = ;
return errorNode; } void solve()
{
int cases;
cin >> cases;
while (cases--)
{
cin >> n;
flag = ;
while (!q.empty())
q.pop();
memset(vis,,sizeof(vis));
n++;
for (int i=;i<n;i++)
{
cin >> number[i];
}
if (n==)
{
if (number[] == number[])
cout << number[] << "=" << number[] << endl;
else
cout << NO << endl;
continue;
}
Node node;
node.curNum = number[];
node.level = ;
node.steps[node.level] = -;
q.push(node);
node = bfs();
if (flag == )
{
cout << NO << endl;
continue;
}
cout << number[];
for (int i=;i<n-;i++)
{
if (node.steps[i] == )
cout << "+";
else if (node.steps[i] == )
cout << "-";
else if (node.steps[i] == )
cout << "*";
else
cout << "/";
cout << number[i];
}
cout << "=" << number[n - ] << endl; } } }; int main()
{ #ifndef ONLINE_JUDGE
freopen("d://1.text", "r", stdin);
#endif // !ONLINE_JUDGE
cc::solve(); return ;
}

uva-10400-搜索的更多相关文章

  1. UVA 10400 Game Show Math (dfs + 记忆化搜索)

    Problem H Game Show Math Input: standard input Output: standard output Time Limit: 15 seconds A game ...

  2. UVa 10400 记忆化搜索

    #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> us ...

  3. UVa 10400 - Game Show Math

    题目大意:给出n(n<100)个正整数和一个目标数,按照给出数的顺序,运用+.-.*./四则运算(不考虑优先级),判断能否得出所要的结果. 首先考虑的就是暴力枚举,不过时间复杂度为O(4n),会 ...

  4. UVa 10400 - Game Show Math 游戏中的数学 dfs+判重

    题意:给出一些数字和一个目标数字,要求你在数字间添加+-*/,让表达式能达到目标数字,运算符号的优先级都是一样的. 由于数据量很大,本来想用map<string>判重的,结果还是超时了,然 ...

  5. uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)

    题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...

  6. UVA 707 - Robbery(内存搜索)

    UVA 707 - Robbery 题目链接 题意:在一个w * h的图上.t个时刻,然后知道一些信息,每一个时刻没有小偷的矩阵位置,问哪些时刻能够唯一确定小偷位置.和确定小偷是否已经逃走,假设没逃走 ...

  7. UVA - 10118Free Candies(记忆化搜索)

    题目:UVA - 10118Free Candies(记忆化搜索) 题目大意:给你四堆糖果,每一个糖果都有颜色.每次你都仅仅能拿随意一堆最上面的糖果,放到自己的篮子里.假设有两个糖果颜色同样的话,就行 ...

  8. UVA - 10917 - Walk Through the Forest(最短路+记忆化搜索)

    Problem    UVA - 10917 - Walk Through the Forest Time Limit: 3000 mSec Problem Description Jimmy exp ...

  9. POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索)

    POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索) Description You ar ...

  10. UVa 10285 Longest Run on a Snowboard - 记忆化搜索

    记忆化搜索,完事... Code /** * UVa * Problem#10285 * Accepted * Time:0ms */ #include<iostream> #includ ...

随机推荐

  1. Jmeter处理返回结果的值

    接口测试中,获取返回结果的值可以用插件JSON Path Extractor 此插件可以直接处理json,通过key来取值. 该插件下载地址为:http://jmeter-plugins.org/wi ...

  2. vue 使用vux封装的微信分享

    main.js引入 import {WechatPlugin} from 'vux' Vue.use(WechatPlugin) 公共的jswxShare.js import Vue from 'vu ...

  3. MYSQL数据模型

    DROP TABLE IF EXISTS `sh_category`; CREATE TABLE `sh_category` ( `id` int(11) NOT NULL AUTO_INCREMEN ...

  4. Dilated Convolutions 空洞卷积

    Dilated Convolutions,中文一般称为空洞卷积或者扩张卷积,是一种改进的图像卷积方法. 扩张卷积工作示意图如下: 图a是普通的卷积,感受野是3*3,相当于扩充dilation=0 图b ...

  5. 【Java】字节数组转换工具类

    import org.apache.commons.lang.ArrayUtils; import java.nio.charset.Charset; /** * 字节数组转换工具类 */ publi ...

  6. 4.python字符串格式化

    格式化字符串时,Python使用一个字符串作为模板.模板中有格式符,这些格式符为真实值预留位置,并说明真实数值应该呈现的格式.Python用一个tuple将多个值传递给模板,每个值对应一个格式符.py ...

  7. C++学习(三十八)(C语言部分)之 排序(冒泡 选择 插入 快排)

    算法是解决一类问题的方法排序算法 根据元素大小关系排序 从小到大 从大到小冒泡 选择 插入 快排希尔排序 归并排序 堆排序 冒泡排序 从头到尾比较 每一轮将最大的数沉底 或者最小数字上浮 选择排序 1 ...

  8. 学习笔记TF045:人工智能、深度学习、TensorFlow、比赛、公司

    人工智能,用计算机实现人类智能.机器通过大量训练数据训练,程序不断自我学习.修正训练模型.模型本质,一堆参数,描述业务特点.机器学习和深度学习(结合深度神经网络). 传统计算机器下棋,贪婪算法,Alp ...

  9. 关于ava容器、队列,知识点总结

    推荐<java 并发编程实战>这本书,中文的翻译有些差(哈哈,并发确实难,不好翻译),适合有并发经验的人来读. 这篇短文,整理了容器的知识点,对容器的使用场景,容器的原理等有个整体的认知! ...

  10. NN 激活函数 待修改

    Softmax 函数/算法 https://www.zhihu.com/question/23765351 RELU 激活函数及其他相关的函数 http://blog.csdn.net/u013146 ...