百练4115:鸣人和佐助

题目连接:http://bailian.openjudge.cn/practice/4115/

这题就是紧接着拯救行动的变形。

直接贴代码吧,嘻嘻

不知道有没有喜欢我这种代码习惯或者风格的人

#include<iostream>
#include<algorithm>
#include<fstream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<cctype>
#include<vector>
#include<limits.h>
#include<queue>

using namespace std;

const int maxn=210;

struct mmp
{
    int r,c;
    int step;
    int sum;
    bool operator<(const mmp &w) const
    {
        if(step==w.step)
        {
            return sum>w.sum;
        }
        return step>w.step;
    }
    mmp(int rr,int cc,int u,int ss):r(rr),c(cc),step(u),sum(ss) { }
};

priority_queue<mmp>q1;  

char s[maxn][maxn];
int vis[maxn][maxn];
int v[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
int n,m,t;

int bfs(int a,int b)
{
    int i,x,y;
    q1.push(mmp(a,b,0,0));
    while(q1.empty()==0)
    {
        mmp m1=q1.top();
        q1.pop();
        for(i=0; i<4; ++i)
        {
            x=m1.r+v[i][0];
            y=m1.c+v[i][1];
            if(x>=0&&x<n&&y>=0&&y<m&&vis[x][y]==0)
            {
                vis[x][y]=1;
                if(s[x][y]=='*'&&m1.sum<=t)
                {
                    q1.push(mmp(x,y,m1.step+1,m1.sum));
                }
                if(s[x][y]=='#'&&m1.sum+1<=t)
                {
                    q1.push(mmp(x,y,m1.step+1,m1.sum+1));
                }
                if(s[x][y]=='+')
                    return m1.step+1;
            }
        }
    }
    return -1;
}

int main()
{
    int a,b,ans,i,j;
    scanf("%d%d%d",&n,&m,&t);
    for(i=0; i<n; ++i)
    {
        scanf("%s",s[i]);
    }
    memset(vis,0,sizeof(vis));
    for(i=0; i<n; ++i)
    {
        for(j=0; j<m; ++j)
        {
            if(s[i][j]=='@')
            {
                a=i;
                b=j;
            }

        }
    }
    ans=bfs(a,b);
    cout<<ans<<endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用C++实现的代码: ``` #include <iostream> #include <queue> #include <cstring> using namespace std; const int MAXN = 205; const int INF = 0x3f3f3f3f; char map[MAXN][MAXN]; int dis[MAXN][MAXN][15]; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; struct Node { int x, y, chakra; }; int bfs(int sx, int sy, int ex, int ey, int t) { memset(dis, INF, sizeof(dis)); queue<Node> q; q.push({sx, sy, t}); dis[sx][sy][t] = 0; while (!q.empty()) { Node now = q.front(); q.pop(); if (now.x == ex && now.y == ey) { return dis[now.x][now.y][now.chakra]; } if (map[now.x][now.y] == '#') { continue; } for (int i = 0; i < 4; i++) { int nx = now.x + dx[i]; int ny = now.y + dy[i]; if (nx < 0 || nx >= MAXN || ny < 0 || ny >= MAXN) { continue; } if (map[nx][ny] == '#') { continue; } if (map[nx][ny] == '+' && now.chakra == 0) { continue; } int nxt_chakra = now.chakra; if (map[nx][ny] == '+') { nxt_chakra--; } if (dis[nx][ny][nxt_chakra] > dis[now.x][now.y][now.chakra] + 1) { dis[nx][ny][nxt_chakra] = dis[now.x][now.y][now.chakra] + 1; q.push({nx, ny, nxt_chakra}); } } } return -1; } int main() { int m, n, t; cin >> m >> n >> t; int sx, sy, ex, ey; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cin >> map[i][j]; if (map[i][j] == '@') { sx = i; sy = j; } if (map[i][j] == '+') { ex = i; ey = j; } } } cout << bfs(sx, sy, ex, ey, t) << endl; return 0; } ``` 思路解析: 题目要求求出鸣人佐助之间的最短时间,因此可以使用 BFS 求解。 首先读入地图和鸣人的初始查克拉数量 T,然后使用 BFS 从鸣人的初始位置开始搜索。在 BFS 的过程中,需要记录鸣人到每个点所需的最短时间,以及鸣人在到达该点时所剩下的查克拉数量。如果鸣人到某个点时查克拉数量为 0,那么就不能继续前进到有大蛇丸手下的位置了。 当鸣人到达佐助的位置时,就可以得到鸣人追上佐助所需要的最短时间。 需要注意的是,题目中给出的地图大小为 M×N,其中 M 和 N 都可能达到 200,因此需要开较大的数组来存储最短时间和鸣人的查克拉数量。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值