题面:

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的更多相关文章

  1. [POJ 2329] Nearest number-2

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

  2. 【POJ】2329 Nearest number - 2(搜索)

    题目 传送门:QWQ 分析 在dp分类里做的,然而并不会$ O(n^3) $ 的$ dp $,怒写一发搜索. 看起来是$ O(n^4) $,但仔细分析了一下好像还挺靠谱的? poj挂了,没在poj交, ...

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

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

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

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

  5. LCA POJ 1330 Nearest Common Ancestors

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

  6. POJ 1330 Nearest Common Ancestors(lca)

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

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

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

  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. mysql 查询正在执行的事务以及等待锁 常用的sql语句

    使用navicat测试学习: 首先使用set autocommit = 0;(取消自动提交,则当执行语句commit或者rollback执行提交事务或者回滚)   在打开一个执行update查询 正在 ...

  2. SenTestingKit.framework的报错!

    本文转载至http://www.cocoachina.com/ask/questions/show/106912 ld: building for iOS Simulator, but linking ...

  3. ORACLE数据库忘记SYS和SYSTEM密码,SYSTEM被锁定怎么办?

    本人忘性太大,竟然将ORACLE的Sys用户和system用户密码搞忘,而且多次尝试登录system后,造成system被锁定. 经过一番尝试,终于解决.过程如下: 首先,重建sys密码文件.重建方式 ...

  4. xamarin.android Activity之间跳转与传值

    前言 由于需要,所以接触到这个新的安卓开发模式,我会把我的学习经历全都记录下来,希望对大家有用. 导读 关于Activity,学习过安卓的人也应该明白什么是Activity,推荐新手去看YZF的这篇文 ...

  5. linux下自动创建设备文件节点---class

    在驱动模块初始化函数中实现设备节点的自动创建 我们在刚开始写Linux设备驱动程序的时候,很多时候都是利用mknod命令手动创建设备节点,实际上Linux内核为我们提供了一组函数,可以用来在模块加载的 ...

  6. I2S

    音频数据传输而制定: Inter—IC Sound : 单线 时钟和数据一条线,时分复用: 标准的I2S总线电缆是由3根串行导线组成的:1根是时分多路复用(简称TDM)数据线:1根是字选择线:1根是时 ...

  7. hdu 1400 Mondriaan's Dream 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1400 题目意思:给出一个h * w的 大 矩形,需要用 1 * 2 的砖块去填充这个大矩形,问填充的方 ...

  8. html5--6-7 CSS选择器4

    html5--6-7 CSS选择器4 实例 学习要点 掌握常用的CSS选择器 了解不太常用的CSS选择器 什么是选择器 当我们定义一条样式时候,这条样式会作用于网页当中的某些元素,所谓选择器就是样式作 ...

  9. 【Selenium】Action.moveToElement

    使用moveToElement可是实现定位焦点,尝试后测试通过,代码如下       //鼠标单击前商品信息被隐藏,我们需要手动除展示商品标签的隐藏属性      JavascriptExecutor ...

  10. JS DOM1核心概要document

    Document类型: document对象表示整个html页面,而且,document对象是window对象的一个属性: 文档信息:document.title,表示当前页面的标题: documen ...