题目

传送门: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. 3. 什么是JSR参考实现? - JavaEE基础系列

    本文是JavaEE基础系列的第三节. Java EE简介 - JavaEE基础系列 JSR简介 - JavaEE基础系列 上一节中, 我们介绍了什么是JSR.JSR就是一个提交到JCP的抽象请求,包含 ...

  2. 利用sklearn的LabelEncoder对标签进行数字化编码

    from sklearn.preprocessing import LabelEncoder def gen_label_encoder(): labels = ['BB', 'CC'] le = L ...

  3. python函数作用域

    python中函数作用域 在python中,一个函数就是一个作用域 name = 'xiaoyafei' def change_name(): name = '肖亚飞' print('在change_ ...

  4. ScrollView中嵌套ExpandableListView

    为了让SrollView中嵌套ExpandableListView,我们就得改ListView的高度,所以写一个类继承于ExpandableListview: import android.conte ...

  5. mysql在innodb索引下b+树的高度问题。

    B+树索引介绍 B+树索引的本质是B+树在数据库中的实现.但是B+树索引有一个特点是高扇出性,因此在数据库中,B+树的高度一般在2到3层.也就是说查找某一键值的记录,最多只需要2到3次IO开销.按磁盘 ...

  6. Jenkins构建持续集成

    Jenkins 是一个开源软件项目,旨在提供一个开放易用的软件平台,使软件的持续集成变得可能.现在软件开发追求的是效率以及质量,Jenkins使得自动化成为可能! 亮点 采用shell自定义脚本,控制 ...

  7. Oracle中用exp/imp命令快速导入导出数据

    from: http://blog.csdn.net/wangchunyu11155/article/details/53635602 [用 exp 数 据 导 出]: 1 将数据库TEST完全导出, ...

  8. LeetCode 424. Longest Repeating Character Replacement

    原题链接在这里:https://leetcode.com/problems/longest-repeating-character-replacement/description/ 题目: Given ...

  9. 在线编辑器KindEditor的使用

    1.官网下载:点击进入 2.解压后目录说明 ├── asp asp示例 ├── asp.net asp.net示例 ├── attached 空文件夹,放置关联文件attached ├── examp ...

  10. BZOJ4543 [POI2014]Hotel加强版

    题意 有一个树形结构,每条边的长度相同,任意两个节点可以相互到达.选3个点.两两距离相等.有多少种方案? 数据范围:n<=100000 分析 参照小蒟蒻yyb的博客. 我们先考虑一个\(O(n^ ...