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. hdu---(1054)Strategic Game(最小覆盖边)

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. 让ie678支持css一些属性及html标签

    昨天写的一个页面,用的css3及html5的一些样式与标签,在ie8下看是没有效果的,然后就在晚上查找了一下如何能让ie8也能实现这些效果. 1.添加respond.js文件,Respond.js让I ...

  3. 20145236 《Java程序设计》第7周学习总结

    20145236 <Java程序设计>第7周学习总结 教材学习内容总结 第十三章 时间与日期 认识时间与日期 时间的度量 格林威治标准时间GMT 格林威治标准时间的正午是太阳抵达天空最高点 ...

  4. 转载python2进制打包相关

    Python模块——struct(字节流,组包拆包实现) http://www.linuxidc.com/Linux/2014-02/97158.htm [日期:2014-02-24] 来源:Linu ...

  5. AES加密 16进制与二进制转换

    import java.security.Key; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax ...

  6. EF中的那些批量操作

    在使用EF的过程中,我们经常会遇到需要批量操作数据的场景,批量操作有的时候不仅能提高性能,比如使用SqlBulkCopy进入批量插入的时候,而且比较方便操作,提高效率.那么这篇文章就来总结EF中的那些 ...

  7. DML数据操作语言练习

    --创建表T_HQ_BM2 --create table t_hq_bm2 as select * from t_hq_bm; commit; --添加行内容 --insert into t_hq_b ...

  8. 带你揭开ATM的神秘面纱

    相信大家都用过ATM取过money吧,但是有多少人真正是了解ATM的呢?相信除了ATM从业者外了解的人寥寥无几吧,鄙人作为一个从事ATM软件开发的伪专业人士就站在我的角度为大家揭开ATM的神秘面纱吧. ...

  9. IBatis.Net 表连接查询(五)

    IBatis.Net之多表查询: 一.定制实际对应类的方式 首先配置多表的测试数据库,IBatis.Net之Oracle表连接查询配置: 首先新建两张表如下: 为两张表建立外键: ALTER TABL ...

  10. IT公司100题-9-判断整数序列是不是二元查找树的后序遍历结果

    问题描述: 输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果. 如果是返回true,否则返回false. 例如输入4, 8, 6, 12, 16, 14, 10,由于这一整数序列是如下树 ...