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

【题意】n*n的矩阵,对于不是0的地方仍为原值,是0的地方,找出离他最近的不是零的地方,如果有离他一样近的两个及以上地方就仍为0,就一个的话,为那一个的值。

【思路】巧用int dx[]= {1,1,-1,-1},cx[]= {-1,0,1,0};int dy[]= {1,-1,-1,1},cy[]= {0,1,0,-1};

还有判定条件if(k>n) return 0;

参考资料:http://blog.csdn.net/code_or_code/article/details/26274451

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=;
int mp[N][N];
int n;
int dx[]= {,,-,-},cx[]= {-,,,};
int dy[]= {,-,-,},cy[]= {,,,-};
bool go(int x,int y)
{
if(x<||x>n||y<||y>n) return false;
else return true;
}
int bfs(int x,int y,int k)
{
if(k>n) return ;
if(n==||mp[x][y]) return mp[x][y];
int cnt=,flag=;
int xx,yy;
int tmpx,tmpy;
for(int i=; i<; i++)
{
xx=x+k*cx[i];
yy=y+k*cy[i];
for(int j=; j<k; j++)
{
if(mp[xx][yy]&&go(xx,yy))
{
if(cnt==)
{
flag=;
break;
}
cnt++;
tmpx=xx,tmpy=yy;
}
xx+=dx[i];
yy+=dy[i]; }
if(flag) break;
}
if(cnt==) return bfs(x,y,k+);
else if(flag) return ;
else return mp[tmpx][tmpy];
}
int main()
{
scanf("%d",&n);
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
scanf("%d",&mp[i][j]);
}
}
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
printf("%d ",bfs(i,j,));
}
printf("\n");
}
return ;
}

Nearest number - 2_暴力&&bfs的更多相关文章

  1. POJ-2329 Nearest number - 2(BFS)

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

  2. hdu 1195:Open the Lock(暴力BFS广搜)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. hdu 4771 13 杭州 现场 B - Stealing Harry Potter's Precious 暴力bfs 难度:0

    Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. W ...

  4. bzoj 1295: [SCOI2009]最长距离 暴力+bfs最短路

    题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1295 题解: 对每个点暴力跑一遍bfs,看能够到达的最远位置,这里如果有障碍物则距离为1 ...

  5. Easy Number Challenge(暴力,求因子个数)

    Easy Number Challenge Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I ...

  6. UVa 10603 Fill (暴力BFS+优先队列)

    题意:给定4个数,a,b,c,d,分别代表空杯子容积为a,b,一个盛满水的杯子容积为c,让你不断倒水,找一个dd,是不是存在某个时刻, 某个杯子里的水dd,和d相同,或者无限接近.让求最少的倒水量和d ...

  7. hdu 4770 13 杭州 现场 A - Lights Against Dudely 暴力 bfs 状态压缩DP 难度:1

    Description Harry: "But Hagrid. How am I going to pay for all of this? I haven't any money.&quo ...

  8. 八数码问题(一) 暴力BFS + STL

    八数码问题是一个经典的人工智能问题.具体问题不累述了. 思路:由于存在多组测试数据,可以考虑“打表法“.所谓打表法,即枚举所有的初始情况,记录其到达终点的路径.而在这个题目中,顺序打表会调用很多次BF ...

  9. CF995E Number Clicker (双向BFS)

    题目链接(洛谷) 题目大意 给定两个数 \(u\) , \(v\) .有三种操作: \(u=u+1(mod\) \(p)\) . \(u=u+p−1(mod\) \(p)\) . \(u=u^{p−2 ...

随机推荐

  1. 用Visual C#向access添加数据

    (1)创建并打开一个OleDbConnection对象. (2)创建一个插入一条记录的SQL语句. (3)创建一个OleDbCommand对象. (4)通过此OleDbCommand对象完成对插入一条 ...

  2. Jquery元素追加和删除

    原文链接:http://www.cnblogs.com/william-lin/archive/2012/08/12/2635402.html 介绍    DOM是Document Object Mo ...

  3. 模拟器的tableView的分割线不显示

    只有iOS9和iPhone6 plus模拟器上TableView分割线不会显示. 原因: 由于iPhone6 plus的分辨率较高,开发的时候同常都使用command + 3 或者 command + ...

  4. Farseer.Net

    Farseer.Net V0.2 ORM开源框架 目录 http://www.cnblogs.com/steden/archive/2013/01/22/2871160.html V1.0教程:htt ...

  5. 3.1 关系数据库标准语言SQL综述

    一.SQL语言的特点 SQL结构查询语言 1.综合统一: 2.高度非过程化:不需要指定存储路径 3.面向集合的操作方式 4.以同一种语法提供两种使用方式:独立语言.嵌入式语言 5.语言简单,易学易用 ...

  6. win7_64bit下桌面及开始菜单中图标变为.lnk

    以下内容参考整理与MSDN: 1.首先 win+r 2.打开运行程序 3.输入: regedit 4.找到: 计算机\HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\WIND ...

  7. js构建工具和预编译

    Gulp应该和Grunt比较,他们的区别我就不说了,说说用处吧.Gulp / Grunt 是一种工具,能够优化前端工作流程.比如自动刷新页面.combo.压缩css.js.编译less等等.简单来说, ...

  8. priority_queue C++

    三种优先队列定义方法:T_T 内部原理以后补..... priority_queue<int> qi;//普通的优先级队列,按从大到小排序 struct Node { friend boo ...

  9. Div层的展开与收缩的代码

    <html> <head> <title>div展开收缩代码</title> <style> * { margin:0; padding:0 ...

  10. POJ 1573 Robot Motion 模拟 难度:0

    #define ONLINE_JUDGE #include<cstdio> #include <cstring> #include <algorithm> usin ...