【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节 ...
随机推荐
- Kotlin都转正成Android官方语言了,你还不试一下?
想想Android Studio 和Eclipse ,我觉得你还是有必要入手Kotlin了. 站好队很重要. 以前的一篇总体概括老文,大家可以看看~ 爽翻天!告别Java.一起来使用kotlin开发完 ...
- HDU - 5628:Clarke and math (组合数&线性筛||迪利克雷卷积)
题意:略. 思路:网上是用卷积或者做的,不太会. 因为上一题莫比乌斯有个类似的部分,所以想到了每个素因子单独考虑. 我们用C(x^p)表示p次减少分布在K次减少里的方案数,由隔板法可知,C(x^p)= ...
- js 值和引用
js对值和引用的赋值/传递在语法上没有区别,完全根据值得类型决定 简单值(即标量基本类型值),总是通过值复制的方式来赋值/传递,包括null,undefined,字符串,数字,布尔值和ES6中的sym ...
- Filter学习(二)Filter使用Decorator设计模式
在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以使用Decorator(装饰器)模式对request.response对象进行包装,再把包装对象传给目 ...
- Redis缓存的设计、性能、应用与数据集群同步
Redis缓存的设计.性能.应用与数据集群同步 http://youzhixueyuan.com/design-performance-and-application-of-redis-cache.h ...
- JavaScript for 循环累加 json 字符串
var msg = {"status":1,"data":[{"id":"12","words":& ...
- VS2013下的64位与32位程序配置
VS2013下的64位与32位程序配置 在Windows 7 64bit和Visual Studio 2013下生成64位程序. 新建一个Visual Studio Win32 Console项目 ...
- arangodb 安装简单试用
备注: 测试环境使用的是docker 1. 安装 docker run -p 8529:8529 -e ARANGO_ROOT_PASSWORD=openSesame arangodb/aran ...
- DZ X3 和 ECshop 通过uc_server实现会员同步整合教程.
会员数据整合是实现商城和论坛系统共享会员数据.论坛系统注册会员整合后可直接在商城登陆.现以本人社区为例.本人社区采用DZ最新的X3.1 商城采用的ECshop最近的V2.7.3 现在想把两个模板整合在 ...
- RESTful的一个样例
后台代码: @RequestMapping(value = { "queues" }) @ResponseBody public List<ResourcePool> ...