劝退二题【观光公交+华容道】

版权声明: https://blog.csdn.net/qq_38234381/article/details/82817119

这两道题其实并没有什么关系,只是碰巧一起做到了。(之后就被劝退了)

观光公交:https://www.luogu.org/problemnew/show/P1315

看到题目后没什么思路,后来看了syc大佬的博客之后终于有了思路,然而却只拿了10分,之后调了很久也没调出来。

不知道哪错的10分代码:

#include<iostream>
#include<cstring>
using namespace std;

const int N=1e3+10, M=1e4+10, INF=0x3f3f3f3f;
struct passenger {
    int get_time, from, to;
} p[M];
int n, m, k, ans, dist[N];
int get_time[N], leave_time[N], last_get[N], influence[N], get_off[N];

void init() {
    int i;
    get_time[1]=0;
    leave_time[1]=last_get[1];
    for (i=2; i<=n; i++) {
        get_time[i]=leave_time[i-1]+dist[i];
        leave_time[i]=max(get_time[i], last_get[i]);
    }
    return ;
}

int main() {
    int i;
    cin >> n >> m >> k;
    for (i=2; i<=n; i++)
        cin >> dist[i];
    for (i=1; i<=m; i++) {
        cin >> p[i].get_time >> p[i].from >> p[i].to;
        last_get[p[i].from]=max(last_get[p[i].from], p[i].get_time);
        get_off[p[i].to]++;
    }
    for (i=1; i<=n; i++)
        get_off[i]+=get_off[i-1];
    init();
    for (i=1; i<=m; i++)
        ans+=get_time[p[i].to]-p[i].get_time;
    while (k--) {
        int i, tmp, now_max=-INF;
        influence[n]=n;
        for (i=n-1; i>=2; i--) {
            if (dist[i]==0) influence[i]=0;
            else influence[i]=(last_get[i]<get_time[i])?influence[i]:i;	
        }
        influence[1]=0;
        for (i=2; i<=n; i++) {
            if (get_off[influence[i]]-get_off[i-1]>now_max) {
                now_max=get_off[influence[i]]-get_off[i-1];
                tmp=i;
            }
        }
        dist[tmp]--;
        ans-=now_max;
        init();
    }
    cout << ans;
    return 0;
}

附上SYC大佬的博客地址:https://www.cnblogs.com/SYCs

总结:这种题主要靠乱搞,还要注意建一些不同含义的数组,利于构思和实现。

还要注意从一些公式上分析问题,确定目标。

华容道:https://www.luogu.org/problemnew/show/P1979

70分的bfs还是很好写的,但AC算法不会搞。

code:

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;

const int N = 31;
struct state{
    int ex, ey, sx, sy, d;
};
queue<state> Q;
int n, m, q, ex, ey, sx, sy, tx, ty, ans, map[N][N], vis[N][N][N][N], dx[4]={1, -1, 0, 0}, dy[4]={0, 0, 1, -1};

void bfs(int ex, int ey, int sx, int sy) {
    int i;
    Q.push(state{ex, ey, sx, sy, 0});
    vis[ex][ey][sx][sy]=true;
    while (!Q.empty()) {
        state tmp=Q.front();
        Q.pop();
        int x1=tmp.ex, y1=tmp.ey, x2=tmp.sx, y2=tmp.sy, d=tmp.d;
        if (x2==tx && y2==ty) {
            ans=d;
            break;
        }
        for (i=0; i<4; i++) {
            int x=x1+dx[i], y=y1+dy[i];  
            if (x>n || x<1 || y<1 || y>m || map[x][y]==0) continue;
            if (x==x2 && y==y2) {
                if (vis[x][y][x1][y1]) continue;
                Q.push(state{x, y, x1, y1, d+1});
                vis[x][y][x1][y1]=true;
            }
            else {
                if (vis[x][y][x2][y2]) continue;
                Q.push(state{x, y, x2, y2, d+1});
                vis[x][y][x2][y2]=true;
            }
        }
    }
    return ;
}

int main() {
    int i, j;
    cin >> n >> m >> q;
    for (i=1; i<=n; i++)
        for (j=1; j<=m; j++)
            cin >> map[i][j];
    while (q--) {
        while (!Q.empty()) Q.pop();
        memset(vis, false, sizeof(vis));
        ans=-1;
        cin >> ex >> ey >> sx >> sy >> tx >> ty;
        bfs(ex, ey, sx, sy);
        cout << ans << endl;
    }
    return 0;
}

总结:这道题dfs只能得45,bfs有时候确实比dfs无论速度还是思维的难度都要好些。

猜你喜欢

转载自blog.csdn.net/qq_38234381/article/details/82817119
今日推荐