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 ...
随机推荐
- Java使用泛型实现栈结构
泛型是Java SE5.0的重要特性,使用泛型编程可以使代码获得最大的重用.由于在使用泛型时要指明泛型的具体类型,这样就避免了类型转换.本实例将使用泛型来实现一个栈结构,并对其进行测试. 思路分析:既 ...
- Java使用String类格式化当前日期
在输出日期信息时,经常需要输出不同格式的日期格式,本实例中介绍了String字符串类中的日期格式化方法,实例使用不同的方式输出String类的日期格式参数值,组合这些值可以实现特殊格式的日期字符串. ...
- Go之简单并发
func Calculate(id int) { fmt.Println(id) } 使用go来实现并发 func main() { for i := 0; i < 100; i++ { go ...
- .NET Framework 4.0源代码
原文出处:http://blogs.microsoft.co.il/blogs/arik/archive/2010/07/12/step-into-net-framework-4-0-source-c ...
- WPF导航总结
使用导航的目的是从一个页面进入到另一个页面.无论是预先决定的线性顺序(向导)还是基于层次的用户驱动程序(大部分网站的形式),或者动态生成的路径,主要有3种方法实现:调用Navigate方法,使用Hyp ...
- 找不同diff-打补丁patch
Q:为什么要找不同,为什么要打补丁? A: 在Linux应用中,作为DBA,我们知道MySQL跑在Linux系统之上,数据库最重要的追求就是性能,“稳”是重中之重,所以不能动不动就是换系统或是换这换那 ...
- 采用get方式提交数据到服务器实例
GetDemo项目目录 一.编写StreamTools.java /** * */ package com.hyzhou.getdemo.utiils; import java.io.ByteArra ...
- SpringBoot application.properties (application.yml)优先级从高到低
SpringBoot application.properties(application.yml) 优先级从高到低 SpringBoot配置文件优先级从高到低 =================== ...
- [Vim] Vim 常用基本操作
1. 导航 1.1. 查看行号 :set number 显示行号 :set number! 隐藏行号 :.= 在底部显示当前行号 := 在底部显示总行号 1.2. 移动光标 0 或 ^ ...
- linux实现开机自启动脚本
Linux下(以RedHat为范本)添加开机自启动脚本有两种方法,先来简单的; 一.在/etc/rc.local中添加如果不想将脚本粘来粘去,或创建链接什么的,则:step1. 先修改好脚本,使其所有 ...