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

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

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

  2. POJ.1426 Find The Multiple (BFS)

    POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...

  3. POJ 1330 Nearest Common Ancestors(Tree)

    题目:Nearest Common Ancestors 根据输入建立树,然后求2个结点的最近共同祖先. 注意几点: (1)记录每个结点的父亲,比较层级时要用: (2)记录层级: (3)记录每个结点的孩 ...

  4. POJ 1330 Nearest Common Ancestors(lca)

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

  5. poj 1426 Find The Multiple( bfs )

    题目:http://poj.org/problem?id=1426 题意:输入一个数,输出这个数的整数 倍,且只有0和1组成 程序里写错了一个数,结果一直MLE.…… #include <ios ...

  6. [NewTrain 10][poj 2329]Nearest Number - 2

    题面: http://poj.org/problem?id=2329 题解: 这题有很多做法 1. 搜索 复杂度$O(n^4)$ 但是实际上远远达不到这个复杂度 所以可以通过 2. 对于每一个格子,我 ...

  7. 【POJ - 3669】Meteor Shower(bfs)

    -->Meteor Shower Descriptions: Bessie听说有场史无前例的流星雨即将来临:有谶言:陨星将落,徒留灰烬.为保生机,她誓将找寻安全之所(永避星坠之地).目前她正在平 ...

  8. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  9. 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 ...

随机推荐

  1. nginx配置http协议和tcp协议配置文件案例

    注意 nginx 1.9版本之后才支持 tcp #user nobody;worker_processes 1; #error_log logs/error.log;#error_log logs/e ...

  2. android5.1移植记录

    应用能够配置Android系统的各种设置,这些设置的默认值都是由frameworks中的SettingsProvider从数据库中读取的frameworks/base/packages/Setting ...

  3. MyBatis 支持的扩展点(version:3.2.7)

    从 [MyBatis 原码解析(version:3.2.7)] 中,我们得知,MyBatis去执行SQL都是通过 DefaultSqlSession 中的工具方法去执行的. 那么问题来了,MyBati ...

  4. Dubbo -- 系统学习 笔记 -- 示例 -- 服务分组

    Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 服务分组 当一个接口有多种实现时,可以用group区分. <dubbo:se ...

  5. web -- 前端访问后台跨区问题解决

    package com.xindatai.ibs.web.filter; import java.io.IOException; import javax.servlet.Filter; import ...

  6. 8 -- 深入使用Spring -- 2...2 指定Bean的作用域

    8.2.2 指定Bean的作用域 当使用XML 配置方式来配置Bean实例时,可以通过scope来指定Bean实例的作用域,没有指定scope属性的Bean实例作用域默认是singleton. 当采用 ...

  7. ios开发之-- tableview/collectionview获取当前点击的cell

    方法如下: 一般collectionView 或者 tableview都有自带的点击函数,如下: , collectionView -(void)collectionView:(UICollectio ...

  8. iOS开发--画一条黑色的横线

    在网上搜索了下大概有下面几种方法: 1.使用Quartz2D画出横线 需要一个UIVIew把这两个Label装起来,你需要计算好他们的位置同时给黑线预留像素的位置.这样你在UIView里面- (voi ...

  9. vsftpd下错误之:500 OOPS

    vsftpd下错误之:500 OOPS.vsftpd 是在Linux发行版中最推崇的一种FTP服务器程序,vsftpd的特点:小巧轻快.安全易用等. Linux也是为人们所常用的操作系统之一.这里主要 ...

  10. mybatis 之引入多个model

    配置hessian: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configurati ...