白银莲花池

题目

题目背景
(Silver Lilypad Pond, USACO 2007 Feb)
题目描述
为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘。这个长方形的池子被分成
了M行N列个方格(1 ≤ M, N ≤ 30)。一些格子是坚固得令人惊讶的莲花,还有一些格子是
岩石,其余的只是美丽、纯净、湛蓝的水。
贝西正在练习芭蕾舞,她站在一朵莲花上,想跳到另一朵莲花上去,她只能从一朵莲花
跳到另一朵莲花上,既不能跳到水里,也不能跳到岩石上。
贝西的舞步很像象棋中的马步:每次总是先横向移动一格,再纵向移动两格,或先纵向
移动两格,再横向移动一格。最多时,贝西会有八个移动方向可供选择。
约翰一直在观看贝西的芭蕾练习,发现她有时候不能跳到终点,因为中间缺了一些荷叶。
于是他想要添加几朵莲花来帮助贝西完成任务。一贯节俭的约翰只想添加最少数量的莲花。
当然,莲花不能放在石头上。
请帮助约翰确定必须要添加的莲花的最少数量。在添加莲花最少的基础上,确定贝西从
起点跳到目标需要的最少步数。最后,确定满足添加的莲花数量最少时,步数最少的路径条

输入输出格式
输入格式:
第一行:两个用空格分开的整数:M和N
第二行到M + 1行:第i + 1行有N个用空格分开的整数,描述了池塘第i行的状态:0 为水,1 为莲花,2 为岩石,3 为贝西所在的起点,4 为贝西想去的终点。
输出格式:
第一行:一个整数:需要添加的莲花的最少数目;如果无解,则输出-1
第二行:一个整数:在添加莲花最少的基础上,贝西从起点跳到终点需要的最少步数;如果第一行是-1,不输出这行
第三行:一个整数:在添加莲花最少的基础上,步数等于第二行输出的路径条数;如果第一行是-1,不输出这行
输入输出样例
输入样例#1:
4 8
0 0 0 1 0 0 0 0
0 0 0 0 0 2 0 1
0 0 0 0 0 4 0 0
3 0 0 0 0 0 1 0
(池塘分成四行八列,贝西的起点在第四行
第一列,想去的终点在第三行第六列,池塘
里一共有五朵莲花和一块石头)
输出样例#1:
2
6
2
最少要加两朵莲花,位置如 x 所示:
0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0
0 x 0 0 0 2 0 1 0 0 0 0 0 2 0 1
0 0 0 0 x 4 0 0 0 0 x 0 x 4 0 0
3 0 0 0 0 0 1 0 3 0 0 0 0 0 1 0
贝西至少要跳六步,两种不同的跳法如下:
0 0 0 C 0 0 0 0 0 0 0 C 0 0 0 0
0 B 0 0 0 2 0 F 0 0 0 0 0 2 0 F
0 0 0 0 D G 0 0 0 0 B 0 D G 0 0
A 0 0 0 0 0 E 0 A 0 0 0 0 0 E 0 )

题解

3遍BFS扫一下即可

code

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <list>
#include <map>
#include <iomanip>    
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int maxn = 1e3 + 100;
typedef long long ull;
typedef pair<int, int> pii;
#define mp make_pair

inline int read() {
    int s = 0, w = 1;
    char ch = getchar();
    while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); }
    while (isdigit(ch)) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
    return s * w;
}

const int dx[8] = { 2, 2, -2, -2, 1, -1, 1, -1 };
const int dy[8] = { -1, 1, -1, 1, 2, 2, -2, -2 };
const int inf = 0x3f3f3f3f;
int n;
int m;
int sx;
int sy;
int ex;
int ey;
int a[maxn][maxn];
int vis[maxn][maxn];
ull f[maxn][maxn];
ull dis[maxn][maxn];
ull t[maxn][maxn];
queue<pii> q1;
queue<pii> q2;
queue<pii> q3;

void bfs1() {
    memset(vis, 0, sizeof(vis));
    memset(f, 63, sizeof(f));
    f[sx][sy] = 0;
    vis[sx][sy] = 1;
    while (!q1.empty()) {
        int x = q1.front().first;
        int y = q1.front().second;
        vis[x][y] = 0;
        q1.pop();
        for (int i = 0; i < 8; ++i) {
            int xx = x +dx[i], yy = y + dy[i];
            if (xx < 1 || yy < 1 || xx > n || yy > m || a[xx][yy] == 2) continue;
            if (f[xx][yy] > f[x][y] + a[xx][yy]) {
                f[xx][yy] = f[x][y] + a[xx][yy];
                if (xx == ex && yy == ey) continue;
                if (!vis[xx][yy]) {
                    q1.push( mp(xx, yy) );
                    vis[xx][yy] = 1;
                }
            }
        }
    }
}

void bfs2() {
    memset(dis, 63, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    q2.push(mp(sx, sy));
    dis[sx][sy] = 0;
    vis[sx][sy] = 0;
    while (!q2.empty()) {
        int x = q2.front().first;
        int y = q2.front().second;
        vis[x][y] = 0;
        q2.pop();
        for (int i = 0; i < 8; ++i) {
            int xx  = x + dx[i], yy = y + dy[i];
            if (xx < 1 || yy < 1 || xx > n || yy > m || a[xx][yy] == 2) continue;
            if (f[xx][yy] != f[x][y] + a[xx][yy]) continue;
            if (dis[xx][yy] < dis[x][y] + 1) continue;
            dis[xx][yy] = dis[x][y] + 1;
            if (!vis[xx][yy]) {
                q2.push( mp(xx, yy) );
                vis[xx][yy] = 1;
            }
        }
    }
}

void bfs3() {
    // memset(t, 63, sizeof(t));
    memset(vis, 0, sizeof(vis));
    t[sx][sy] = 1;
    // vis[sx][sy] = 1;
    q3.push(mp(sx, sy));
    while (!q3.empty()) {
        int x = q3.front().first;
        int y = q3.front().second;
        vis[x][y] = 0;
        q3.pop();
        for (int i = 0; i < 8; ++i) {
            int xx = x + dx[i], yy = y + dy[i];
            if (xx < 1 || yy < 1 || xx > n || yy > m || a[xx][yy] == 2) continue;
            if (dis[x][y] + 1 != dis[xx][yy]) continue;
            if (f[x][y] + a[xx][yy] != f[xx][yy]) continue;
            t[xx][yy] += t[x][y];
            if (xx == ex && yy == ey) continue;
            if (!vis[xx][yy]) {
                q3.push( mp(xx, yy) );
                vis[xx][yy] = 1;
            }
        }
    }
}

int main() {
    freopen("silvlily.in","r",stdin);
    freopen("silvlily.out","w",stdout);
    n = read(), m = read();
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            int p;
            p = read();
            if (p == 3) { sx = i; sy = j; a[i][j] = 0; }
            else if (p == 4) { ex = i; ey = j; a[i][j] = 0; }
            if (p == 0) a[i][j] = 1;
            if (p == 1) a[i][j] = 0;
            if (p == 2) a[i][j] = 2;
        }
    }
    q1.push( mp(sx, sy) );
    bfs1();
    if (f[ex][ey] == f[0][0]) {
        printf("-1");
        return 0;
    }
    else printf("%lld\n", f[ex][ey]);
    bfs2();
    printf("%lld\n", dis[ex][ey]);
    bfs3();
    printf("%lld\n", t[ex][ey]);
    // system("PAUSE");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值