hdu 1495 非常可乐 (bfs)

小记:这题给我留下了一个TLE的印记,之前因为用的标记数组是int型的,导致了一直TLE,唉,memset一个int型数组浪费的时间真多,不然我之前的那个没用结构体的直接bfs的应该可以A过去,唉,又,b了。一般在写代码前都会先分析自己的算法的时间复杂度的大小,而觉得可以的,动手写了之后还是超时,要么就是你的想法错了,要么就是你的优化剪枝不够,而对于常数级优化 则是没必要的。谨记。。。


题解:题目要求 n + m == s 那么每一次的倒可乐就可以从这一杯倒到另外两杯去,而有三个杯子因此有6种倒的方法。即相当于一颗六叉树。bfs 搜索每一种状态,当有两个杯子是s的一半的时候,那个状态就是答案,输出即可。已搜过的进行标记。

剪枝:若s 为奇数,则无解,分不了相等的两半,因为你不能倒出一个0.5的状态值。

优化:标记数组设为bool型。我的设int型,156MS,设bool型,31MS。


代码奉上:

#include <stdio.h>
#include <string.h>
#include <queue>

using namespace std;
#define MAX 101

struct NODE {
    int x,y,z,t;
};


bool visited[MAX][MAX][MAX];


void bfs(int s,int n,int m) {
    queue<NODE>q;
    NODE now, next;
    now.x = s;
    now.y = 0;
    now.z = 0;
    now.t = 0;
    q.push(now);
    memset(visited,0,sizeof(visited));
    while(!q.empty()) {
        now = q.front();
        q.pop();
        if((now.x == s>>1 && now.y == s>>1) || (now.x == s>>1 && now.z == s>>1) || (now.z == s>>1 && now.y == s>>1)){
            printf("%d\n",now.t);
            return ;
        }

        if(now.x) {
            if(now.x > (n - now.y)){
                next.x = now.x - n + now.y;
                next.y = n;
                next.z = now.z;
            }
            else {
                next.x = 0;
                next.y = now.y + now.x;
                next.z = now.z;
            }

            if(!visited[next.x][next.y][next.z]) {
                visited[next.x][next.y][next.z] = 1;
                next.t = now.t + 1;
                q.push(next);
            }

            if(now.x > (m - now.z)){
                next.x = now.x - m + now.z;
                next.z = m;
                next.y = now.y;
            }
            else {
                next.x = 0;
                next.z = now.z+ now.x;
                next.y = now.y;
            }
            if(!visited[next.x][next.y][next.z]) {
                visited[next.x][next.y][next.z] = 1;
                next.t = now.t + 1;
                q.push(next);
            }
        }

        if(now.y) {
            if(now.y > (s - now.x)){
                next.y = now.y - s + now.x;
                next.x = s;
                next.z = now.z;
            }
            else {
                next.y = 0;
                next.x = now.y+ now.x;
                next.z = now.z;
            }
            if(!visited[next.x][next.y][next.z]) {
                visited[next.x][next.y][next.z] = 1;
                next.t = now.t + 1;
                q.push(next);
            }

            if(now.y > (m - now.z)){
                next.y = now.y - m + now.z;
                next.z = m;
                next.x = now.x;
            }
            else {
                next.y = 0;
                next.z = now.z+ now.y;
                next.x = now.x;
            }
            if(!visited[next.x][next.y][next.z]) {
                visited[next.x][next.y][next.z] = 1;
                next.t = now.t + 1;
                q.push(next);
            }
        }

        if(now.z) {
            if(now.z > (s - now.x)){
                next.z = now.z - s + now.x;
                next.x = s;
                next.y = now.y;
            }
            else {
                next.z = 0;
                next.x = now.z+ now.x;
                next.y = now.y;
            }

            if(!visited[next.x][next.y][next.z]) {
                visited[next.x][next.y][next.z] = 1;
                next.t = now.t + 1;
                q.push(next);
            }
            if(now.z > (n - now.y)){
                next.z = now.z - n + now.y;
                next.y = n;
                next.x = now.x;
            }
            else {
                next.z = 0;
                next.y = now.z+ now.y;
                next.x = now.x;
            }
            if(!visited[next.x][next.y][next.z]) {
                visited[next.x][next.y][next.z] = 1;
                next.t = now.t + 1;
                q.push(next);
            }
        }
    }
    puts("NO");
    return ;
}

