题目

传送门: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(搜索)的更多相关文章

  1. [NewTrain 10][poj 2329]Nearest Number - 2

    题面: http://poj.org/problem?id=2329 题解: 这题有很多做法 1. 搜索 复杂度$O(n^4)$ 但是实际上远远达不到这个复杂度 所以可以通过 2. 对于每一个格子,我 ...

  2. [POJ 2329] Nearest number-2

    Link: POJ 2329 传送门 Solution: 比较明显的$dp$,但爆搜好像也能过 用多个方向$dp$来解决此题,最后汇总答案即可 一开始我写了4个,但后来发现只要相反的2个方向即可,同时 ...

  3. POJ 2329 (暴力+搜索bfs)

    Nearest number - 2 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3943 Accepted: 1210 De ...

  4. POJ - 1330 Nearest Common Ancestors(基础LCA)

    POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %l ...

  5. POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)

    POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...

  6. LCA POJ 1330 Nearest Common Ancestors

    POJ 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24209 ...

  7. POJ 1330 Nearest Common Ancestors(lca)

    POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...

  8. POJ-2329 Nearest number - 2(BFS)

    Nearest number - 2 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4100 Accepted: 1275 De ...

  9. POJ.1330 Nearest Common Ancestors (LCA 倍增)

    POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...

随机推荐

  1. IR Cut Filter

    IR cut filter,即红外截止滤光片,它放在于LENS与Sensor之间.因人眼与CMOS Sensor对各波长的响应不同,人眼看不到红外光但sensor会感应,因此需要IR cut filt ...

  2. Nginx 作为反向代理优化要点proxy_buffering

    当nginx用于反向代理时,每个客户端将使用两个连接:一个用于响应客户端的请求,另一个用于到后端的访问: 那么,可以从如下配置起步: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  3. HDU 3378

    http://acm.hdu.edu.cn/showproblem.php?pid=3378 规则去玩三国杀就理解了 纯模拟 注意的点:有已经分出胜负但还在杀的情况出现,所以要每次杀操作前判断是否分出 ...

  4. (腾讯视频)iOS开发之视频根据url获取第一帧图片,获取任一帧图片

    #import <AVFoundation/AVFoundation.h> + (UIImage*) thumbnailImageForVideo:(NSURL *)videoURL at ...

  5. 单机ZooKeeper配置

    1.创建zoo.cfg copy D:\zookeeper3.4.6\conf\zoo_sample.cfg zoo.cfg 修改追加如下内容 dataDir=D:/zookeeper3.4.6/da ...

  6. swift 定义枚举和结构体 及使用

    //定义枚举 enum MapDirection { case North case South case East case West func simpleDescription() -> ...

  7. vue 之 vue-router

    官方文档 // 0. 如果使用模块化机制编程,导入Vue和VueRouter,要调用 Vue.use(VueRouter) // 1. 定义(路由)组件. // 可以从其他文件 import 进来 c ...

  8. [转]MongoDB c++驱动安装与使用

    安装 获取源码:git clone https://github.com/mongodb/mongo-cxx-driver.git,解压 安装编译工具scons:yum install -y scon ...

  9. 使用HttpURLConnection请求multipart/form-data类型的form提交

    写一个小程序,模拟Http POST请求来从网站中获取数据.使用Jsoup(http://jsoup.org/)来解析HTML. Jsoup封装了HttpConnection的功能,可以向服务器提交请 ...

  10. Unity Obstacle分析

    NavMeshObstacle Normal 通过设置半径和高度来设定障碍物,配合NavMesh使用. 优点: 简单易用,效率高 动态生成 缺点: 可能会被主角穿过,但目前没发现 形状固定为圆柱 Na ...