目录
173.矩阵距离
题意:
- 173.矩阵距离
- 题意:
- 思路:
- code:
- 前置
- 队列
- 性质
- 总结
求每个数(包括1)到最近的1的距离(曼哈顿距离 就是x+y)
我看完之后的想法:
这好像就是正难则反,因为如果是你0入队的话你会判断许多没必要的数,如果1入队的话,那么每次都是有必要的了
如果只有1个起点,直接跑bfs 建立虚拟源点(em),可以转换成一个最短路问题, 不过我们做这题是不需要建立虚拟源点的 把所有是1的位置初始化为0,然后把所有1加入到队列中
code:#include
using namespace std;
const int N = 1e3+10;
int dist[N][N];
int st[N][N];
char g[N][N];
const int INF = 0x3f3f3f3f;
struct node
{
int x,y;
};
int n,m;
int dx[] ={1,-1,0,0};
int dy[] ={0,0,-1,1};
void bfs()
{
memset(dist,0x3f,sizeof dist);
queue q;
for(int i=0;i>m;
for(int i= 0 ;i>g[i];
bfs();
for(int i=0;i
关注
打赏