uva-10400-搜索

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

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 = 64002;
    constexpr int MAXN = 32000;
    constexpr int MINN = -32000;
    
     string NO = "NO EXPRESSION";
     constexpr int MAXP = 105;
     int vis[MAXP][N];
     int n;
    int number[MAXP+1];

     class  Node
     {
     public:
         int level;
         int curNum;
         int steps[MAXP+1];
     };
     queue<Node>q;
     
     int flag = 0;

     Node bfs()
     {
         while (!q.empty()) 
         {
             Node node = q.front();
             q.pop();
             int level = node.level;
             int curNum = node.curNum;
             int nextLevel = level+1;
             int nextNum = 0;
             for (int i=0;i<4;i++) 
             {
                 if (i == 0)
                 {
                     //+
                     nextNum = number[nextLevel] + curNum;

                 }
                 else if (i == 1)
                 {
                     //-
                     nextNum = curNum - number[nextLevel];

                 }
                 else if (i == 2)
                 {
                     //*
                     nextNum = curNum * number[nextLevel];
                 }
                 else if (i==3&&number[nextLevel]!=0&&curNum % number[nextLevel] == 0)
                 {
                     // /
                     nextNum = curNum / number[nextLevel];
                 }
                 if (nextNum > MAXN || nextNum < MINN)
                     continue;
                 if (vis[nextLevel][nextNum + MAXN] == 1)
                     continue;
                 vis[nextLevel][nextNum + MAXN] = 1;
                 Node newNode;
                 newNode.curNum = nextNum;
                 newNode.level = nextLevel;
                 memcpy(&(newNode.steps),&(node.steps),sizeof(node.steps));
                 newNode.steps[nextLevel] = i;
                 if (nextLevel == n - 2)
                 {
                     //final number
                     if (number[n-1] == nextNum) 
                     {
                         flag = 1;
                         return newNode;
                     }
                     continue;
                 }
                 q.push(newNode);
             }
         
         }
         Node errorNode;
         errorNode.curNum = 0;
         return errorNode;
     
     }

    void solve()
    {
        int cases;
        cin >> cases;
        while (cases--) 
        {
            cin >> n;
            flag = 0;
            while (!q.empty())
                q.pop();
            memset(vis,0,sizeof(vis));
            n++;
            for (int i=0;i<n;i++) 
            {
                cin >> number[i];
            }
            if (n==2) 
            {
                if (number[0] == number[1])
                    cout << number[0] << "=" << number[1] << endl;
                else
                    cout << NO << endl;
                continue;
            }
            Node node;
            node.curNum = number[0];
            node.level = 0;
            node.steps[node.level] = -1;
            q.push(node);
            node = bfs();
            if (flag == 0)
            {
                cout << NO << endl;
                continue;
            }
            cout << number[0];
            for (int i=1;i<n-1;i++) 
            {
                if (node.steps[i] == 0)
                    cout << "+";
                else if (node.steps[i] == 1)
                    cout << "-";
                else if (node.steps[i] == 2)
                    cout << "*";
                else
                    cout << "/";
                cout << number[i];
            }
            cout << "=" << number[n - 1] << endl;
            
        }
    
    }

};


int main()
{

#ifndef ONLINE_JUDGE
    freopen("d://1.text", "r", stdin);
#endif // !ONLINE_JUDGE
    cc::solve();

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/shuiyonglewodezzzzz/p/10293373.html
今日推荐