ACM-ICPC Asia Training League 暑假第一阶段第六场
The 2018 ACM-ICPC China JiangSu Provincial Programming Contest
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 NNrows 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
对于多远广搜,一次压入多个起点到队列中,再用广搜搜索
AC:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#define inf 0x3f3f3f
int tx[4]={0,1,0,-1};
int ty[4]={1,0,-1,0};
struct node
{
int x,y,step;
};
using namespace std;
int n,m,k,maxi;
int mp[2001][2001];
int vis[2001][2001];
int ex[1024],ey[1024];
void bfs()
{
queue<node> que;
node q,p;
for(int i=0;i<k;i++)///同时压入所以起点
{
q.x=ex[i],q.y=ey[i],q.step=0;
vis[ex[i]][ey[i]]=0;
que.push(q);
}
while(!que.empty())
{
q=que.front();
que.pop();
for(int i=0;i<4;i++)
{
p=q,p.x+=tx[i],p.y+=ty[i];
if(p.x<1||p.y<1||p.x>n||p.y>m) continue;
if(vis[p.x][p.y]>=0) continue;
p.step++;
vis[p.x][p.y]=p.step;
que.push(p);
}
}
}
int main()
{
int flag,xi,xj;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(vis,-1,sizeof(vis));
maxi=0,flag=0;
scanf("%d",&k);
for(int i=0;i<k;i++)
scanf("%d%d",&ex[i],&ey[i]);
bfs();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(vis[i][j]>maxi)
maxi=vis[i][j],xi=i,xj=j;
printf("%d %d\n",xi,xj);
}
return 0;
}