尴尬

矩阵距离

https://ac.nowcoder.com/acm/contest/1017/B

开始我是直接用的bfs模板进行的,没有过,好像是内存过线了....
以下是没过的代码:
#include <iostream>
#include<queue>
#include<string>
using namespace std;
string a[1001];
int dir[]={0,1,0,-1,0};
struct node
{
int x,y;
int step;
};
int main()
{
int n,m;
char ch;
cin>>n>>m;
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[i][j]=='1')
{
cout<<"0"<<" ";
continue;
}
int flag=1;
queue<node> q;
node start; start.x=i,start.y=j,start.step=0;
q.push(start);
while(!q.empty()&&flag)
{
node temp=q.front();
q.pop();
int xx,yy;
for(int k=0;k<4;k++)
{
node obj=temp;
xx=obj.x+dir[k]; yy=obj.y+dir[k+1];
if(xx>=0&&xx<n&&yy>=0&&yy<m)
{
if(a[xx][yy]=='1')
{
cout<<temp.step+1<<" ";
flag=0;
break;
}
else
{
obj.x=xx; obj.y=yy; obj.step=temp.step+1;
q.push(obj);
}
}
}</node></string></queue></iostream>

        }

    }
    cout<<endl;
}
return 0;

}

全部评论

相关推荐

06-23 17:45
门头沟学院 Java
里面的项目啥的真的有用吗?&nbsp;这些人是割韭菜吗?
HellowordX:很简单,如果你有自己稳定的学习路线和获取知识的方式就没必要,如果你啥都不懂的小白或者里边有你感兴趣的知识,我觉得挺值,我也经常为知识付费,因为时间精力有限,很多东西我不可能自己重复造轮子
点赞 评论 收藏
分享
牛客刘北:如果暑期实习是27届的话,你要晚一年才会毕业,企业为什么会等你呢?要搞清时间逻辑呀!27届现在实习只能是在暑假实习,这是日常实习,不是暑期实习。所以多去投日常实习吧,暑期实习肯定不会要你的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务