bfs 2017.2.19

1、UESTC 149 解救小Q

因为有可能传送后需要再回来才能找到 Q,所以每次遇到传送门时只标记当前这个传送门,那么就有可能再传送回来

刚开始以为标记当前传送门以后就回不来了,写的一塌糊涂...

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
#include <cctype>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;

const ull mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 50 + 10;
char G[maxn][maxn];
int N, M;
int love_x, love_y;
int dir[][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
bool vis[maxn][maxn];

struct Door {
    int x1, y1, x2, y2;
    bool flag;
};

struct Node {
    int x, y;
    int Count;
};

Door door[40];

int bfs(void);

int main()
{
#ifdef __AiR_H
    freopen("in.txt", "r", stdin);
#endif // __AiR_H
    int T;
    scanf("%d", &T);
    while (T--) {
        memset(vis, false,sizeof(vis));
        memset(door, 0, sizeof(door));
        scanf("%d%d", &N, &M);
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < M; ++j) {
                cin >> G[i][j];
                if (G[i][j] == 'L') {
                    love_x = i; love_y = j;
                } else if (islower(G[i][j])) {
                    int t = G[i][j] - 'a';
                    if (!door[t].flag) {
                        door[t].x1 = i; door[t].y1 = j; door[t].flag = true;
                    } else {
                        door[t].x2 = i; door[t].y2 = j;
                    }
                }
            }
        }
        printf("%d\n", bfs());
    }
    return 0;
}

int bfs(void)
{
    Node node;
    queue<Node> Q;
    node.x = love_x; node.y = love_y; node.Count = 0;
    Q.push(node);
    vis[node.x][node.y] = true;
    while (!Q.empty()) {
        int x_t = Q.front().x;
        int y_t = Q.front().y;
        int Count_t = Q.front().Count;
        if (G[x_t][y_t] == 'Q') {
            return Count_t;
        }
        Q.pop();
        for (int i = 0; i < 4; ++i) {
            node.x = x_t + dir[i][0];
            node.y = y_t + dir[i][1];
            node.Count = Count_t + 1;
            if (0 <= node.x && node.x < N && 0 <= node.y && node.y < M && !vis[node.x][node.y] && G[node.x][node.y] != '#') {
                vis[node.x][node.y] = true;
                if (islower(G[node.x][node.y])) {
                    int t = G[node.x][node.y] - 'a';
                    if (door[t].x1 == node.x && door[t].y1 == node.y) {
                        node.x = door[t].x2; node.y = door[t].y2;
                    } else {
                        node.x = door[t].x1; node.y = door[t].y1;
                    }
                }
                Q.push(node);
            }
        }
    }
    return -1;
}

2、POJ 1426 Find The Multiple

#include <iostream>
#include <cstdio>
#include <queue>

using namespace std;

typedef long long LL;

int n;

void bfs(void);

int main()
{
//    freopen("in.txt", "r", stdin);
    while (scanf("%d", &n) != EOF && n != 0) {
        bfs();
    }
    return 0;
}

void bfs(void)
{
    queue<LL> Queue;
    Queue.push(1);
    while (!Queue.empty()) {
        LL t = Queue.front();
        if (t%n == 0) {
            printf("%I64d\n", t);
            return;
        }
        Queue.pop();
        Queue.push(t*10);
        Queue.push(t*10+1);
    }
}

3、FZU 2150 Fire Game

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
#include <cctype>
#include <bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int, int> Pair;

const ull mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 20;

struct Node {
    int x, y, step;
};

