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

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. admin 后台

    https://segmentfault.com/a/1190000015835976#articleHeader3https://github.com/PanJiaChen/vue-element- ...

  2. js实现上拉加载思路整理

    1.整体模拟滚动 监听touchmove事件,比较 scrollTop 和 $scroller.scrollHeight() - $container.height(). 缺点:滑动不流畅, tran ...

  3. JavaScript之循环

    我是昨天的小尾巴...https://blog.csdn.net/weixin_42217154/article/details/81182817 3.2 循环结构 循环结构是指在程序中需要反复执行某 ...

  4. Is It Always a Good Idea to Reach Outside Your Comfort Zone?

    Learning to stretch outside your comfort zone is critical for learning and growing, advancing in you ...

  5. __FILES__

    _FILE_ :被称为PHP魔术常量 ,返回当前执行PHP脚本的完整路径和文件名,包含一个绝对路径 1)dirname(__FILE___) 函数返回的是脚本所在在的路径.   比如文件 b.php ...

  6. 样本失衡会对SVM的影响

    假设正类样本远多于负类 1.线性可分的情况 假设真实数据集如下: 由于负类样本量太少,可能会出现下面这种情况 使得分隔超平面偏向负类.严格意义上,这种样本不平衡不是因为样本数量的问题,而是因为边界点发 ...

  7. Maven用途

    1.使用Maven编译项目,命令是:“mvncompile” 在命令行中,进入pom.xml所在目录,输入命令即可. 2.使用Maven清理项目,命令是:“mvnclean” 3.使用Maven测试项 ...

  8. 启动Kernel提示Bad Data CRC

    如上图,我明明将uImage正确写入到里nandflash里面,但启动但时候就是提示bad CRC. 后来我手动执行nand read kernel想看看是不是环境变量里面的命令执行有问题,意外但被我 ...

  9. MySQL Error--The Table is full

    问题描述 在MySQL 错误日志中发下以下错误信息:[ERROR] /export/servers/mysql/bin/mysqld: The table '#sql-xxxx-xxx' is ful ...

  10. insert 插入

    自动关联当前时间: GETDATE():返回当前时间和日期.