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 ...
随机推荐
- php构造函数的继承方法
第一种情况:子类没有定义构造函数时,默认继承.例子: ? 1 2 3 4 5 6 7 8 9 10 11 12 <?php class A{ public $name; function _ ...
- ssm+dubbo/zk
1.原始 Connection conn = null; String url = "jdbc:mysql://localhost:3306/emps?user=root&passw ...
- POJ 1577 Falling Leaves(二叉搜索树)
思路:当时学长讲了之后,似乎有点思路----------就是倒着建一个 二叉搜索树 代码1:超时 详见超时原因 #include<iostream> #include<cstrin ...
- python习题-产生8位随机密码要包含大小写及数字
# 1.写一个产生密码的程序,# 输入次数,输入多少次就产生多少条数据,# 要求密码必须包含大写字母.小写字母和数字,长度8位,不能重复 #需求分析#1.循环,输入什么就循环多少次#2.随机来取值,是 ...
- C++ STL源码剖析
stl_config.h defalloc.h stl_alloc.h memory.cpp stl_construct.h stl_uninitialized.h stl_iterator.h ty ...
- ACM学习历程—BestCoder Round #75
1001:King's Cake(数论) http://acm.hdu.edu.cn/showproblem.php?pid=5640 这题有点辗转相除的意思.基本没有什么坑点. 代码: #inclu ...
- java的HashMap的几个问题
HashMap处理hash冲突的几种方法 一. 开放定址法 Hi=(H(key) + di) MOD m i=1,2,...k(k<=m-1)其中H(key)为哈希函数:m为哈希表表长:di为增 ...
- BZOJ1727:[Usaco2006 Open]The Milk Queue挤奶队列
我对\(Jhonson\)算法的理解:https://www.cnblogs.com/AKMer/p/9863620.html 题目传送门:https://www.lydsy.com/JudgeOnl ...
- Python图片识别——人工智能篇
一.安装pytesseract和PIL PIL全称:Python Imaging Library,python图像处理库,这个库支持多种文件格式,并提供了强大的图像处理和图形处理能力. 由于PIL仅 ...
- 集合对象与自定义javabean对象接收数据库查询的数据 (基础知识扫盲)
一.集合对象(List,Map,数组)等对象接收数据库查询的记录,如果没有一条记录,就得到的内容为空的集合,不是null: 例如:List查不到记录得到的就是size=0的list 二.自定义的jav ...