【POJ】2329 Nearest number - 2(搜索)
题目
传送门:QWQ
分析
在dp分类里做的,然而并不会$ O(n^3) $ 的$ dp $,怒写一发搜索。
看起来是$ O(n^4) $,但仔细分析了一下好像还挺靠谱的?
poj挂了,没在poj交,在zoj上交的500ms
P.S. 如果要在poj交还要把多数据改成单数据
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn=;
int A[maxn][maxn], num[maxn*maxn], id[], cnt, ok[maxn][maxn], n;
int now[maxn][maxn], ans[maxn][maxn], can[maxn][maxn], vis[maxn][maxn];
int dx[]={,,,-}, dy[]={,-,,}; struct Node{ int x,y,dis,numm; }; bool in(int x,int y){ return x>=&&x<=n&&y>=&&y<=n; } queue<Node> que;
void bfs(int x,int y,int dis,int numm){
que.push((Node){x,y,dis,numm});
while(!que.empty()){
Node a=que.front(); que.pop();
x=a.x; y=a.y; dis=a.dis++; numm=a.numm;
for(int i=;i<;i++){
int px=x+dx[i], py=y+dy[i];
if(!in(px,py) || vis[px][py] || ok[px][py]) continue;
vis[px][py]=; a.x=px; a.y=py;
if(dis< now[px][py]){ans[px][py]=numm;can[px][py]=;now[px][py]=dis;}
if(dis==now[px][py]){can[px][py]++;}
que.push(a);
}
} }
int main(){
int t; scanf("%d",&t);
while(t--){
cnt=; memset(id,,sizeof(id)); memset(num,,sizeof(num)); memset(ok,,sizeof(ok));
memset(ans,,sizeof(ans)); memset(can,,sizeof(can));
scanf("%d",&n);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
scanf("%d",&A[i][j]);
if(A[i][j]) id[A[i][j]]=++cnt, num[cnt]=A[i][j], ok[i][j]=;
}
memset(now,,sizeof(now));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
if(!ok[i][j]) continue;
vis[i][j]=; bfs(i,j,,id[A[i][j]]);
memset(vis,,sizeof(vis));
}
for(int i=;i<=n;i++){
for(int j=;j<n;j++){
if(!ok[i][j] && can[i][j]==) printf("%d ",num[ans[i][j]]);
else printf("%d ",A[i][j]);
}
if(!ok[i][n] && can[i][n]==) printf("%d\n",num[ans[i][n]]);
else printf("%d\n",A[i][n]);
} if(t!=) puts("");
}
return ;
}
【POJ】2329 Nearest number - 2(搜索)的更多相关文章
- [NewTrain 10][poj 2329]Nearest Number - 2
题面: http://poj.org/problem?id=2329 题解: 这题有很多做法 1. 搜索 复杂度$O(n^4)$ 但是实际上远远达不到这个复杂度 所以可以通过 2. 对于每一个格子,我 ...
- [POJ 2329] Nearest number-2
Link: POJ 2329 传送门 Solution: 比较明显的$dp$,但爆搜好像也能过 用多个方向$dp$来解决此题,最后汇总答案即可 一开始我写了4个,但后来发现只要相反的2个方向即可,同时 ...
- POJ 2329 (暴力+搜索bfs)
Nearest number - 2 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3943 Accepted: 1210 De ...
- POJ - 1330 Nearest Common Ancestors(基础LCA)
POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %l ...
- POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
- LCA POJ 1330 Nearest Common Ancestors
POJ 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24209 ...
- POJ 1330 Nearest Common Ancestors(lca)
POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...
- POJ-2329 Nearest number - 2(BFS)
Nearest number - 2 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4100 Accepted: 1275 De ...
- POJ.1330 Nearest Common Ancestors (LCA 倍增)
POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...
随机推荐
- GreenPlum的Primary和Mirro切换恢复
gp节点出现了acting as primary change tracking错误,判断是节点primary和mirror发生了切换 1.没有配置gp的日志,无法获取为什么切换了,待会儿看看默认日志 ...
- MySQL 不开启slave如何完成异地复制
1,分批次通过远程的binlog来进行数据加载 业务新需求,线上数据库数据拉到本次,但是不允许开启slave服务,不建立直接外网的数据库账号,也不能打通数据库对外网的网络,所以我们测试环境无法通过普通 ...
- HashSet实现原理及源码分析
HashSet简介 HashSet是Set接口实现,它按照Hash算法来存储集合中的元素 不保证元素顺序 HashSet是非同步的,如果多个线程同时访问一个HashSet,要通过代码来保证其同步 集合 ...
- 从互联网进化的角度看AI+时代的巨头竞争
今天几乎所有的互联网公司在谈论和布局人工智能,收购相关企业.人工智能和AI+成为当今科技领域最灸手可热的名词,关于什么是AI+,其概念就是用以表达将"人工智能"作为当前行业科技化发 ...
- D3.js 使用心得
教程 D3.js 入门教程系列 http://www.ourd3js.com/wordpress/296/ 全球地图数据
- jQuery AJAX 与 jQuery 事件
jQuery 本身即是为事件处理而特别设计的,jQuery 事件处理方法是 jQuery 中的核心函数. $(function() { ... }); 是如下格式的缩写: $(document).re ...
- 【剑指offer】09-2跳台阶,C++实现
原创博文,转载请注明出处! # 本文是牛客网<剑指offer>刷题笔记 1.题目 # 一只青蛙一次可以跳1级台阶,也可以跳2级.求该青蛙跳n级的台阶总共有多少种跳法. 2.思路 # 跳0级 ...
- ranch分析学习(二)
紧接上篇,今天我们来分析监督树的工作者,打工仔执行任务的人.废话不多少我们直接进入正题. 3.ranch_server.erl 整个文件的功能主要是存储tcp对应参数的的信息.信息的存储方式采用的 ...
- BZOJ - 3295 动态逆序对 (树状数组套treap)
题目链接 思路和bzoj2141差不多,不过这道题的数据更强一些,线段树套treapT了,树状数组套treap卡过~~ #include<bits/stdc++.h> using name ...
- 《DSP using MATLAB》示例Example8.2
代码: N = 3; OmegaC = 0.5; % Direct form [b, a] = u_buttap(N, OmegaC); [C, B, A] = sdir2cas(b, a) 运行结果 ...