HDU 3023 Dirt 踩黑白块

原题: http://acm.hdu.edu.cn/showproblem.php?pid=3023

题目:

Dirt

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 697 Accepted Submission(s): 150

Problem Description
– Hello, may I speak to Petrov, please? Hello, my darling… You know, there was a little accident at our home… No, no, don’t worry, your computer was not damaged. It is only a bit dirty there now. Well, I should say it’s very dirty there and I’m at my Mom’s now. Of course, I’ll clean it… When? Well, maybe when I have my vacation. What? Well, when we are back from Turkey… the next vacation then. I’ll stay at Mother’s until then, and you may live here also. No, no, I don’t insist, sure, you may stay at home if you wish so. I prepared boots for you, they are at the door. But please, don’t make it worse, before you step on a clean floor, change your boots, put on your slippers, they are at the door also. Take them with you when you walk through the dirt. And when you walk on a clean floor, take the boots with you. You see, the dirt is in different places. OK, my love? Thank you!

It is not a great pleasure to change boots each time you get from a clean floor to a dirty floor and vice versa, it’s easier to walk extra several meters. So it is necessary to find a way of getting from one place in the apartment to another with the minimal possible number of boots changes; and among these paths the shortest one must be found.

To begin with, it is natural to determine an optimal way of passing the Most Important Route: from the computer to the refrigerator.

Input
The first line of the input contains two integers M and N, which are dimensions of the apartment (in meters), 1 ≤ N, M ≤ 1000. The two integers in the second line are the coordinates of the computer, and the third line contains the coordinates of the refrigerator. Each of the following M lines contains N symbols; this is the plan of the apartment. On the plan, 1 denotes a clean square, 2 denotes a dirty square, and 0 is either a wall or a square of impassable dirt. It is possible to get from one square to another if they have a common vertex. When you pass from a clean square to a dirty one or vice versa, you must change shoes. The computer and the refrigerator are not on the squares marked with 0.

The upper left square of the plan has coordinates (1, 1).

Output
You should output two integers in one line separated with a space. The first integer is the length of the shortest path (the number of squares on this path including the first and the last squares) with the minimal possible number of boots changes. The second number is the number of boots changes. If it is impossible to get from the computer to the refrigerator, you should output 0 0.

Sample Input
3 7
1 1
3 7
1200121
1212020
1112021

Sample Output
8 4

思路:

输入起点,终点,地图。地图上0不能走,有1和2两种地板,分别代表干净和肮脏,从一种干净程度的点到另一种的要换鞋,每次前进有8个方向可选,要求换鞋的次数最少。

使用bfs和优先队列即可。

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define N 1001
using namespace std;
int fx[8][2]={0,1,0,-1,1,0,-1,0,1,1,1,-1,-1,1,-1,-1},n,m,sx,sy,ex,ey,tp,tb;  //最短距离,最少步数
struct Map
{
    int cb;             //交换次数
    int path;           //距离
    int x;
    int y;
    char c;
    bool operator<(Map t1,Map t2)
    {
        if(t1.cb==t2.cb)
        {
            return t1.path>t2.path;
        }
        return t1.cb>t2.cb;

    }
};
Map map[N][N];
void input()
{
    int i,j;
    scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
    for(i=1;i<=n;i++)
    {
        getchar();
        for(j=1;j<=m;j++)
        {
            map[i][j].c=getchar();
            map[i][j].path=-1;
            map[i][j].cb=0;
        }
    }
}
bool isvalide(int x,int y)
{
    if(x<1||x>n||y<1||y>m||map[x][y].c=='0')
        return false;
        return true;
}
void BFS()
{
    int i;
        priority_queue<Map> qu;
    Map t1,t2;                          //t1用来暂存压入,t2取出加减变换
    int tx,ty;
    map[sx][sy].path=0;
    map[sx][sy].cb=0;
    t1=map[sx][sy];
    t1.x=sx;
    t1.y=sy;
    qu.push(t1);
    while(!qu.empty())
    {
        t2=qu.top();
        if(t2.cb>tb||(t2.cb==tb&&t2.path>=tp))
            return;
        qu.pop();
        for(i=0;i<8;i++)
        {
            tx=t2.x+fx[i][0];
            ty=t2.y+fx[i][1];
            if(isvalide(tx,ty))             //可以走
            {
                if(map[tx][ty].path!=-1)    //被访问过
                {
                    if(map[tx][ty].c!=t2.c) //污染程度不同
                    {
                        if(map[tx][ty].cb<t2.cb+1||(map[tx][ty].cb==t2.cb+1&&map[tx][ty].path<=t2.path+1))  //tmp为更优解,跳过该点
                            continue;
                        map[tx][ty].cb=t2.cb+1;
                        map[tx][ty].path=t2.path+1;
                    }
                    else                    //污染程度相同
                    {
                        if(map[tx][ty].cb<t2.cb||(map[tx][ty].cb==t2.cb&&map[tx][ty].path<=t2.path+1))      //tmp为更优解,跳过该点
                            continue;
                        map[tx][ty].cb=t2.cb;
                        map[tx][ty].path=t2.path+1;
                    }
                }
                else        //未被访问过
                {
                    if(map[tx][ty].c!=t2.c) //污染不同
                    {
                        map[tx][ty].cb=t2.cb+1;
                        map[tx][ty].path=t2.path+1;
                    }
                    else
                    {
                        map[tx][ty].cb=t2.cb;
                        map[tx][ty].path=t2.path+1;
                    }
                }
                t1=map[tx][ty];
                t1.x=tx;
                t1.y=ty;
                if(tx==ex&&ey==ty)  //找到终点
                {
                    if(t1.cb<tb)
                    {
                        tb=t1.cb;   //更新最少次数
                        tp=t1.path;
                    }
                    else if(t1.cb==tb&&t1.path<tp)
                    {
                        tp=t1.path; //更新最少步骤
                    }
                    continue;
                }
                qu.push(t1);
            }
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        input();
        tb=tp=100000;
        BFS();
        printf("%d %d\n",tp+1,tb);
    }
}

此题代码是在别人的代码上的再加上自己的理解批修改过的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值