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 MMcolumns. 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

其实这道题我是很气的,  我加了队列超时,  剪枝也超时。   后面看到通过率挺高的,就怀疑是暴力水题,结果果然是这样。但是我还是写在博客上,帮助理解自己BFS;

超时代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Swap(a,b,t) t=a,a=b,b=t
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x));
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f;
const double eps=1e-12;
int n,m,k;
const int MAXN=2005;
int maxx,sum;
struct s{
    int x,y,second;
}map[MAXN][MAXN];
bool vis[MAXN][MAXN];
queue<s> q;
int dir[4][2]={-1,0,0,1,1,0,0,-1};
bool Solve(s f)
{
    if (f.x<=0||f.y<=0||f.x>n||f.y>m||vis[f.x][f.y])
        return false;
    return true;
}    
void BFS()
{
    while (!q.empty())
    {
        if (sum==0)
            break;
        s f,node;
        node=q.front();
        q.pop();
        for (int t=0;t<4;t++){
            f.x=node.x+dir[t][0];
            f.y=node.y+dir[t][1];
            f.second=node.second;
            if (Solve(f)){
                f.second=node.second+1;
                map[f.x][f.y].second=f.second;
                sum--;
                vis[f.x][f.y]=true;        
                q.push(f);
                maxx=f.second;
            }
        }
    }
}
int main()
{
    while (scanf("%d%d",&n,&m)!=EOF){
        sum=n*m;
        while (!q.empty())
            q.pop();
        memset(vis,0,sizeof(vis));
        for (int i=1;i<=n;i++){
            for (int j=1;j<=m;j++){
                map[i][j].second=0;
            }
        }
        scanf("%d",&k);

sum-=k;
        int x,y,flag=1;
        for (int i=1;i<=k;i++){
            scanf("%d%d",&x,&y);
            map[x][y].x=x;
            map[x][y].y=y;
            map[x][y].second=1;
            vis[x][y]=true;
            q.push(map[x][y]); 
        }
        if (n*m<=k){
            printf("1 1\n");
            continue;
        }        
        BFS();
    /*    for (int i=1;i<=n;i++){
            for (int j=1;j<=m;j++)
                printf("%d ",map[i][j].second);
            cout<<endl;
        }    */    
        for (int i=1;i<=n&&flag;i++){
            for (int j=1;j<=m;j++){
                if (map[i][j].second==maxx){
                    flag=0;
                    printf("%d %d\n",i,j);
                    break;
                }
            }
        }
    }
    return 0;
}

暴力AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Swap(a,b,t) t=a,a=b,b=t
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x));
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f;
const double eps=1e-12;
int MAX;
int map[2010][2010];
int main()
{
    int n,m,k,x,y,cnt;
    while (scanf("%d%d",&n,&m)!=EOF){
        MAX=-inf;
        scanf("%d",&k);
        int g=k;
        MemX(map);
        while (k--){
//            system("pause");
            cin>>x>>y;
            for (int i=1;i<=n;i++){
                for (int j=1;j<=m;j++){
                    if (i==x||j==y)
                        map[i][j]=Min(map[i][j],abs(x-i)+abs(y-j));
                    else 
                        map[i][j]=Min(map[i][j],abs(x-i)+abs(y-j)-1);    
                }
            }        
        }
        for (int i=1;i<=n;i++){
            for (int j=1;j<=m;j++){
                if (map[i][j]>MAX)
                    MAX=map[i][j];
            }
        }
/*    */    for (int i=1,flag=1;flag&&i<=n;i++){
            for (int j=1;j<=m;j++){
                if (MAX==map[i][j]){
                    flag=0;
                    printf("%d %d\n",i,j);
                    break;
                }
            }
        }
    }
    return 0;
}

