1009 - Fat Cat

版权声明:转载注明出处,大家一起交流 https://blog.csdn.net/qq_38231051/article/details/82292201

肥猫题,比较水,思路挺好找的

题目

Problem Description

Fat cat's duty is to catch mice, and it controls several rooms arranged in a matrix in which mice often come. Now a mouse comes into this room matrix and digs many many holes, and the mouse is cunning and it knows that fat cat will sleep in the daytime, so it goes out for food in that time instead of at night. Fat cat has to put some cheese in each room to make it out.

The mouse always appears first at room(0,0), and it will turn to another room near the current room which has the most number of cheese after it eats up the cheese in the room. If there is no adjacent room or the number of cheese is 0 in every adjacent room, the mouse quickly run in a hole after it eats the cheese and fat cat can't catch it.

When the mouse and the cat arrive at the same room or the mouse turns to a room where the cat is at the same time, the cat can catch the mouse!

When the mouse first appears, fat cat is in room(i,j)(not in room(0,0)),and it wants to catch the mouse as quickly as it can before the mouse runs away. We suppose that both the cat and the mouse move only vertically or horizontally 1 step in each time period.

Now it's your turn to write a program to calculate how many steps at least fat cat should move to catch the mouse. For example: the mouse starts at room(0,0), and fat cat is in room(2,0) at the same time. And the mouse moves to room(0,1),while fat cat can go to either room(2,1) or room(1,0), or just stay there.

Input

the input contains mutiple cases. The first line of each case consists of two positive integer: n and m (1&ltn&lt=100,1&ltm&lt=100),showing that the room matrix has n rows and m columns rooms. Then will be n rows non_negative integers presenting the number of cheese in each room(there are no two rooms have the same positive number of cheese). Each row has m integers. The following row shows fat cat's initial position: row i and column j.

Output

for each case,print the minimum number of time period fat cat uses to catch the mouse in a single line. If the mouse run away before being caught, print "impossible" instead.

Sample Input

2 2
15 3
2 4
0 1
3 3
1 5 9
3 7 0
2 4 6
2 0

Sample Output

1
impossible

这里我是先把老鼠的路径给保存下来,然后每一步与肥猫的原始位置计算距离(步骤距离),如果距离小于时间,退出打印结果。

ac代码:

#include <stdio.h>
#include <stdlib.h>
#define MAX 100000

/*
注:提交需要把中文去掉,不然无法提交。

*/

struct port
{
    int x;
    int y;
};
struct port a[102];//保存老鼠路径,这里的索引值就是时间。
int b[102][102];//保存房间信息
int count;//时间
void getway(struct port x)//下一步的位置
{
    int temp = 0;
    int i,j;
    //printf("%d %d\n",x.x,x.y);
    if(b[x.x-1][x.y]>0)
    {
        temp=b[x.x-1][x.y];
        i=x.x-1;
        j=x.y;
    }
    if(b[x.x+1][x.y]>0)
    {
        if(temp<b[x.x+1][x.y])
        {
            temp = b[x.x+1][x.y];
            i=x.x+1;
            j=x.y;
        }
    }
    if(b[x.x][x.y+1]>0)
    {
        if(temp<b[x.x][x.y+1])
        {
            temp = b[x.x][x.y+1];
            i=x.x;
            j=x.y+1;
        }
    }
    if(b[x.x][x.y-1]>0)
    {
        if(temp<b[x.x][x.y-1])
        {
            temp = b[x.x][x.y-1];
            i=x.x;
            j=x.y-1;
        }
    }
    if(temp>0)
    {
        ++count;
        a[count].x=i;
        a[count].y=j;
        b[x.x][x.y]=0;//走过后奶酪清零
        getway(a[count]);

    }else
    {
        ++count;
        a[count].x=MAX;
        a[count].y=0;
    }
}

int main()
{
    int m,n;
    int i,j;
    while(scanf("%d%d",&m,&n)==2)
    {
        count=0;
//--------------这里是扩大矩阵,这样可以省去边缘计算---------------
        for (i=0;i<=m+1;i = i+m+1)
            for (j=0;j<=n+1;++j)
        {
            b[i][j]=0;
        }
        for (i=0;i<=m+1;++i)
            for(j=0;j<=n+1;j=j+n+1)
        {
            b[i][j]=0;
        }
//------------------------------------------------------------
        for (i=1;i<=m;++i)
        {
            for(j=1;j<=n;++j)
                scanf("%d",&b[i][j]);
        }
        int port_x,port_y;
        scanf("%d %d",&port_x,&port_y);
        //扩展矩阵后肥猫的位置需要加一下。
        port_x+=1;
        port_y+=1;
        a[0].x=1;
        a[0].y=1;
        getway(a[0]);
        int min=-1;
        int dis;
        for(i=0;i<count;++i)
        {
            dis = abs(port_x-a[i].x)+abs(port_y-a[i].y);
            if(dis<=i)//满足条件就是我们需要的最短时间,直接退出循环
            {
                min = i;
                break;
            }
        }
        if(min>=0)
            printf("%d\n",min);
        else
            printf("impossible\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38231051/article/details/82292201
cat