在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/C/ 一道ACM競賽的水題。

一道ACM競賽的水題。

來自hoj 1009 fat cat。
原題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

我的程序如下。然而OJ無限WA。反正給出的用例沒問題來著。菜雞真的求助。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int MAX(int a,int b,int c,int d);
int main()
{
    int matrix[100][100],flag;
    memset(matrix,0,sizeof(matrix));
    int c,r;
    int i,j;
    int a,b;
    int m,n;
    int step=0;
    while(scanf("%d %d",&r,&c)==2&&r>0&&c>0)
    {
        step=0;
        for(i=0; i<r; i++)
        {
            for(j=0; j<c; j++)
            {
                scanf("%d",&matrix[i][j]);
            }
        }
        a=0,b=0;
        scanf("%d %d",&m,&n);
        if(m==0&&n==0)
        {
            printf("impossible\n");
            continue;
        }
        else if(m>=r||n>=c)
        {
            printf("impossible\n");
            continue;
        }
        while(matrix[a][b]!=0)
        {
            matrix[a][b]=0;
            if(a-1>=0)
            {
                if(b-1>=0)
                {
                    flag=MAX(matrix[a-1][b],matrix[a][b-1],matrix[a][b+1],matrix[a+1][b]);
                    switch (flag)
                    {
                    case 1:
                        a=a-1;
                        b=b;
                        break;
                    case 2:
                        a=a;
                        b=b-1;
                        break;
                    case 3:
                        a=a;
                        b=b+1;
                        break;
                    case 4:
                        a=a+1;
                        b=b;
                        break;
                    }
                }
                else
                {
                    flag=MAX(matrix[a-1][b],0,matrix[a][b+1],matrix[a+1][b]);
                    switch (flag)
                    {
                    case 1:
                        a=a-1;
                        b=b;
                        break;
                    case 2:
                        a=a;
                        b=b;
                        break;
                    case 3:
                        a=a;
                        b=b+1;
                        break;
                    case 4:
                        a=a+1;
                        b=b;
                        break;
                    }
                }

            }
            else
            {
                if(b-1>=0)
                {
                    flag=MAX(0,matrix[a][b-1],matrix[a][b+1],matrix[a+1][b]);
                    switch (flag)
                    {
                    case 1:
                        a=a;
                        b=b;
                        break;
                    case 2:
                        a=a;
                        b=b-1;
                        break;
                    case 3:
                        a=a;
                        b=b+1;
                        break;
                    case 4:
                        a=a+1;
                        b=b;
                        break;
                    }
                }
                else
                {
                    flag=MAX(0,0,matrix[a][b+1],matrix[a+1][b]);
                    switch (flag)
                    {
                    case 1:
                        a=a;
                        b=b;
                        break;
                    case 2:
                        a=a;
                        b=b;
                        break;
                    case 3:
                        a=a;
                        b=b+1;
                        break;
                    case 4:
                        a=a+1;
                        b=b;
                        break;
                    }
                }
            }
            step++;
            if(step>=(fabs(m-a)+fabs(n-b)))
            {
                printf("%d\n",step);
                break;
            }
            else if(matrix[a][b]==0)
            {
                printf("impossible\n");
            }
        }
    }
    return 0;
}

int MAX(int a,int b,int c,int d)
{
    int max;
    max=a;
    if(max<b)
    {
        max=b;
    }
    if(max<c)
    {
        max=c;
    }
    if(max<d)
    {
        max=d;
    }
    if(max==a)
    {
        return 1;
    }
    else if(max==b)
    {
        return 2;
    }
    else if(max==c)
    {
        return 3;
    }
    else if(max==d)
    {
        return 4;
    }
}
回答
編輯回答
檸檬藍
#include <stdlib.h>
#include <iostream>
using namespace  std;
const int maxn = 102;
int a[maxn][maxn];
int si,sj;
int n,m;
int dx[]= {-1,0,1,0};
int dy[]={0,-1,0,1};
int f( int a,int b) {return a>b? (a-b):(b-a);}
void  bfs(int x,int y,int time,int &ok)
{    
    a[x][y]=0;  int r = 0,c=0,max =0;
    for ( int i =0;i<4;++i )
        if (x+dx[i]>=0&&x+dx[i]<n&&y+dy[i]>=0&&y+dy[i]<m && a[x+dx[i]][y+dy[i]]>max )
        {
            r= x+dx[i];c=y+dy[i]; max=a[r][c];
        }
    if (!r&&!c)  return;
    if (f(si,r)+f(sj,c)<=time )  {ok =time;return;}
    bfs(r,c,time+1,ok);
}


int main(int argc, char** argv) {
    while ( cin>>n>>m&&n>0&&m>0)
    {
        for ( int i =0;i<n;++i )
            for ( int j =0;j<m;++j) cin>>a[i][j];
            cin>>si>>sj;
            int ok =0;
            bfs(0,0,1,ok);
            if ( ok)  cout<<ok<<endl;
            else cout<<"impossible\n";      
    }
    return (EXIT_SUCCESS);
}
2017年4月14日 02:31