两层暴力搜索为什么会WA?
查看原帖
两层暴力搜索为什么会WA?
28096
北海若楼主2017/7/8 17:21

TLE我能理解,为什么第一个点WA了……

'''

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
int map[10][10] = {{0}};
int nmap[10][10] = {{0}};
int n;
int maxv = -1;
int val = 0;
struct point
{
    int px, py;
    void move(int dir)
    {
        px += dir;
        py += (1 - dir);
    }
    void moveback(int dir)
    {
        px -= dir;
        py -= (1 - dir);
    }
};
point pos;
point pos2;
vector<point> route = vector<point>();
vector<point> mroute;
void reset()
{
    for (int x=0; x<10; x++)
    {
        for (int y=0; y<10; y++)
        {
            nmap[x][y] = map[x][y];
        }
    }
}
void sch2()
{
    for (int i=0; i<2; i++)
    {
        pos2.move(i);
        //cout<<pos.px<<" "<< pos.py <<endl;
        if (!(pos2.px > n || pos2.py > n))
        {
            val += nmap[pos2.px][pos2.py];
            //nmap[pos.px][pos.py] = 0;
            if ((pos2.px == n) && (pos2.py == n))
            {
                //cout<<val<<endl;
                if (val > maxv)
                {
                    maxv = val;
                }
            }
            else
            {
                sch2();
            }
            //nmap[pos.px][pos.py] = dV;
            val -= nmap[pos2.px][pos2.py];
            //cout<<pos.px<<"R"<<pos.py<<endl;
            pos2.moveback(i);
        }
        else pos2.moveback(i);
    }
}
void sch()
{
    for (int i=0; i<2; i++)
    {
        pos.move(i);
        //cout<<pos.px<<" "<< pos.py <<endl;
        if (!(pos.px > n || pos.py > n))
        {
            route.push_back(pos);
            val += nmap[pos.px][pos.py];
            int dV = nmap[pos.px][pos.py];
            //nmap[pos.px][pos.py] = 0;
            if ((pos.px == n) && (pos.py == n))
            {
                //cout<<val<<endl;
                reset();
                for (int i=0; i<route.size(); i++)
                {
                    nmap[route[i].px][route[i].py] = 0;
                }
                pos2.px = pos2.py = 1;
                sch2();
            }
            else
            {
                sch();
            }
            //nmap[pos.px][pos.py] = dV;
            val -= dV;
            //cout<<pos.px<<"R"<<pos.py<<endl;
            pos.moveback(i);
            route.pop_back(); 
        }
        else pos.moveback(i);
    }
}
int main()
{
    //freopen("1004.in", "r", stdin);
    //freopen("1004.txt", "w", stdout);
    cin >> n;
    int x = -1, y = -1, num = -1;
    while ((x != 0) || (y != 0) || (num != 0))
    {
        scanf("%d%d%d", &x, &y, &num);
        map[x][y] = num;
    }
    reset();
    val = 0;
    pos.px = pos.py = 1;
    sch();
    cout<<maxv;
    return 0;
}
'''
2017/7/8 17:21
加载中...