【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节 ...
随机推荐
- Python中的数据结构 --- 元组(tuple)、字典(tuple)
元组(tuple)本身是不可变数据类型,没有增删改查:元组内可以存储任意数据类型一.元组的创建 例:t = (1,2.3,'star',[1,2,3]) ## 元组里面包含可变类型,故 ...
- vue.js 源代码学习笔记 ----- 工具方法 perf
import { inBrowser } from './env' export let mark export let measure if (process.env.NODE_ENV !== 'p ...
- 《利用Python进行数据分析》笔记---第4章NumPy基础:数组和矢量计算
写在前面的话: 实例中的所有数据都是在GitHub上下载的,打包下载即可. 地址是:http://github.com/pydata/pydata-book 还有一定要说明的: 我使用的是Python ...
- anjular2以及微信小程序的一点比较
1条件渲染: 小程序:用 wx:if="{{condition}}" 来判断是否需要渲染该代码块. <view wx:if="{{condition}}" ...
- gdi+ 中发生一般性错误 wpf解决方法
错误背景:原来在winform程序中写了一个窗口,在wpf应用程序中调用显示了这个窗口,有个头像功能,加载本地的一个图片文件,加载前进行了各种逻辑判断,效果如下: 而加载的关键代码如下面: pictu ...
- key相同合并Map
public class Demo11 { public static void main(String[] args) { ConcurrentHashMap<Integer, Map< ...
- Android 4.0 Camera架构分析之Camera初始化
Android Camera 采用C/S架构,client 与server两个独立的线程之间使用Binder通信,这已经是众所周知的了.这里将介绍Camera从设备开机,到进入相机应用是如何完成初始化 ...
- 为Linux服务器的SSH登录启用Google两步验证
对于Linux服务器而言使用密钥登录要比使用密码登录安全的多,毕竟当前网上存在多个脚本到处进行爆破. 这类脚本都是通过扫描IP端的开放端口并使用常见的密码进行登录尝试,因此修改端口号也是非常有必要的. ...
- Java 多线程 2015/9/21
http://lavasoft.blog.51cto.com/62575/27069 http://blog.csdn.net/aboy123/article/details/38307539 ...
- 编程之美Ex1——求二进制中1的个数
又被阿里机考虐了一次,决定改变策略开始刷题T^T 一个字节(8bit)的无符号整型,求其二进制中的“1”的个数,算法执行效率尽可能高. 最先想到的移位操作,末尾位&00000001,然后右移, ...