ZOJ 2849【瞎暴力的搜索】
思路:
靠评测机抖一抖的思路:
拿个队列维护一下符合类型的可以搜索(指四周还存在可以遍历的点)的点。然后暴力搜索,所以问题来了,这个暴力搜索会大大地重复遍历次数。
这个思路的代码见第一份。
正解:
那么问题就很明显了,为了减少遍历次数,所以我们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;
}
第二份正解:百度一堆都是一样的吧。
ZOJ 2849【瞎暴力的搜索】的更多相关文章
- 汕头市队赛 SRM14 T1 计算几何瞎暴力
计算几何瞎暴力 (easy.pas/c/cpp) 128MB 1s 在平面上,给定起点和终点,有一面墙(看作线段)不能穿过,问从起点走到终点的最短路程. 输入格式 输入一行,包含8个用空格分隔的整数x ...
- loj517 计算几何瞎暴力
在序列上维护4个操作 1.在序列的尾端添加x 2.输出Al~Ar的和 3.将所有数异或x 4.将序列从小到大排序 第一眼看上去是Splay于是头铁硬刚了一发 后来发现splay没法异或 去百度“维护异 ...
- 玲珑杯”ACM比赛 Round #18 A -- 计算几何你瞎暴力(瞎暴力)
题目链接:http://www.ifrog.cc/acm/problem/1143?contest=1020&no=0 题解:就是瞎暴力具体多暴力看一下代码就知道了. #include < ...
- LibreOJ #517. 「LibreOJ β Round #2」计算几何瞎暴力
二次联通门 : LibreOJ #517. 「LibreOJ β Round #2」计算几何瞎暴力 /* LibreOJ #517. 「LibreOJ β Round #2」计算几何瞎暴力 叫做计算几 ...
- zoj 3644(dp + 记忆化搜索)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 思路:dp[i][j]表示当前节点在i,分数为j的路径条数,从 ...
- 玲珑杯 round18 A 计算几何瞎暴力
题目链接 : http://www.ifrog.cc/acm/problem/1143 当时没看到坐标的数据范围= =看到讨论才意识到,不同的坐标最多只有1k多个,完全可以暴力做法,不过也要一些技巧. ...
- hihocoder 微软编程之美2015 初赛 第一场 (树算法 + 暴力思想 + 搜索思想)
题目1 : 彩色的树 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定一棵n个节点的树,节点编号为1, 2, …, n.树中有n - 1条边,任意两个节点间恰好有一条路 ...
- ZOJ Seven-Segment Display 暴力dfs + 剪枝
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3954 0 = on 1 = off A seven segment ...
- 玲珑杯 Round #18 A -- 计算几何你瞎暴力
因为坐标都在1-10之间,因此可暴力求解 #include <iostream> #include <cstdio> #include <cstring> #inc ...
随机推荐
- Mysql备份和还原(命令)
1.备份方法一 ①.进入数据库 mysql -uroot -p pwd; ②.查看数据库 show databases; ③.备份数据库 mysqldump -hlocalhost -uroot(用户 ...
- Unity3D之Mesh(五)绘制圆
前言: Unity3D中Mesh的基本单位是三角形,而圆形就是由许许多多的三角形组成的.那么我们就知道了绘制圆形的Mesh需要两个变量:圆的半径 以及分割数: 一.实现过程 基本过程与之前的类似,最 ...
- 具有增、删、改、查功能的vue-tree树组件
最近写了一个具有增删改查功能的多级树组件,感觉很实用,啦啦啦啦, 废话不多说,看代码: tree.vue <template> <div> <div class=&quo ...
- 数据库ACID和mvcc
一.数据库的ACID性: 原子性(atomicity).一致性(consistency).隔离性(isolation).持久性(durability). 二.原子性 1.原子性:一个事务要么全部完成, ...
- 设置Suse linux 用户远程登录超时时间
执行 # echo "export TMOUT=900" >> /etc/profile 查询设置结果: # cat /etc/profile|grep TMOU ...
- 每天一个linux命令(10):touch命令
版权声明更新:2017-05-14博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的mv命令. 2. ...
- 1068 Find More Coins (30)(30 分)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...
- TVYJ1266:费解的开关
我对状态空间的理解:https://www.cnblogs.com/AKMer/p/9622590.html 题目传送门:http://www.joyoi.cn/problem/tyvj-1266 这 ...
- 微服务理论之六:ESB与SOA的关系
一.SOA和ESB一直是没有明确概念的两个缩略词 SOA----面向服务架构,实际上强调的是软件的一种架构,一种支撑软件运行的相对稳定的结构,表面含义如此,其实SOA是一种通过服务整合来解决系统集成的 ...
- Linux编程里getopt_long_only函数用法详解
在程序中难免需要使用命令行选项,可以选择自己解析命令行选项,但是有现成的,何必再造轮子.下面介绍使用getopt_long_only和getopt_long(两者用法差不多)解析命令行选项. 程序中主 ...