HDU 2822 Dogs

题目大意:有一只狗想要去拜访另一只狗,但这只狗非常懒,它想要挖最少的洞。X代表这些是路是通的,且四个方向只要连着的X都不需要挖洞,“  . ”代表需要挖洞。

每组最后两行分别代表狗的起始点和终点。问小狗最少需要挖多少洞才能去拜访他的朋友。

解题思路:最短路第一反应应该是BFS,但是还需要加点什么,因为走X步数是不变的,走“ . ”步数需要加一,也就是每个点的权值是不一样的,所以小狗每移动一个方向走的步数可能不一样,所以每次都要从最小的步数出发,这里就需要用到优先队列了。


Prairie dog comes again! Someday one little prairie dog Tim wants to visit one of his friends on the farmland, but he is as lazy as his friend (who required Tim to come to his place instead of going to Tim's), So he turn to you for help to point out how could him dig as less as he could. 

We know the farmland is divided into a grid, and some of the lattices form houses, where many little dogs live in. If the lattices connect to each other in any case, they belong to the same house. Then the little Tim start from his home located at (x0, y0) aim at his friend's home ( x1, y1 ). During the journey, he must walk through a lot of lattices, when in a house he can just walk through without digging, but he must dig some distance to reach another house. The farmland will be as big as 1000 * 1000, and the up left corner is labeled as ( 1, 1 ). 
Input
The input is divided into blocks. The first line in each block contains two integers: the length m of the farmland, the width n of the farmland (m, n ≤ 1000). The next lines contain m rows and each row have n letters, with 'X' stands for the lattices of house, and '.' stands for the empty land. The following two lines is the start and end places' coordinates, we guarantee that they are located at 'X'. There will be a blank line between every test case. The block where both two numbers in the first line are equal to zero denotes the end of the input.
Output
For each case you should just output a line which contains only one integer, which is the number of minimal lattices Tim must dig.
Sample Input
6 6
..X...
XXX.X.
....X.
X.....
X.....
X.X...
3 5
6 3

0 0
Sample Output
3
 
        
  
Hint
Hint: Three lattices Tim should dig: ( 2, 4 ), ( 3, 1 ), ( 6, 2 ).

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
char s[1005][1005];
int n,m,x1,y1,x2,y2,book[1005][1005];
int dir[4][2]= {0,-1,0,1,-1,0,1,0};
struct node
{
    int x,y,step;
    bool operator <(node a)const //优先队列从小到大排序
    {
        return a.step<step;
    }
};
int bfs()
{
    priority_queue<node>q;
    node now,tmp;
    now.x=x1;
    now.y=y1;
    now.step=0;
    book[x1][y1]=1;
    q.push(now);
    while(!q.empty())
    {
        now=q.top();
        q.pop();
        if(now.x==x2&&now.y==y2)
            return now.step;
        for(int i=0; i<4; i++)
        {
            int tx=now.x+dir[i][0];
            int ty=now.y+dir[i][1];
            if(tx>=0&&tx<m&&ty>=0&&ty<n&&!book[tx][ty])
            {
                tmp.x=tx;
                tmp.y=ty;
                tmp.step=now.step;
                book[tx][ty]=1;
                if(s[tx][ty]=='.')   //只有是点的情况步数才加一
                    tmp.step=now.step+1;
                q.push(tmp);
            }
        }
    }
}

int main()
{
    int i,j;

    while(~scanf("%d%d",&m,&n))
    {
        if(m==0&&n==0)break;
        memset(book,0,sizeof(book));
        for(i=0; i<m; i++)
            scanf("%s",s[i]);
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        //输入时从0开始的,所以起点和终点都减一
        x1--;
        y1--;
        x2--;
        y2--;
        printf("%d\n",bfs());
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
4S店客户管理小程序-毕业设计,基于微信小程序+SSM+MySql开发,源码+数据库+论文答辩+毕业论文+视频演示 社会的发展和科学技术的进步,互联网技术越来越受欢迎。手机也逐渐受到广大人民群众的喜爱,也逐渐进入了每个用户的使用。手机具有便利性,速度快,效率高,成本低等优点。 因此,构建符合自己要求的操作系统是非常有意义的。 本文从管理员、用户的功能要求出发,4S店客户管理系统中的功能模块主要是实现管理员服务端;首页、个人中心、用户管理、门店管理、车展管理、汽车品牌管理、新闻头条管理、预约试驾管理、我的收藏管理、系统管理,用户客户端:首页、车展、新闻头条、我的。门店客户端:首页、车展、新闻头条、我的经过认真细致的研究,精心准备和规划,最后测试成功,系统可以正常使用。分析功能调整与4S店客户管理系统实现的实际需求相结合,讨论了微信开发者技术与后台结合java语言和MySQL数据库开发4S店客户管理系统的使用。 关键字:4S店客户管理系统小程序 微信开发者 Java技术 MySQL数据库 软件的功能: 1、开发实现4S店客户管理系统的整个系统程序; 2、管理员服务端;首页、个人中心、用户管理、门店管理、车展管理、汽车品牌管理、新闻头条管理、预约试驾管理、我的收藏管理、系统管理等。 3、用户客户端:首页、车展、新闻头条、我的 4、门店客户端:首页、车展、新闻头条、我的等相应操作; 5、基础数据管理:实现系统基本信息的添加、修改及删除等操作,并且根据需求进行交流信息的查看及回复相应操作。
现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本微信小程序医院挂号预约系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此微信小程序医院挂号预约系统利用当下成熟完善的SSM框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。微信小程序医院挂号预约系统有管理员,用户两个角色。管理员功能有个人中心,用户管理,医生信息管理,医院信息管理,科室信息管理,预约信息管理,预约取消管理,留言板,系统管理。微信小程序用户可以注册登录,查看医院信息,查看医生信息,查看公告资讯,在科室信息里面进行预约,也可以取消预约。微信小程序医院挂号预约系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值