CF877D 一道BFS求助
  • 板块题目总版
  • 楼主ZackYoung
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/11/4 15:43
  • 上次更新2024/11/4 17:30:19
查看原帖
CF877D 一道BFS求助
1434595
ZackYoung楼主2024/11/4 15:43

一个bfs的题 就是特别裸的那种bfs 题目地址:https://codeforces.com/problemset/problem/877/D 简单说就是输入一个地图 从起点走到终点的最短步数 不过和模板题不一样的是一次可以走1到k步 为什么我这个bfs这么写一直会wrong answer on test 31?卡在第三十一个样例挺久了 这个是我的代码:

```cpp
#include <bits/stdc++.h>
using namespace std;
struct node{
	int x,y,step;
}now;
int n,m,k,sx,sy,ex,ey,ans=-1;
char g[1010][1010];
bool vis[1010][1010];
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
void bfs()
{
	queue<node> q;
	now.x=sx,now.y=sy,now.step=0;
	q.push(now);
	vis[sx][sy]=true;
	while(!q.empty()){
		auto t=q.front();q.pop();
		if(t.x==ex&&t.y==ey){
			ans=t.step;
			return;
		}
		for(int i=0;i<4;i++){
			for(int j=1;j<=k;j++){
				int tx=t.x+dx[i]*j;
				int ty=t.y+dy[i]*j;
				if(tx<1||tx>n||ty<1||ty>m||g[tx][ty]=='#'||vis[tx][ty]) break;;
				vis[tx][ty]=true;
				now.x=tx,now.y=ty,now.step=t.step+1;
				q.push(now);
			}
		}
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin>>n>>m>>k;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>g[i][j];
		}
	}
	cin>>sx>>sy>>ex>>ey;
	bfs();
	cout<<ans;
	return 0;
}
2024/11/4 15:43
加载中...