int n, m, ans, Case = 0, size;
char G[maxn][maxn];
bool vis[maxn][maxn];
int dir[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

void bfs(int x1, int y1, int x2, int y2);

int main()
{
#ifdef __AiR_H
    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif // __AiR_H
    int T;
    scanf("%d", &T);
    int Case = 0;
    while (T--) {
        vector<Pair> V;
        scanf("%d%d", &n, &m);
        ans = INF;
        for (int i = 0; i < n; ++i) {
            scanf("%s", G[i]);
            for (int j = 0; j < m; ++j) {
                if (G[i][j] == '#') {
                    V.push_back(make_pair(i, j));
                }
            }
        }
        size = V.size();
        if (size <= 2) {
            printf("Case %d: 0\n", ++Case);
            continue;
        }
        for (int i = 0; i < size-1; ++i) {
            for (int j = i+1; j < size; ++j) {
                bfs(V[i].first, V[i].second, V[j].first, V[j].second);
            }
        }
        if (ans == INF) {
            ans = -1;
        }
        printf("Case %d: %d\n", ++Case, ans);
    }
    return 0;
}

void bfs(int x1, int y1, int x2, int y2)
{
    memset(vis, false, sizeof(vis));
    vis[x1][y1] = vis[x2][y2] = true;
    int Max = 0, cnt = 2;
    queue<Node> Queue;
    Node node;
    node.x = x1, node.y = y1, node.step = 0, Queue.push(node);
    node.x = x2, node.y = y2, node.step = 0, Queue.push(node);
    while (!Queue.empty()) {
        int x_t = Queue.front().x, y_t = Queue.front().y, step_t = Queue.front().step;
        if (step_t > ans) {
            return;
        }
        if (step_t > Max) {
            Max = step_t;
        }
        Queue.pop();
        for (int i = 0; i < 4; ++i) {
            node.x = x_t+dir[i][0], node.y = y_t+dir[i][1], node.step = step_t + 1;
            if (0 <= node.x && node.x < n && 0 <= node.y && node.y < m && G[node.x][node.y] == '#' && !vis[node.x][node.y]) {
                vis[node.x][node.y] = true;
                ++cnt;
                Queue.push(node);
            }
        }
    }
    if (cnt == size && Max < ans) {
        ans = Max;
    }
}

4、HDU 5945 Fxx and game

用优先队列当然更好,不过窝这个暴力 bfs 竟然也过了

当 val_t - 1 <= t 时不能直接 return step_t + 1 。。。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <bitset>
#include <ctime>
#include <cctype>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> Pair;

const int maxn = 1e6 + 10;

struct Node {
    int val, step;
};

int T;
int X, k, t;
bool vis[maxn];

int bfs(void);

int main() {
#ifdef __AiR_H
    freopen("in.txt", "r", stdin);
#endif // __AiR_H_
    scanf("%d", &T);
    while (T--) {
        scanf("%d %d %d", &X, &k, &t);
        printf("%d\n", bfs());
    }
    return 0;
}

int bfs(void) {
    queue<Node> Q;
    Node node;
    node.val = X, node.step = 0;
    vis[X] = true;
    memset(vis, false, sizeof(vis));
    Q.push(node);
    while (!Q.empty()) {
        int val_t = Q.front().val, step_t = Q.front().step;
        Q.pop();
        if (val_t == 1) {
            return step_t;
        }
        if (val_t - 1 <= t) {
            node.val = 1, node.step = step_t + 1;
            if (!vis[node.val]) {
                Q.push(node), vis[node.val] = true;
            }
            continue;
        }
        if (k != 1 && val_t % k == 0) {
            node.val = val_t / k, node.step = step_t + 1;
            if (!vis[node.val]) {
                Q.push(node), vis[node.val] = true;
            }
        }
        node.val = val_t - t, node.step = step_t + 1;
        if (!vis[node.val]) {
            Q.push(node), vis[node.val] = true;
        }
        if (k != 1) {
            int x = val_t / k;
            for (int i = x; val_t - t < i * k && i * k < val_t; --i) {
                node.val = i * k, node.step = step_t + 1;
                if (!vis[node.val]) {
                    Q.push(node), vis[node.val] = true;
                }
            }
        }
    }
    return 0;
}

5、UVa 12558 Egyptian Fractions (HARD version)

注意:要用 ll

第一次做时限15s的题,T了好几次。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <ctime>
#include <cassert>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define eps 1e-9

typedef long long ll;
typedef pair<int, int> Pair;

const int INF = 0x7fffffff;

struct Node {
    ll x, y;
    int cnt;
    ll num[20];
};

int a, b, k, ans_cnt = 0;
int key[10];
ll ans[20];

bool bfs(int depth);
bool can_t(ll x);
bool cmp(ll* x, ll *y, ll len);

int main() {
#ifdef __AiR_H
    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif // __AiR_H
    int T, Case = 0;
    scanf("%d", &T);
    while (T--) {
        scanf("%d %d %d", &a, &b, &k);
        REP(i, k) { scanf("%d", &key[i]); }
        int d = 1;
        ans_cnt = 0;
        while (1) {
            if (bfs(d)) { break; }
            ++d;
        }
        printf("Case %d: %d/%d=", ++Case, a, b);
        REP(i, ans_cnt - 1) { printf("1/%lld+", ans[i]); }
        printf("1/%lld\n", ans[ans_cnt - 1]);
    }
#ifdef __AiR_H
    printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
#endif // __AiR_H
    return 0;
}

bool cmp(ll* x, ll *y, int len) {
    for (int i = len - 1; i >= 0; --i) {
        if (x[i] < y[i]) { return true; }
        if (x[i] > y[i]) { return false; }
    }
    return false;
}

bool bfs(int depth) {
    queue<Node> q;
    Node node;
    bool flag = false;
    ll t_1, x_t, y_t, gcd;
    t_1 = depth * b;
    for (ll i = b / a; a * i <= t_1; ++i) {
        if (a * i < b) { continue; }
        if (can_t(i)) { continue; }
        node.x = a * i - b; node.y = b * i;
        gcd = __gcd(node.x, node.y); node.x /= gcd; node.y /= gcd;
        node.cnt = 1; node.num[0] = i; q.push(node);
    }
    while (!q.empty()) {
        Node t = q.front(); q.pop();
        if (flag && t.num[t.cnt - 1] > ans[ans_cnt - 1]) { continue; }
        if (t.x == 0) {
            if (ans_cnt < t.cnt || cmp(t.num, ans, ans_cnt)) {
                ans_cnt = t.cnt;
                REP(i, ans_cnt) { ans[i] = t.num[i]; }
            }
            flag = true; continue;
        }
        if (t.cnt == depth) { continue; }
        t_1 = (depth - t.cnt) * t.y;
        for (ll i = max(t.num[t.cnt - 1] + 1, t.y / t.x); t.x * i <= t_1; ++i) {
            if (flag && i > ans[ans_cnt - 1]) { break; }
            if (t.x * i < t.y) { continue; }
            if (can_t(i)) { continue; }
            x_t = t.x * i - t.y; y_t = i * t.y;
            gcd = __gcd(x_t, y_t); x_t /= gcd; y_t /= gcd;
            node.x = x_t; node.y = y_t;
            REP(i, t.cnt) { node.num[i] = t.num[i]; }
            node.cnt = t.cnt + 1; node.num[t.cnt] = i;
            q.push(node);
        }
    }
    return flag;
}

bool can_t(ll x) {
    REP(i, k) { if (x == (ll)key[i]) { return true; } }
    return false;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
毕业设计,基于SpringBoot+Vue+MySQL开发的社区医院管理系统,源码+数据库+毕业论文+视频演示 信息数据从传统到当代,是一直在变革当中,突如其来的互联网让传统的信息管理看到了革命性的曙光,因为传统信息管理从时效性,还是安全性,还是可操作性等各个方面来讲,遇到了互联网时代才发现能补上自古以来的短板,有效的提升管理的效率和业务水平。传统的管理模式,时间越久管理的内容越多,也需要更多的人来对数据进行整理,并且数据的汇总查询方面效率也是极其的低下,并且数据安全方面永远不会保证安全性能。结合数据内容管理的种种缺点,在互联网时代都可以得到有效的补充。结合先进的互联网技术,开发符合需求的软件,让数据内容管理不管是从录入的及时性,查看的及时性还是汇总分析的及时性,都能让正确率达到最高,管理更加的科学和便捷。本次开发的社区医院管理系统实现了病例信息、字典表、家庭医生、健康档案、就诊信息、前台、药品、用户、用户、用户表等功能。系统用到了关系型数据库中王者MySql作为系统的数据库,有效的对数据进行安全的存储,有效的备份,对数据可靠性方面得到了保证。并且程序也具备程序需求的所有功能,使得操作性还是安全性都大大提高,让社区医院管理系统更能从理念走到现实,确确实实的让人们提升信息处理效率。 关键字:社区医院管理系统;信息管理,时效性,安全性,MySql
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值