思路:

靠评测机抖一抖的思路:

拿个队列维护一下符合类型的可以搜索(指四周还存在可以遍历的点)的点。然后暴力搜索,所以问题来了,这个暴力搜索会大大地重复遍历次数。

DFS遍历图以前一直忽略重复,以为搜到打个标记复杂度就很棒棒了,其实还是有一堆重复。

这个思路的代码见第一份。

正解:

那么问题就很明显了,为了减少遍历次数,所以我们BFS,每次仅遍历附近四个,然后拿优先队列维护天数小的,类型小的,这样复杂度仅仅是多了个log,减少了很多重复遍历次数。

第一份代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#include <queue>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
const int N = 250010; struct asd{
int tp;
int day;
int x,y;
}q[N];
int num;
bool cmp(asd a,asd b){
return a.tp<b.tp;
} int cnt[N];
int n,m;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int ma[550][550];
int type[550][550];
queue<asd>que; bool Judge(int x,int y)
{
if(x<0||y<0||x>=n||y>=m) return false;
return true;
} bool Check(int x,int y)
{
for(int i=0;i<4;i++){
int xx=x+dx[i];
int yy=y+dy[i];
if(Judge(xx,yy)){
if(type[xx][yy]==-1) return true;
}
}
return false;
} void init()
{
num=0;
while(!que.empty()) que.pop();
memset(type,-1,sizeof(type));
memset(cnt,0,sizeof(cnt));
} void DFS(asd now){
asd nex;
bool flag=false;
for(int i=0;i<4;i++){
int xx=now.x+dx[i];
int yy=now.y+dy[i];
if(!Judge(xx,yy)) continue;
if(type[xx][yy]!=-1) continue;
if(abs(ma[xx][yy])>now.day)
{
if(!flag){
nex=now;
nex.day=now.day+1;
que.push(nex);
flag=true;
}
continue;
}
type[xx][yy]=now.tp;
nex.day=now.day;
nex.x=xx;nex.y=yy;
nex.tp=now.tp;
DFS(nex);
}
} int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%d",&ma[i][j]);
if(ma[i][j]>0){
type[i][j]=ma[i][j];
q[num].day=1;
q[num].tp=ma[i][j];
q[num].x=i;
q[num].y=j;
num++;
}
}
}
sort(q,q+num,cmp);
for(int i=0;i<num;i++)
{
if(Check(q[i].x,q[i].y))
que.push(q[i]);
} asd now;
while(!que.empty())
{
now=que.front();que.pop();
DFS(now);
} for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cnt[type[i][j]]++; int qq,x;
scanf("%d",&qq);
while(qq--){
scanf("%d",&x);
printf("%d\n",cnt[x]);
}
}
return 0;
}

第二份正解:百度一堆都是一样的吧。

最新文章

  1. [NHibernate]关联映射
  2. ubuntu安装配置elasticSearch(vagrant)
  3. 24.编写一个Car类,具有String类型的属性品牌,具有功能drive; 定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速; 定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特 性。
  4. PAT 1021. 个位数统计 (15)
  5. vim实现全选功能
  6. Spring Integration - 自动轮询发送手机短信
  7. .NET 知识
  8. 记一个奇怪的python异常处理过程
  9. Inno Setup 打包工具总结
  10. 解析XtraBackup备份MySQL的原理和过程(转)
  11. selenium webdriver(5)---超时设置
  12. C风格字符串和C++ string 对象赋值操作的性能比较
  13. 六、Hadoop学习笔记————调优之操作系统以及JVM
  14. Java设计模式之策略设计模式
  15. Jetson TX2安装tensorflow(原创)
  16. PermutationTwo
  17. 关于部署php遇到的坑
  18. win10 adb(Android Debug Bridge)导出日志
  19. 【Linux】&amp;、&amp;&amp;、|、||的用法说明
  20. BZOJ 4826: [Hnoi2017]影魔 单调栈 主席树

热门文章

  1. Buffer类的详解(转)
  2. webpack 教程资源目录
  3. Python基础之元组操作
  4. ES查看segment大小
  5. 一文读懂所有的编码方式(UTF-8、GBK、Unicode、宽字节...)
  6. leetcode 201. Bitwise AND of Numbers Range(位运算,dp)
  7. 多线程编程-pthread 未定义的引用
  8. ACdream1430SETI(后缀自动机)
  9. JavaScript RegExp 正则表达式基础详谈
  10. YARN指令