POJ-2329 Nearest number - 2(BFS)
Nearest number - 2
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 4100 Accepted: 1275
Description
Input is the matrix A of N by N non-negative integers.
A distance between two elements Aij and Apq is defined as |i − p| + |j − q|.
Your program must replace each zero element in the matrix with the nearest non-zero one. If there are two or more nearest non-zeroes, the zero must be left in place.
Constraints
1 ≤ N ≤ 200, 0 ≤ Ai ≤ 1000000
Input
Input contains the number N followed by N2 integers, representing the matrix row-by-row.
Output
Output must contain N2 integers, representing the modified matrix row-by-row.
Sample Input
3
0 0 0
1 0 2
0 3 0
Sample Output
1 0 2
1 0 2
0 3 0
有DP的方法,效率是O(n^2),但是我想不出,也没看到有博客是用DP,所以就暴力了。
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <queue>
using namespace std;
int a[205][205];
int b[205][205];
int vis[205][205];
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int n;
int c[405];
int d[405];
struct Node
{
int x,y;
};
void bfs(int x,int y)
{
Node Q[40005];
int rear=0,front=0;
int flag=0,level=9999999;
//Queue.push(Node(x,y));
Node term;
term.x=x;
term.y=y;
Q[rear++]=term;
vis[x][y]=1;
while(rear!=front&&flag!=-1)
{
// Node temp=Queue.front();
//Queue.pop();
Node temp=Q[front++];
if(level<=vis[temp.x][temp.y])
break;
for(int i=0;i<4;i++)
{
int xx=temp.x+dir[i][0];
int yy=temp.y+dir[i][1];
if(xx<0||xx>n-1||yy<0||yy>n-1||vis[xx][yy])
continue;
vis[xx][yy]=vis[temp.x][temp.y]+1;
if(a[xx][yy]!=0)
{
if(!flag)
{
flag=a[xx][yy];
level=vis[xx][yy];
}
else
{
flag=-1;
break;
}
}
//Queue.push(Node(xx,yy));
else
{
Node item;
item.x=xx;
item.y=yy;
Q[rear++]=item;
}
}
}
if(flag>0)
b[x][y]=flag;
}
void init()
{
memset(vis,0,sizeof(vis));
memset(c,0,sizeof(c));
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{scanf("%d",&a[i][j]);b[i][j]=a[i][j];}
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
if(a[i][j]==0)
{
init();
bfs(i,j);
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(j!=n-1)
printf("%d ",b[i][j]);
else
printf("%d",b[i][j]);
}
printf("\n");
}
}
}
POJ-2329 Nearest number - 2(BFS)的更多相关文章
- 【POJ】2329 Nearest number - 2(搜索)
题目 传送门:QWQ 分析 在dp分类里做的,然而并不会$ O(n^3) $ 的$ dp $,怒写一发搜索. 看起来是$ O(n^4) $,但仔细分析了一下好像还挺靠谱的? poj挂了,没在poj交, ...
- POJ.1426 Find The Multiple (BFS)
POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...
- POJ 1330 Nearest Common Ancestors(Tree)
题目:Nearest Common Ancestors 根据输入建立树,然后求2个结点的最近共同祖先. 注意几点: (1)记录每个结点的父亲,比较层级时要用: (2)记录层级: (3)记录每个结点的孩 ...
- 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 1426 Find The Multiple( bfs )
题目:http://poj.org/problem?id=1426 题意:输入一个数,输出这个数的整数 倍,且只有0和1组成 程序里写错了一个数,结果一直MLE.…… #include <ios ...
- [NewTrain 10][poj 2329]Nearest Number - 2
题面: http://poj.org/problem?id=2329 题解: 这题有很多做法 1. 搜索 复杂度$O(n^4)$ 但是实际上远远达不到这个复杂度 所以可以通过 2. 对于每一个格子,我 ...
- 【POJ - 3669】Meteor Shower(bfs)
-->Meteor Shower Descriptions: Bessie听说有场史无前例的流星雨即将来临:有谶言:陨星将落,徒留灰烬.为保生机,她誓将找寻安全之所(永避星坠之地).目前她正在平 ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- poj 3278 Catch That Cow (bfs)
题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 #include<s ...
随机推荐
- 安装程序配置服务器失败。参考服务器错误日志和C:\windows\sqlstp.log 了解更多信息
重装sql经常遇到2个问题 1,以前的某个程序安装已在安装计算机上创建挂起的文件操作.运行安装程序之前必须重新启动计算机. 删除C:\Program Files\Microsoft SQL Serve ...
- [原] unity3d动态加载脚本
本文记录如何通过unity3d进行脚本资源打包加载 1.创建TestDll.cs文件 public class TestDll : MonoBehaviour { void Start () { ...
- okHttp3自用封装
okHttp都已经出到3.2.0了,现在才开始要用到它,感觉自己好low~~ 根据平时自己的习惯,还是自己做一下封装,让代码撸起来更加顺畅一点! okhttp-3.2.0和okio-1.7.0就不多说 ...
- LHC大神问的矩阵转置问题
数学中线性代数中提到的矩阵转置,其实在我们的业务场景中也有需要的地方,比如LHC大神问到的这个问题 那么如何进行行列转换呢? 代码如下: <?php $array=array( '部门1'=&g ...
- ios开发之--时间格式化
字符串转时间 iOS 中 NSDate 是时间管理类,这里获取到的时间是 UTC 时间,可以根据需要转换成任意地区的时间. NSFormatter 类是时间格式化类,可以根据指定的格式.地区将指定的时 ...
- 通过RF数据库查询中文字段结果正常显示的转换方法
方法1:统一显示为中文 1.通过RF数据库查询中文字段结果格式:'\xba\xcb\xbc\xf5\xcd\xa8\xb9\xfd' 2.通过Decode Bytes To String进行gbk解码 ...
- Python迭代器笔记
python中的三大器有迭代器,生成器,装饰器,本文重点讲解下迭代器的概念,使用,自定义迭代器等的介绍. 1.概念: 迭代器是一个对象,一个可以记住遍历位置的对象,迭代器对象从集合的第一个元素开始访问 ...
- mysql check约束无效
转自http://blog.csdn.net/maxint64/article/details/8643288 今天在mysql中尝试使用check约束时,才知道在MySQL中CHECK约束是无效的, ...
- 【python3】Mac下selenium3+chrome驱动+python3
环境: python3.6.4 seleinum3.11 事先准备好python3 环境.安装谷歌浏览器 1 安装seleinum pip3 install selenium 2 安装chrome ...
- css笔记 - 张鑫旭css课程笔记之 vertical-align 篇
支持负值的属性: margin letter-spacing word-spacing vertical-align 元素vertical-align垂直对齐的位置与前后元素都没有关系元素vertic ...