社区讨论
多组数据使用BFS,一定要将队列定在函数内部
P1747好奇怪的游戏参与者 2已保存回复 1
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @lp22sxif
- 此快照首次捕获于
- 2023/11/17 11:44 2 年前
- 此快照最后确认于
- 2023/11/17 15:55 2 年前
CPP
void bfs(int x,int y)
{
queue<node> q;
node h,t;
t.x = x; t.y=y; t.step=0; vis[t.x][t.y]=1;
q.push(t);
while(!q.empty())
{
h=q.front();q.pop();
if(h.x==1&&h.y==1){cout<<h.step<<endl;return;}
for(int i=0;i<12;i++)
{
int tx = h.x+nex[i][0];
int ty = h.y+nex[i][1];
if(tx<1||tx>n||ty<1||ty>n||vis[tx][ty]==1){continue;}
else
{
vis[tx][ty]=1;
t.x = tx,t.y = ty;
t.step = h.step+1;
q.push(t);
}
}
}
}
回复
共 1 条回复,欢迎继续交流。
正在加载回复...