int main() {
    int s, n, m;
    while(scanf("%d%d%d",&s,&n,&m),s||n||m) {
        if(s%2){
            puts("NO");
        }
        else {
            bfs(s,n,m);
        }
    }
    return 0;
}


简化一点点:

#include <stdio.h>
#include <string.h>
#include <queue>

using namespace std;
#define MAX 101

typedef struct n {
    int x,y,z,t;
}NODE;

bool visited[MAX][MAX][MAX];
int s, n, m;

int judge(NODE p){
    if((p.x == p.y && p.x + p.y == s) || (p.x == p.z && p.x + p.z == s) || (p.z == p.y && p.z + p.y == s)){
        return 1;
    }
    return 0;
}

void bfs() {
    queue<NODE>q;
    NODE now,next;
    now.x = s;
    now.y = 0;
    now.z = 0;
    now.t = 0;
    q.push(now);
    memset(visited,0,sizeof(visited));
    while(!q.empty()) {
        now = q.front();
        q.pop();
        if(judge(now)){
            printf("%d\n",now.t);
            return ;
        }

        if(now.x) {
            next.x = (now.x > (n - now.y))? now.x - n + now.y: 0;
            next.y = (now.x > (n - now.y))? n: now.y + now.x;
            next.z = now.z;

            if(!visited[next.x][next.y][next.z]) {
                visited[next.x][next.y][next.z] = 1;
                next.t = now.t + 1;
                q.push(next);
            }

            next.x = (now.x > (m - now.z))? now.x - m + now.z: 0;
            next.z = (now.x > (m - now.z))? m: now.z + now.x;
            next.y = now.y;
            if(!visited[next.x][next.y][next.z]) {
                visited[next.x][next.y][next.z] = 1;
                next.t = now.t + 1;
                q.push(next);
            }
        }

        if(now.y) {
            next.y = (now.y > (s - now.x))? now.y - s + now.x: 0;
            next.x = (now.y > (s - now.x))? s: now.x + now.y;
            next.z = now.z;

            if(!visited[next.x][next.y][next.z]) {
                visited[next.x][next.y][next.z] = 1;
                next.t = now.t + 1;
                q.push(next);
            }
            next.y = (now.y > (m - now.z))? now.y - m + now.z: 0;
            next.z = (now.y > (m - now.z))? m: now.z + now.y;
            next.x = now.x;
            if(!visited[next.x][next.y][next.z]) {
                visited[next.x][next.y][next.z] = 1;
                next.t = now.t + 1;
                q.push(next);
            }
        }

        if(now.z) {
            next.z = (now.z > (s - now.x))? now.z - s + now.x: 0;
            next.x = (now.z > (s - now.x))? s: now.z + now.x;
            next.y = now.y;

            if(!visited[next.x][next.y][next.z]) {
                visited[next.x][next.y][next.z] = 1;
                next.t = now.t + 1;
                q.push(next);
            }

            next.z = (now.z > (n - now.y))? now.z - n + now.y: 0;
            next.y = (now.z > (n - now.y))? n: now.z + now.y;
            next.x = now.x;
            if(!visited[next.x][next.y][next.z]) {
                visited[next.x][next.y][next.z] = 1;
                next.t = now.t + 1;
                q.push(next);
            }
        }
    }
    puts("NO");
    return ;
}
int main() {
    while(scanf("%d%d%d",&s,&n,&m),s||n||m) {
        if(s%2){
            puts("NO");
        }
        else {
            bfs();
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值