分支限界法

最少打多少油井

题目描述
有一片长方形地域,@为出油点,*为不能出油的点,每个出油点,与八个方向联通,问可以最少打几口井能采到所有的油。

输入
M行N列,然后是方阵数据。只有@和*

数据不止一组

输出
输出整数,表示最少的油井数。

样例输入
5 5
* * *@@
@* * *@
@@* *@
* * @ *@
* @* *@
样例输出
2

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

using namespace std;

typedef struct Node {
    int x;
    int y;
    char c;
};
Node path[99][99];
queue<Node> myQue;

void bfs(int i, int j) {
    myQue.push(path[i][j]);     // 入队
    path[i][j].c = '*';         // 标记走过
    while (!myQue.empty()) {
        Node front = myQue.front();                 // 注意下标位置不要写错
        if (path[front.x][front.y+1].c == '@') {    // 右
            myQue.push(path[front.x][front.y+1]);
            path[front.x][front.y+1].c = '*';
        }
        if (path[front.x+1][front.y-1].c == '@') {  // 左下
            myQue.push(path[front.x+1][front.y-1]);
            path[front.x+1][front.y-1].c = '*';
        }
        if (path[front.x+1][front.y].c == '@') {    // 下
            myQue.push(path[front.x+1][front.y]);
            path[front.x+1][front.y].c = '*';
        }
        if (path[front.x+1][front.y+1].c == '@') {  // 右下
            myQue.push(path[front.x+1][front.y+1]);
            path[front.x+1][front.y+1].c = '*';
        }
        myQue.pop();
    }
}
void Clear(int m, int n) {
    for (int i = 0; i < m + 5; i++) {
        for (int j = 0; j < n + 5; j++) {
            path[i][j].x = i;
            path[i][j].y = j;
            path[i][j].c = '*';
        }
    }
}
void print(int m, int n) {      // 测试用, 打印数据
    for (int i = 1; i <= m; i++){
        for (int j = 1; j <= n; j++) {
            cout << path[i][j].c << " ";
        }
        cout << endl;
    }
    cout << endl;
}
int main() {
    int m, n;
    while (cin >> m >> n) {
        Clear(m, n);
        for (int ii = 1; ii <= m; ii++) {
            for (int jj = 1; jj <= n; jj++) {
                cin >> path[ii][jj].c;
            }
            getchar();
        }
        int cnt = 0;
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (path[i][j].c == '@') {
                    bfs(i, j);
                    //print(m,n);      // 测试用, 按步打印数组情况
                    cnt++;
                }
            }
        }
        cout << cnt << endl;
    }

    return 0;
}

大营救(广度)

题目描述
小A在监狱里。他的朋友们要去营救他。监狱里有守卫,朋友走一步需要一秒,但是杀死守卫需要一秒。

问,朋友最短多长时间能救出小A。

输入
整数,M,N,表示行列。

然后是M行N列个字符,x 表示守卫,a 表示小A,r表示朋友 .表示可走的路。#表示不能走的墙。

a只有一个。x,r不唯一。

输出
输出朋友营救小A的最短时间。

样例输入
3 4
a…
x..r
r..#
7 8
#.#####.
#.a#..r.
#..#x…
..#..#.#
#…##..
.#……
……..
样例输出
3
13

#include <iostream>
#include <queue>
#include <cstdio>
#include <climits>      // 包含INT_MAX
using namespace std;

typedef struct Node {
    int x;
    int y;
    char c;
    int t;
};

Node path[109][109];
queue<Node> myQue;
int fx[] = {1, 0, -1, 0};       // 位置改变量
int fy[] = {0, -1, 0, 1};

