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 ...
随机推荐
- Objective-C(一简介)
Objective-C简介 通常写作ObjC和较少用的Objective C或Obj-C,是扩充C的面向对象编程语言.它主要使用于Mac OS X和GNUstep这两个使用OpenStep标准的系统, ...
- J2EE开发实战基础系列一 HelloWorld【转】
开始咱们的第一个程序,首先是配置环境,按照上一章所描述的方式下载开发工具,然后配置Java环境变量,给大家看下具体的结构: 环境变量配置OK的提示,如上图. Eclipse和Tomcat的文件目录 ...
- linux 安装 apache
1. 系统基本信息 CentOS 6.4 内存2G 硬盘 200G cpu 4核 (cat /proc/cpuinfo |grep 'processor'|wc -l 查看cpu核数 ...
- <context:annotation-config> 和 <context:component-scan>的差别
<context:annotation-config> is used to activate annotations in beans already registered in the ...
- Oracle 中的 decode
含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译值1)ELSIF 条件=值2 THEN R ...
- linux-网卡故障
Linux 网络问题解决思路 1.查看 /etc/sysconfig/network-script/ 查看eth0和eth1的配置是否正确 2.查看 /etc/modual.conf 的配置模块是否正 ...
- jquery之getJSON方法获取中文数据乱码解决方法
最近公司做的东西要用到js,感觉js太繁琐,所以自己学起了jquery,发现jquery确实强大.在学到jquery ajax的时候(用的工具是eclipse),发现$.getJSON()方法请求服务 ...
- php解压zip文件
<?php header("Content-type:text/html;charset=utf-8"); function get_zip_originalsize($fi ...
- struts中获取域
在struts的Action中,有三种方法可以得到request.session.servletContext域. 1.通过ServletActionContext类获取对象 HttpServletR ...
- 基于百度定位及天气获取的DEMO
demo基于百度定位APIv4.0版.新浪天气(不用查询城市代码). 需求: 1.button实现触发定位监听和天气捕获 2.两个textview 分别显示详细地址.天气. 界面很简陋,侧重功能实现. ...