Nearest number - 2_暴力&&bfs
Description
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
Output
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的更多相关文章
- POJ-2329 Nearest number - 2(BFS)
Nearest number - 2 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4100 Accepted: 1275 De ...
- hdu 1195:Open the Lock(暴力BFS广搜)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 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 ...
- bzoj 1295: [SCOI2009]最长距离 暴力+bfs最短路
题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1295 题解: 对每个点暴力跑一遍bfs,看能够到达的最远位置,这里如果有障碍物则距离为1 ...
- Easy Number Challenge(暴力,求因子个数)
Easy Number Challenge Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I ...
- UVa 10603 Fill (暴力BFS+优先队列)
题意:给定4个数,a,b,c,d,分别代表空杯子容积为a,b,一个盛满水的杯子容积为c,让你不断倒水,找一个dd,是不是存在某个时刻, 某个杯子里的水dd,和d相同,或者无限接近.让求最少的倒水量和d ...
- 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 ...
- 八数码问题(一) 暴力BFS + STL
八数码问题是一个经典的人工智能问题.具体问题不累述了. 思路:由于存在多组测试数据,可以考虑“打表法“.所谓打表法,即枚举所有的初始情况,记录其到达终点的路径.而在这个题目中,顺序打表会调用很多次BF ...
- CF995E Number Clicker (双向BFS)
题目链接(洛谷) 题目大意 给定两个数 \(u\) , \(v\) .有三种操作: \(u=u+1(mod\) \(p)\) . \(u=u+p−1(mod\) \(p)\) . \(u=u^{p−2 ...
随机推荐
- Maximal Rectangle [LeetCode]
Problem Description: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle co ...
- C#中DateTime应用
编写一个控制台程序,输入一个日期,求下一天的日期. 要求如下:在控制台输入一个日期(分别输入年.月.日),判断输入的日期是否有效,如果有效,计算该日期的下一天日期,并显示:否则,输出"无效的 ...
- 超实用的JavaScript代码段 --倒计时效果
现今团购网.电商网.门户网等,常使用时间记录重要的时刻,如时间显示.倒计时差.限时抢购等,本文分析不同倒计时效果的计算思路及方法,掌握日期对象Date,获取时间的方法,计算时差的方法,实现不同的倒时计 ...
- 解决li在ie,firefox中行高不一致问题
转载 http://www.cnblogs.com/jikey/archive/2011/11/13/2247543.html li在ie与firefox的高度是不一样的,解决办法是li font-s ...
- (转)SqlServer将数据库中的表复制到另一个数据库
本文为转载地址为:http://jingyan.baidu.com/article/d5c4b52bc5c102da570dc547.html 复制表结构 在使用SqlServer的过程中,我们可能需 ...
- HDU 1864
最大报销额 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- JavaScript中的test()方法
定义和用法 test() 方法用于检测一个字符串是否匹配某个模式. 语法 RegExpObject.test(string) 参数 描述 string 必需.要检测的字符串. 返回值 如果字符串 st ...
- C# 检测网络链接
;//Local system uses a modem to connect to the Internet. ; //Local system uses a local area network ...
- No suitable driver found for jdbc:mysql://localhost/dbname
把mysql-connector-java的jar包放入jdk/jre/lib/ext文件下
- SuperGridControl 使用小技巧
1.显示行号 superGridControl1.PrimaryGrid.ShowRowGridIndex = true; 2.允许调整行头的宽度 superGridControl1.PrimaryG ...