洛谷 P4554 小明的游戏

洛谷 P4554 小明的游戏

Description

  • 小明最近喜欢玩一个游戏。给定一个n×m的棋盘,上面有两种格子#@。游戏的规则很简单:给定一个起始位置和一个目标位置,小明每一步能向上,下,左,右四个方向移动一格。如果移动到同一类型的格子,则费用是0,否则费用是1。请编程计算从起始位置移动到目标位置的最小花费。

Input

  • 输入文件有多组数据。
    输入第一行包含两个整数n,m,分别表示棋盘的行数和列数。
    输入接下来的n行,每一行有m个格子(使用#或者@表示)。
    输入接下来一行有四个整数x1,y1,x2,y2,分别为起始位置和目标位置。
    当输入n,m均为0时,表示输入结束。

Output

  • 对于每组数据,输出从起始位置到目标位置的最小花费。每一组数据独占一行。

Sample Input

2 2
@#
#@
0 0 1 1
2 2
@@
@#
0 1 1 0
0 0

Sample Output

2
0

Data Size

  • 对于20%的数据满足:1≤n,m≤10
    对于40%的数据满足:1≤n,m≤300
    对于100%的数据满足:1≤n,m≤500

题解:

  • 看起来像搜索,我把它当最短路来写。
  • 把二维矩阵的每个元素的附上一个编号,依据题意建图,跑最短路即可。
  • 我的代码晦涩难懂,因为最近学了dijstra堆优化,练练手,大材小用了
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <queue>
#define maxn 505
#define N 505 * 505
using namespace std;

struct Node
{
    int val, pos;
    friend bool operator < (Node x, Node y) {
        return x.val > y.val;
    }
};
struct E {int next, to, dis;} e[N * 5];
int n, m, x1, y1, x2, y2, num;
int a[maxn][maxn];
int dx[5] = {0, -1, 1, 0, 0};
int dy[5] = {0, 0, 0, -1, 1};
int h[N], dis[N];
bool vis[N];

int cal(int x, int y) {
    return (x - 1) * m + y;
}

void add(int u, int v, int w)
{
    e[++num].next = h[u];
    e[num].to = v;
    e[num].dis = w;
    h[u] = num;
}

int dijstrka(int st, int en)
{
    priority_queue<Node> que;
    memset(dis, 0x3f, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    dis[st] = 0, que.push((Node){0, st});
    while(que.size())
    {
        int now = que.top().pos; que.pop();
        if(vis[now]) continue;
        vis[now] = 1;
        for(int i = h[now]; i != 0; i = e[i].next)
            if(dis[now] + e[i].dis < dis[e[i].to])
            {
                dis[e[i].to] = dis[now] + e[i].dis;
                if(!vis[e[i].to]) que.push((Node){dis[e[i].to], e[i].to});
            }
    }
    return dis[en];
}

int main()
{
    while(scanf("%d%d", &n, &m) == 2)
    {
        if(!n && !m) break;
        num = 0;
        memset(h, 0, sizeof(h));
        memset(a, 0, sizeof(a));
        for(int i = 1; i <= n; i++)
        {
            string t;   cin >> t;
            for(int j = 0; j < m; j++)
                if(t[j] == '#') a[i][j + 1] = 0;
                else a[i][j + 1] = 1;
        }
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++)
                if(i >= 1 && i <= n && j >= 1 && j <= m)
                {
                    int x = i, y = j;
                    for(int k = 1; k <= 4; k++)
                    {
                        int xx = x + dx[k], yy = y + dy[k];
                        if(xx >= 1 && xx <= n && yy >= 1 && yy <= m)
                        {
                            if(a[x][y] == a[xx][yy]) add(cal(x, y), cal(xx, yy), 0);
                            else add(cal(x, y), cal(xx, yy), 1);
                        }
                    }
                }
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        x1++, y1++, x2++, y2++;
        printf("%d\n", dijstrka(cal(x1, y1), cal(x2, y2)));
    }
    return 0;
}

转载于:https://www.cnblogs.com/BigYellowDog/p/11250854.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值