Plague Inc

题目:

Plague Inc. is a famous game, which player develop virus to ruin the world.

JSZKC wants to model this game. Let's consider the world has N\times MN×M cities. The world has NN rows and MM columns. The city in the XX row and the YY column has coordinate (X,Y)(X,Y).

There are KK cities which are sources of infection at the 0^{th}0th day. Each day the infection in any infected city will spread to the near four cities (if exist).

JSZKC wants to know which city is the last one to be infected. If there are more than one cities , answer the one with smallest XX firstly, smallest YY secondly.

Input Format

The input file contains several test cases, each of them as described below.

  • The first line of the input contains two integers NN and MM (1 \le N,M \le 2000)(1≤N,M≤2000), giving the number of rows and columns of the world.
  • The second line of the input contain the integer KK (1 \le K \le 10)(1≤K≤10).
  • Then KK lines follow. Each line contains two integers X_iXi​ and Y_iYi​, indicating (X_i,Y_i)(Xi​,Yi​) is a source. It's guaranteed that the coordinates are all different.

There are no more than 2020 test cases.

Output Format

For each testcase, output one line with coordinate XX and YY separated by a space.

样例输入

3 3
1
2 2
3 3
2
1 1
3 3

样例输出

1 1
1 3

题目来源

The 2018 ACM-ICPC China JiangSu Provincial Programming Contest

分析:

很显然的广搜问题,最后入队的点就是最后一个感染的点;

代码:
 

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
struct node
{
    int xx;
    int yy;
    int step;
}head,tail,maxx;
queue<node>q;
int nextt[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int mapp[2005][2005];
bool book[2005][2005];
int n,m,k,xxx,yyy;
bool charge(int a,int b)
{
    if(a<=0||b<=0||a>n||b>m||(book[a][b]==1))
        return 0;
    else
        return 1;
}
int main()
{
    int i,j,tx,ty;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        memset(book,0,sizeof(book));
        scanf("%d",&k);
        for(i=0;i<k;i++)
        {
            scanf("%d %d",&xxx,&yyy);
            head.xx=xxx;
            head.yy=yyy;
            head.step=0;
            q.push(head);
            book[xxx][yyy]=1;
        }
        if(k==m*n)
        {
            printf("1 1\n");
            continue;
        }
        maxx.step=-1;
        while(!q.empty())
        {
            head=q.front();
            q.pop();
            for(i=0;i<4;i++)
            {
                tx=head.xx+nextt[i][0];
                ty=head.yy+nextt[i][1];
                if(charge(tx,ty))
                {
                    book[tx][ty]=1;
                    tail.xx=tx;
                    tail.yy=ty;
                    tail.step=head.step+1;
                   /* if(tail.step>maxx.step)
                    {
                        maxx.xx=tx;
                        maxx.yy=ty;
                        maxx.step=tail.step;
                    }
                    else if(tail.step==maxx.step)
                    {
                        if(tx<maxx.xx)
                        {
                            maxx.xx=tx;
                            maxx.yy=ty;
                            maxx.step=tail.step;
                        }
                        else if(tx==maxx.xx&&ty<maxx.yy)
                        {
                            maxx.xx=tx;
                            maxx.yy=ty;
                            maxx.step=tail.step;
                        }
                    }*/
                    if(tail.step==maxx.step)
                    {
                        if(tx==maxx.xx&&ty<maxx.yy)
                        {
                            maxx.xx=tx;
                            maxx.yy=ty;
                        }
                        if(tx<maxx.xx)
                        {
                            maxx.xx=tx;
                            maxx.yy=ty;
                        }
                    }
                    if(tail.step>maxx.step)
                    {
                        maxx.xx=tx;
                        maxx.yy=ty;
                        maxx.step=tail.step;
                    }
                    q.push(tail);
                    //printf("%d %d\n",maxx.xx,maxx.yy);
                }
            }
        }
        printf("%d %d\n",maxx.xx,maxx.yy);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值