棋盘游戏

#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
int a[6][6], sx, sy, ex, ey;
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
bool vis[6][6];
struct node {
    int x, y, sta, cost;
};
queue<node> q;
int bfs() {
    int ans = 0x3f3f3f3f;
    node st = (node) {sx, sy, 1, 0};
    vis[sx][sy] = true;
    q.push(st);
    int nx, ny, dc, ns, nc;
    while(!q.empty()) {
        node now = q.front();
        q.pop();
        if(now.x == ex && now.y == ey) ans = min(ans, now.cost);
        for(int i = 0; i < 4; i++) {
            nx = now.x + dx[i];
            ny = now.y + dy[i];
            if(nx >= 0 && nx <= 5 && ny >= 0 && ny <= 5 && !vis[nx][ny]) {
                dc = now.sta * a[nx][ny];
                nc = now.cost + dc;
                ns = dc % 4 + 1;
                node nex = (node) {nx, ny, ns, nc};
                vis[nx][ny] = true;
                q.push(nex);
            }
        }
    }
    return ans;
}
int main() {
    for(int i = 0; i < 6; i++) {
        for(int j = 0; j < 6; j++) {
            scanf("%d", &a[i][j]);
        }
    }
    scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
    printf("%d", bfs());
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值