[NewTrain 10][poj 2329]Nearest Number - 2
题面:
http://poj.org/problem?id=2329
题解:
这题有很多做法
1. 搜索 复杂度$O(n^4)$ 但是实际上远远达不到这个复杂度 所以可以通过
2. 对于每一个格子,我们枚举每一行,找到每一行离他最近的格子
当前格子向右移动时,每一行离他最近的格子不可能向左移动,所以复杂度$O(n^3)$
3. dp 一共四个方向 左上到右下 左下到右上 右上到左下 右下到左上
然后分别dp 找到这个方向离他最近的格子 以左上到右下为例 $f[i][j]$可由$f[i-1][j],f[i][j-1],f[i-1][j-1]$转移得到
复杂度$O(n^2)$
Code:
1. 搜索
#include<cmath>
#include<math.h>
#include<ctype.h>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<complex>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cwchar>
#include<cwctype>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std; int n;
int a[][];
const int dx[]={,,-,},dy[]={,,,-},cx[]={-,,,-},cy[]={-,-,,}; int bfs(int x,int y,int k){
if(a[x][y]) return a[x][y];
if(k>=*n-) return ;
int cnt=;
int ok=;
for(int d=;d<;d++){
int nwx=x+k*dx[d],nwy=y+k*dy[d];
for(int i=;i<=k;i++){
if(nwx>= && nwx<=n && nwy>= && nwy<=n && a[nwx][nwy]){
if(ok==){
ok=a[nwx][nwy];
}
else{
cnt=;
}
}
nwx=nwx+cx[d];
nwy=nwy+cy[d];
}
}
if(cnt==) return ;
else if(ok) return ok;
else return bfs(x,y,k+);
} int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&a[i][j]);
for(int i=;i<=n;i++){
for(int j=;j<=n;j++)
printf("%d ",bfs(i,j,));
puts("");
}
return ;
}
2.
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<iostream>
#include<queue>
#include<string>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<long long,long long> pll;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define rep(i,j,k) for(register int i=(int)(j);i<=(int)(k);i++)
#define rrep(i,j,k) for(register int i=(int)(j);i>=(int)(k);i--) ll read(){
ll x=,f=;char c=getchar();
while(c<'' || c>''){if(c=='-')f=-;c=getchar();}
while(c>='' && c<=''){x=x*+c-'';c=getchar();}
return x*f;
} const int maxn=;
int n;
int a[maxn][maxn];
int col[maxn];
int b[maxn][maxn],num[maxn]; int main(){
#ifdef LZT
freopen("in","r",stdin);
#endif
n=read();
rep(i,,n)
rep(j,,n){
a[i][j]=read();
if(a[i][j]){
num[i]++;
b[i][num[i]]=j;
}
}
rep(i,,n){
rep(j,,n) col[j]=;
rep(j,,n){
if(a[i][j]==){
int mndis=1e9,mn=-;
rep(k,,n){
int nw=1e9,xx=-;
if(col[k]<=num[k] && nw>abs(b[k][col[k]]-j)+abs(k-i)){
nw=abs(b[k][col[k]]-j)+abs(k-i);
xx=a[k][b[k][col[k]]];
}
if(col[k]<num[k]){
int nwdis=abs(b[k][col[k]+]-j)+abs(k-i);
if(nw>nwdis){
nw=nwdis;
xx=a[k][b[k][col[k]+]];
}
else if(nw==nwdis){
xx=-;
}
}
if(mndis>nw){
mndis=nw;mn=xx;
}
else if(mndis==nw){
mn=-;
}
//cout<<i<<' '<<j<<' '<<k<<' '<<col[k]<<' '<<xx<<endl;
}
if(mn!=-) a[i][j]=mn;
}
rep(k,,n){
if(col[k]<num[k] && abs(b[k][col[k]]-(j+))>abs(b[k][col[k]+]-(j+))) col[k]++;
}
}
} rep(i,,n){
rep(j,,n)
printf("%d ",a[i][j]);
puts("");
}
return ;
}
3.
没写过
不过应该很好写
Review:
水题
数据也很水
比如第一个代码 在所有格子均为0的时候复杂度是严格$O(n^4)$的 但是竟然跑得比第二个代码快300ms。。。
每种做法都挺好想
[NewTrain 10][poj 2329]Nearest Number - 2的更多相关文章
- [POJ 2329] Nearest number-2
Link: POJ 2329 传送门 Solution: 比较明显的$dp$,但爆搜好像也能过 用多个方向$dp$来解决此题,最后汇总答案即可 一开始我写了4个,但后来发现只要相反的2个方向即可,同时 ...
- 【POJ】2329 Nearest number - 2(搜索)
题目 传送门:QWQ 分析 在dp分类里做的,然而并不会$ O(n^3) $ 的$ dp $,怒写一发搜索. 看起来是$ O(n^4) $,但仔细分析了一下好像还挺靠谱的? poj挂了,没在poj交, ...
- 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 (暴力+搜索bfs)
Nearest number - 2 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3943 Accepted: 1210 De ...
- 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节 ...
随机推荐
- linux 输入子系统(2) platform device
Input platform device 一般是在板级bsp注册了的资源. 以gpio-keys为例: #####################gpio_key.h############ ...
- LeetCode(28)题解:Implement strStr()
https://leetcode.com/problems/implement-strstr/ 题目: Implement strStr(). Returns the index of the fir ...
- VS 预先生成事件命令
宏 说明 $(ConfigurationName) 当前项目配置的名称(例如,“Debug|Any CPU”). $(OutDir) 输出文件目录的路径,相对于项目目录.这解析为“输出目录”属性的值. ...
- nlp_tool
http://www.afenxi.com/post/9700 11款开放中文分词引擎大比拼 附录评测数据地址http://bosonnlp.com/dev/resource 各家分词系统链接地址Bo ...
- iOS UI控件之间的关系图
- redis06----消息订阅
使用办法: 订阅端: Subscribe 频道名称 发布端: publish 频道名称 发布内容 r1:>publish news "aaaaaa" "" ...
- download file by python in google colab
https://stackoverflow.com/questions/15352668/download-and-decompress-gzipped-file-in-memory You need ...
- I.MX6 Android 5.1.1 下载、编译
/************************************************************************* * I.MX6 Android 5.1.1 下载. ...
- Java常用数据结构和算法
二叉树: 1.每个结点不能多于两个子树: 2.一颗平衡二叉树的深度要比及结点个数N小得多. 二叉查找树: 1.结点x的所有左子树的值小于x,所有右子树的值大于x: AVL树: 1.一种带有平衡条件的二 ...
- Servlet3.0之九:web模块化
一.使用web-fragment.xml 在Servlet 3.0中,可以使用标注来设置Servlet的相关信息.实际上,Web容器并不仅读取/WEB-INF/classes中的Servlet标注信息 ...