Plague Inc

7553: Plague Inc

时间限制: 2 Sec  内存限制: 64 MB
提交: 117  解决: 23
[提交] [状态] [讨论版] [命题人:admin]

题目描述
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*M cities. The world has N rows and M columns. The city in the X row and the Y column has coordinate (X,Y). 
There are K cities which are sources of infection at the 0 th 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 X firstly, smallest Y secondly.  

 

输入
The input file contains several test cases, each of them as described below. 
  • The first line of the input contains two integers N and M (1 ≤ N,M ≤ 2000), giving the  number of rows and columns of the world.   
  • The second line of the input contain the integer K (1 ≤K ≤ 10).   
  • Then K lines follow. Each line contains two integers Xi and Yi, indicating (X i ,Y i ) is a source. It’s guaranteed that the coordinates are all different. 
There are no more than 20 test cases. 

 

输出
For each testcase, output one line with coordinate X and Y separated by a space. 

 

样例输入

复制样例数据

3 3
1
2 2
3 3
2
1 1
3 3
样例输出
1 1
1 3
思路:标记每个起始点为1,每次bfs到一个点次数加1,注意要判断一下是否以及访问过了。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include<bits/stdc++.h>
#define REP(i, a, b) for(int i = (a); i <= (b); ++ i)
#define REP(j, a, b) for(int j = (a); j <= (b); ++ j)
#define PER(i, a, b) for(int i = (a); i >= (b); -- i)
using ll = long long;
using namespace std;
const int maxn=1e5+5;
template <class T>
inline void rd(T &ret){
    char c;
    ret = 0;
    while ((c = getchar()) < '0' || c > '9');
    while (c >= '0' && c <= '9'){
        ret = ret * 10 + (c - '0'), c = getchar();
    }
}
struct node{int x,y;}p[15];
queue<node>q;
int n,m,k,val,w[2005][2005];
void bfs(){
     while(!q.empty()){
          node tmp=q.front();
          q.pop();
          int cx=tmp.x,cy=tmp.y,nx,ny;
          val=max(val,w[cx][cy]);
          REP(i,1,4){
              if(i==1)nx=cx-1,ny=cy;
              else if(i==2)nx=cx+1,ny=cy;
              else if(i==3)nx=cx,ny=cy-1;
              else if(i==4)nx=cx,ny=cy+1;
              if(w[nx][ny]||nx<1||ny<1||nx>n||ny>m)continue;
              w[nx][ny]=w[cx][cy]+1;
              q.push(node{nx,ny});
          }
     }
     return;
}
int main(){
    while(~scanf("%d%d",&n,&m)){
        val=0;
        scanf("%d",&k);
        memset(w,0,sizeof(w));
        REP(i,1,k){
             int x,y;
             scanf("%d%d",&x,&y);
             node tmp;
             tmp.x=x,tmp.y=y,w[x][y]=1;
             q.push(tmp);
        }
        bfs();
        bool f=false;
        REP(i,1,n){
            REP(j,1,m){
                if(w[i][j]==val){
                    cout<<i<<' '<<j<<endl,f=true;
                    break;
                }
            }
            if(f)break;
        }
    }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/czy-power/p/10392981.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值