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*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 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 X firstly, smallest Y secondly.
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 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 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.
输出
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
记录每个城市感染的天数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <bits/stdc++.h> #define ll long long using namespace std; int n,m,k; int mapp[2010][2010]; int node[4000010][2]; int main() { while(scanf("%d %d",&n,&m)!=EOF){ scanf("%d",&k); memset(mapp,0,sizeof(mapp)); memset(node,0,sizeof(node)); int top=0; int x,y; for(int i=1;i<=k;i++){ scanf("%d %d",&x,&y); mapp[x][y]=1; top++; node[top][0]=x; node[top][1]=y; } int tail=1; while(tail<=top){ x=node[tail][0]; y=node[tail][1]; if(x-1>=1){ if(!mapp[x-1][y]){ mapp[x-1][y]=mapp[x][y]+1; top++; node[top][0]=x-1; node[top][1]=y; } } if(x+1<=n){ if(!mapp[x+1][y]){ mapp[x+1][y]=mapp[x][y]+1; top++; node[top][0]=x+1; node[top][1]=y; } } if(y-1>=1){ if(!mapp[x][y-1]){ mapp[x][y-1]=mapp[x][y]+1; top++; node[top][0]=x; node[top][1]=y-1; } } if(y+1<=m){ if(!mapp[x][y+1]){ mapp[x][y+1]=mapp[x][y]+1; top++; node[top][0]=x; node[top][1]=y+1; } } tail++; } int fl=-1; x=y=0; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(mapp[i][j]>fl){ fl=mapp[i][j]; x=i; y=j; } } } cout<<x<<" "<<y<<endl; } return 0; }
不要忘记努力,不要辜负自己
欢迎指正 QQ:1468580561