zoukankan      html  css  js  c++  java
  • 【Aizu

    -->Cheese

    原文是日语,这里就写中文了

    Descriptions:

    在H * W的地图上有N个奶酪工厂,每个工厂分别生产硬度为1-N的奶酪。有一只老鼠准备从出发点吃遍每一个工厂的奶酪。老鼠有一个体力值,初始时为1,每吃一个工厂的奶酪体力值增加1(每个工厂只能吃一次),且老鼠只能吃硬度不大于当前体力值的奶酪。 老鼠从当前格到上下左右相邻的无障碍物的格需要时间1单位,有障碍物的格不能走。走到工厂上时即可吃到该工厂的奶酪,吃奶酪时间不计。问吃遍所有奶酪最少用时

    input

    第一行三个整数H(1 <= H <= 1000)、W(1 <= W <=1000)、N(1 <= N <= 9),之后H行W列为地图, “.“为空地, ”X“为障碍物,”S“为老鼠洞,N表示有N个生产奶酪的工厂,硬度为1-N。

    output 

    吃完所有工厂的最小步数

    Sample Input 1

    3 3 1
    S..
    ...
    ..1
    

    Sample Output 1

    4
    

    Sample Input 2

    4 5 2
    .X..1
    ....X
    .XX.S
    .2.X.
    

    Sample Output 2

    12
    

    Sample Input 3

    10 10 9
    .X...X.S.X
    6..5X..X1X
    ...XXXX..X
    X..9X...X.
    8.X2X..X3X
    ...XX.X4..
    XX....7X..
    X..X..XX..
    X...X.XX..
    ..X.......
    

    Sample Output 3

    91

    题目链接:

    https://vjudge.net/problem/Aizu-0558

    题目比较复杂

    简单来说就是让你求从起点‘S’到‘1’,‘1’到‘2’,‘2’到‘3’...‘n-1’到‘n’的最短距离,这样大家应该都会了吧,我这里写的bfs,具体可以看注释,dfs应该也行......

    #include <iostream>
    #include <cstdio>
    #include <fstream>
    #include <algorithm>
    #include <cmath>
    #include <deque>
    #include <vector>
    #include <queue>
    #include <string>
    #include <cstring>
    #include <map>
    #include <stack>
    #include <set>
    #include <sstream>
    #define mod 1000000007
    #define eps 1e-6
    #define ll long long
    #define INF 0x3f3f3f3f
    #define MEM(x,y) memset(x,y,sizeof(x))
    #define Maxn 1005
    using namespace std;
    struct node
    {
        int x,y,step;//在(x,y)的步数
    };
    node now,net;
    node a[Maxn*Maxn];//工厂
    char mp[Maxn][Maxn];//地图
    int vis[Maxn][Maxn];//标记是否走过
    int dt[][2] = {{0,1},{1,0},{0,-1},{-1,0}};//4个方向
    int h,w,n;
    int total;
    int bfs(node s,node e)//从s走到e
    {
        MEM(vis,0);//没次都要初始化为0
        queue<node>q;
        now.x=s.x,now.y=s.y,now.step=s.step;//初始化队列
        vis[now.x][now.y]=1;
        q.push(now);
        while(!q.empty())
        {
            now=q.front();
            q.pop();
            if(now.x==e.x&&now.y==e.y)//找到目的地
                return now.step;
            for(int i=0; i<4; i++)//四个方向走法
            {
                net.x=now.x+dt[i][0];
                net.y=now.y+dt[i][1];
                net.step=now.step+1;
                if(net.x>=0&&net.x<h&&net.y>=0&&net.y<w&&!vis[net.x][net.y]&&mp[net.x][net.y]!='X')//满足条件
                {
                    vis[net.x][net.y]=1;//标记走过
                    q.push(net);
                }
            }
        }
    }
    int main()
    {
        cin>>h>>w>>n;
        for(int i=0; i<h; i++)
            for(int j=0; j<w; j++)
            {
                cin>>mp[i][j];
                if(mp[i][j]=='S')//把起点设为0
                    mp[i][j]='0';
                for(int k=0; k<=n; k++)//标记工厂,分别储存
                {
                    if(mp[i][j]-'0'==k)
                    {
                        a[k].x=i;
                        a[k].y=j;
                        a[k].step=0;
                    }
                }
            }
        total=0;
        for(int i=0; i<n; i++)//开始搜索  增加步数
            total+=bfs(a[i],a[i+1]);//a[0]到a[1] a[1]到a[2]......
        cout<<total<<endl;
    
    }
  • 相关阅读:
    在Windows Server 2008 R2上安装IIS服务
    Linux 系统化学习系列文章总目录(持续更新中)
    Oracle 11g一步步安装详解
    MySQL 菜鸟入门“秘籍”
    Linux下的/proc目录介绍
    Python查找文件
    Python对文件和文件路径的管理
    Python中执行外部命令
    Python处理命令行参数
    CentOS7--TigerVNC
  • 原文地址:https://www.cnblogs.com/sky-stars/p/11186562.html
Copyright © 2011-2022 走看看