BFS 队列的更多相关文章

  1. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

  2. 农夫过河 (BFS)(队列)

    1 .问题描述 要求设计实现农夫过河问题(农夫带着一只狼,一只养,一棵白菜,一次只能带一个东西)如何安全过河. 2 .问题的解决方案: 可以用栈与队列.深度优先搜索算法及广度优先搜索算法相应的原理去解 ...

  3. 拆边+BFS队列骚操作——cf1209F

    这个拆边+队列操作实在是太秒了 队列头结点存的是一个存点集的vector,1到这个点集经过的路径权值是一样的,所以向下一层拓展时,先依次走一遍每个点的0边,再走1边...以此类推,能保证最后走出来的路 ...

  4. POJ——3278 Catch That Cow(BFS队列)

    相比于POJ2251的三维BFS,这道题做法思路完全相同且过程更加简单,也不需要用结构体,check只要判断vis和左右边界的越界情况就OK. 记得清空队列,其他没什么好说的. #include< ...

  5. hdoj 2612 Find a way【bfs+队列】

    Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. poj 3278 Catch That Cow(bfs+队列)

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  7. HDU 6171 Admiral(双向BFS+队列)题解

    思路: 最大步骤有20,直接BFS会超时. 因为知道开始情况和结果所以可以用双向BFS,每个BFS规定最大步骤为10,这样相加肯定小于20.这里要保存每个状态搜索到的最小步骤,用Hash储存.当发现现 ...

  8. 剑指offer:对称的二叉树(镜像,递归,非递归DFS栈+BFS队列)

    1. 题目描述 /** 请实现一个函数,用来判断一颗二叉树是不是对称的. 注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的 */ 2. 递归 思路: /** 1.只要pRoot.left和 ...

  9. bfs(队列模板)

    [题目描述] 当你站在一个迷宫里的时候,往往会被错综复杂的道路弄得失去方向感,如果你能得到迷宫地图,事情就会变得非常简单. 假设你已经得到了一个n*m的迷宫的图纸,请你找出从起点到出口的最短路. [输 ...

随机推荐

  1. python接口测试-项目实践(七)脚本优化

    七 脚本优化:重复代码的提取成函数:与项目接口相关的都封装到一个类中:添加手工验证脚本,增加输入值的判断逻辑 将所有与该项目接口相关的封装成类 class ProjectApi: #3个数据源接口 d ...

  2. python:递归函数

    1,初识递归函数 1)什么是递归函数? 在函数中自己调用自己叫做递归函数 递归函数超过一定程度会报错.---RecursionError: maximum recursion dep th excee ...

  3. React中的响应式设计思想和事件绑定

    这两个点是react入门非常重要的两个点,以前我们是直接操作dom的形式去做,react的设计思想和以前直接操作dom是完全不同的,react是一个响应式的框架,他在做编程的时候,强调的是我们不要直接 ...

  4. redis未授权访问getshell

    redis未授权访问的问题一年前就爆了,当时刚开始学安全,还不太懂.今天借着工作的机会来搞一把,看看能不能拿下一台服务器.其实前几天就写好了一直想找个实际环境复现一下,一直没有找到,只说下大致思路. ...

  5. linux shell——zsh的安装与使用

    Shell是在程序员与服务器间建立一个桥梁,它对外提供一系列命令,让我们得以控制服务器.常用的Bash就是Shell的一种,也是Linux下默认Shell程序.这里介绍一种更强大的.更人性化的Shel ...

  6. 最短路算法 —— Dijkstra算法

    用途: 解决单源最短路径问题(已固定一个起点,求它到其他所有点的最短路问题) 算法核心(广搜): (1)确定的与起点相邻的点的最短距离,再根据已确定最短距离的点更新其他与之相邻的点的最短距离. (2) ...

  7. PHP设计模式——装饰器模式

    <?php /** * 装饰器模式 * 如果已有对象的部分内容或功能发生变化,但是不需要修改原始对象的结构,应使用装饰器模式 * * 为了在不修改对象结构的前提下对现有对象的内容或功能稍加修改, ...

  8. 2017.9.5 Java知识点总结

    1.*程序的数据操作往往都在内存中操作的,也就是说数据运算都在内存中完成. 2.*什么是变量? --变量就是内存中的一块用来存放数据的存储单元. --变量中的值可变 --我们通过变量名就可以找到内存中 ...

  9. SpringBoot Docs

    http://docs.spring.io/spring-boot/docs/1.2.3.RELEASE/reference/html/boot-features-external-config.ht ...

  10. Linux 安装ngnix

    第一步:把nginx的源码包上传到linux系统/home 第二步:解压缩 [root@localhost ~]# tar zxf nginx-1.8.0.tar.gz 第三步:使用configure ...