牛客小白月赛 22 D.收集纸片

题目连接

https://ac.nowcoder.com/acm/contest/4462/D

一开始还想的排序什么鬼,并不是,再一看数据范围,爆搜吧~但是调试了不少时间,码力码力码力!!!

如果可以的话,以后会写一下状态压缩版本,还一点不会。。

更新一下 ,这个代码虽然ac了,我感觉有错误的地方,应该是数据不太行。在这个搜索中我默认拾完最后一个纸片距离最短然后记录最后一个点,再用这个搜到的最短距离+记录的最后一个点和起始点的距离就是答案,明显有问题。

#include <iostream>
#include <malloc.h>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#define IO \
    // ios::sync_with_stdio(false); \
    // cin.tie(0);                  \
    // cout.tie(0);
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
struct Node
{
    int x, y;
} node[maxn];
int vis[maxn];
int sx, sy;
int f;
int ans = 0;
int t = 0;
int T, n, m, k;
void dfs(int s, int n)
{
    if (n == 0)
    {
        if (t < ans)
        {
            f = s;
            ans = t;
            return;
        }
        return;
    }
    for (int i = 1; i <= k; i++)
        if (!vis[i])
        {
            vis[i] = 1;
            t += abs(node[i].x - node[s].x) + abs(node[i].y - node[s].y);
            dfs(i, n - 1);
            t -= abs(node[i].x - node[s].x) + abs(node[i].y - node[s].y);
            vis[i] = 0;
        }
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
#endif
    IO;
    cin >> T;
    while (T--)
    {
        cin >> n >> m;
        cin >> node[0].x >> node[0].y;
        cin >> k;
        for (int i = 1; i <= k; i++)
            cin >> node[i].x >> node[i].y;
        t = 0;
        ans = inf;
        memset(vis, 0, sizeof vis);
        dfs(0, k);
        // cout << f << endl;
        ans += abs(node[f].x - node[0].x) + abs(node[f].y - node[0].y);
        cout << "The shortest path has length " << ans << endl;
    }
    return 0;
}

正解

#include <iostream>
#include <malloc.h>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#define IO \
    // ios::sync_with_stdio(false); \
    // cin.tie(0);                  \
    // cout.tie(0);
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
struct Node
{
    int x, y;
} node[maxn];
int vis[maxn];
int sx, sy;
int f;
int ans = 0;
int t = 0;
int T, n, m, k;
void dfs(int s, int n)
{
    if (n == 0)
    {
        t += abs(node[s].x - node[0].x) + abs(node[s].y - node[0].y);
        if (t < ans)
        {
            f = s;
            ans = t;
            t -= abs(node[s].x - node[0].x) + abs(node[s].y - node[0].y);
            return;
        }
        t -= abs(node[s].x - node[0].x) + abs(node[s].y - node[0].y);
        return;
    }
    for (int i = 1; i <= k; i++)
        if (!vis[i])
        {
            vis[i] = 1;
            t += abs(node[i].x - node[s].x) + abs(node[i].y - node[s].y);
            dfs(i, n - 1);
            t -= abs(node[i].x - node[s].x) + abs(node[i].y - node[s].y);
            vis[i] = 0;
        }
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
#endif
    IO;
    cin >> T;
    while (T--)
    {
        cin >> n >> m;
        cin >> node[0].x >> node[0].y;
        cin >> k;
        for (int i = 1; i <= k; i++)
            cin >> node[i].x >> node[i].y;
        t = 0;
        ans = inf;
        memset(vis, 0, sizeof vis);
        dfs(0, k);
        // cout << f << endl;
        // ans += abs(node[f].x - node[0].x) + abs(node[f].y - node[0].y);
        cout << "The shortest path has length " << ans << endl;
    }
    return 0;
}
发布了81 篇原创文章 · 获赞 29 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_44115065/article/details/104453121