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 ...
随机推荐
- linux proc目录介绍
https://www.cnblogs.com/DswCnblog/p/5780389.html
- R语言概述
R是一个有着统计分析功能及强大作图功能的软件系统,是由Ross Ihaka和Robert Gentleman共同创立.它是属于GNU系统的一个自由.免费.源码开放的软件,同一时候也是一个用于统计计算和 ...
- 架构设计:系统存储(28)——分布式文件系统Ceph(挂载)
(接上文<架构设计:系统存储(27)--分布式文件系统Ceph(安装)>) 3. 连接到Ceph系统 3-1. 连接客户端 完毕Ceph文件系统的创建过程后.就能够让客户端连接过去. Ce ...
- webstorm批量查找,批量替换快捷键
ctrl+shift+f:批量查找,我的webstorm11不能用ctrl+shift+f进行批量查找了,不知道什么原因,自己又胡乱实验了一下, 发现ctrl+shift+g+f可以批量查找 ctrl ...
- Splash args 属性
args属性可以获取加载时配置的参数,一般我们只传入URL,如下,args.url 就相当于加载时配置的URL参数,我们把它赋值给 url 变量然后返回:
- 项目适配iOS9遇到的一些问题及解决办法(更新两个小问题)
本文转载至 http://www.bubuko.com/infodetail-1110714.html http://www.jianshu.com/p/631bd7f12a38 1.网络请求报错.升 ...
- C# mvc 500 内部服务器访问异常
20161018 项目发布到IIS上后,无法访问,由于页面默认跳转到异常处理去了,所以详细信息一直查看不了. 在找寻无果,异常信息日志记录无效的情况下,只好一点点来测试了 在异常处理前,就已经试过,a ...
- Esper学习之九:EPL语法(五)
本篇的内容主要包括了Subquery(也就是子查询)和Join,内容不少,但是不难,基本上和sql差不太多. 1.Subquery EPL里的Subquery和sql的类似,是否比sql的用法更多我不 ...
- android include标签的使用,在RelativeLayout中使用include标签需注意!!!!!
转:http://4265337.blog.163.com/blog/static/195375820127935731114/ include和merge标记的作用主要是为了解决layout的重用问 ...
- Android 全屏Activity以透明的对话框形式弹出
1. styles.xml <style name="transcutestyle" parent="@android:style/Theme.DeviceDefa ...