void bfs(int i, int j, int m, int n) {
    myQue.push(path[i][j]);
    path[i][j].c = '#';
    path[i][j].t = 1;
    while (!myQue.empty()) {
        Node front = myQue.front();                 // 获取队头后, 队头出队
        myQue.pop();
        for (int i = 0; i < 4; i++) {
            if (path[front.x+fx[i]][front.y+fy[i]].c != '#' &&               // 未走过, 且不为墙
                !path[front.x+fx[i]][front.y+fy[i]].t) {
                if (path[front.x+fx[i]][front.y+fy[i]].c == 'x') {           // 是守卫
                    path[front.x+fx[i]][front.y+fy[i]].c = '#';
                    path[front.x+fx[i]][front.y+fy[i]].t = front.t + 2;      // 杀掉守卫多耗时1
                    myQue.push(path[front.x+fx[i]][front.y+fy[i]]);
                } else if (path[front.x+fx[i]][front.y+fy[i]].c == 'r') {    // 当为r时, 特殊标记, 方便输出
                    path[front.x+fx[i]][front.y+fy[i]].c = 'R';
                    path[front.x+fx[i]][front.y+fy[i]].t = front.t + 1;
                    myQue.push(path[front.x+fx[i]][front.y+fy[i]]);
                } else if (path[front.x+fx[i]][front.y+fy[i]].c == '.') {    // 正常可走
                    path[front.x+fx[i]][front.y+fy[i]].c = '#';
                    path[front.x+fx[i]][front.y+fy[i]].t = front.t + 1;
                    myQue.push(path[front.x+fx[i]][front.y+fy[i]]);
                }
            }
        }

        /**
        用tmp变量怎么死循环了?求指点!
        */
        /*
        for (int i = 0; i < 4; i++) {
            Node tmp = path[front.x+fx[i]][front.y+fy[i]];
            tmp.y = path[front.x+fx[i]][front.y+fy[i]].y;
            tmp.c = path[front.x+fx[i]][front.y+fy[i]].c;
            tmp.t = path[front.x+fx[i]][front.y+fy[i]].t;
            if (tmp.c != '#' && !tmp.t) {
                if (tmp.c == 'x') {
                    tmp.c = '#';
                    tmp.t = front.t + 2;
                    myQue.push(tmp);
                }
                if (tmp.c == 'r') {
                    tmp.c = '#';
                    tmp.t = front.t + 1;
                    myQue.push(tmp);
                }
                if (tmp.c == '.') {
                    tmp.c = '#';
                    tmp.t = front.t + 1;
                    myQue.push(tmp);
                }
            }
        }*/

    }
}

void Clear(int m, int n) {
    for (int i = 0; i <= m + 1; i++) {
        for (int j = 0; j <= n + 1; j++) {
            path[i][j].x = i;
            path[i][j].y = j;
            path[i][j].c = '#';
            path[i][j].t = 0;
        }
    }
}

int main() {
    int m, n, x, y;
    while (cin >> m >> n) {
        Clear(m, n);            // 初始化
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                cin >> path[i][j].c;
                if (path[i][j].c == 'a') {      // 记录a的位置
                    x = path[i][j].x;
                    y = path[i][j].y;
                }
            }
            getchar();
        }
        bfs(x, y, m, n);
        int min_t = INT_MAX;
        for (int i = 1; i <= m ; i++) {
            for (int j = 1; j <= n; j++) {
                if (path[i][j].c == 'R' && path[i][j].t) {
                    if (min_t > path[i][j].t) {
                        min_t = path[i][j].t;
                    }
                }
            }
        }
        cout << min_t << endl;          // 如果结果为INT_MAX说明无法营救
    }
    return 0;
}

布线问题

题目描述

已知一个线路版如图所示,如何从A到B进行布线,使得使用的材料最省。

输入

输出
格于格之间使用一根线。所使用的线的个数。

样例输入
样例输出
H布线问题


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

typedef struct Node {
    int x;
    int y;
    int v;
};

Node path[7][7];
void bfs() {
    queue<Node> myQue;
    path[2][2].v = 0;
    myQue.push(path[2][2]);
    while (!myQue.empty()) {
        Node front = myQue.front();
        // 判断四个方向上的点是否可以入队
        if (!path[front.x+1][front.y].v) {
            path[front.x+1][front.y].v = front.v + 1;
            myQue.push(path[front.x+1][front.y]);
        }
        if (!path[front.x][front.y+1].v) {
            path[front.x][front.y+1].v = front.v + 1;
            myQue.push(path[front.x][front.y+1]);
        }
        if (!path[front.x-1][front.y].v) {
            path[front.x-1][front.y].v = front.v + 1;
            myQue.push(path[front.x-1][front.y]);
        }
        if (!path[front.x][front.y-1].v) {
            path[front.x][front.y-1].v = front.v + 1;
            myQue.push(path[front.x][front.y-1]);
        }
        myQue.pop();
    }
}

void Clear() {  // 初始化位置
    for (int i = 0; i <= 7; i++)
        for (int j = 0; j <= 7; j++) {
            path[i][j].v = 1;
            path[i][j].x = i;
            path[i][j].y = j;
        }
    for (int k = 1; k <= 6; k++) {
        for (int p = 1; p <= 6; p++) {
            path[k][p].v = 0;
        }
    }
    for (int ii = 2; ii <= 5; ii++) {
        path[ii][3].v = 1;
    }
    path[5][1].v = 1;
    path[5][2].v = 1;
}

int main() {
    Clear();
    bfs();
    cout << path[5][5].v << endl;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值