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;
}
 

猜你喜欢

转载自www.cnblogs.com/czy-power/p/10392981.html
今日推荐