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. uva-----11292 The Dragon of Loowater

    Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance tur ...

  2. 创建交货单/外向交货BAPI_OUTB_DELIVERY_CREATE_SLS/STO

    FUNCTION Z_SD_CREATE_DN. *"-------------------------------------------------------------------- ...

  3. JFrame背景

    1.引言 在了解了JFrame面板的相关知识后,我们可以选择在RootPane根面板或LayeredPane面板中设置背景图案. 2.方法 对于大小固定的窗口背景设置如下: //导入图案 ImageI ...

  4. 《Learning Play! Framework 2》学习笔记——案例研究1(Templating System)

    注解: 这是对<Learning Play! Framework 2>第三章的学习 本章是一个显示聊天记录的项目,只有一个页面,可以自动对聊天记录进行排序.分组和显示,并整合使用了less ...

  5. Git工作流指南:集中式工作流

    转载:http://blog.jobbole.com/76847/ 本文由 伯乐在线 - 李鼎 翻译.未经许可,禁止转载!英文出处:atlassian.欢迎加入翻译组. 转到分布式版本控制系统看起来像 ...

  6. 抓包工具tshark使用备忘

         抓包命令行工具tshark可以用于自定制,相比GUI工具可以实现一些自动化,譬如把某些关注的数据抓起下来存放到文本中,然后再分析输出.      demo: std::string deco ...

  7. ASP.NET-【Excel】-将Excel中的数据批量加载到SQLserver数据库

    用到了一个SqlBulkCopy的类 核心代码分析 代码我还没有测试过 string excelConnectionString = string.Format("Provider=Micr ...

  8. 使用Matrix控制图像或组件变换的步骤

    1.获取Matrix对象,该Matrix对象既可新创建,也可直接获取其他对象内封装的Matrix(例如Transformation对象内部) 2.调用Matrix的方法进行平移.旋转.缩放.倾斜等. ...

  9. Js笔试题之parseInt()和.map()

    parseInt()的几个例子 var b = parseInt("01"); alert("b="+b); var c = parseInt("09 ...

  10. [Jquery]网页定位导航特效

    描述:左右联动的导航,非常适合展示页面内容多,区块划分又很明显的,点击右边固定导航项时,左边的内容跟着切换.滑动滚动条的时候,右边的导航也随着左边的展示而进行高亮切换. 思路:比较滚动距离和楼层距离( ...