bfs,10pts,蒟蒻求帮调
  • 板块P1605 迷宫
  • 楼主docxjun
  • 当前回复15
  • 已保存回复15
  • 发布时间2022/6/24 19:10
  • 上次更新2023/10/27 22:40:30
查看原帖
bfs,10pts,蒟蒻求帮调
580135
docxjun楼主2022/6/24 19:10

rt 样例过了

#include<bits/stdc++.h>
using namespace std;
struct cp{
	int x, y;
};
int walk[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
int a[20][20];
bool vis[20][20];
queue<cp> q;
int main()
{
	memset(a, -1 ,sizeof(a));
	memset(vis, false , sizeof(vis));
	int n,m,t;
	cin>>n>>m>>t;
	int sx,sy,fx,fy;
	cin>>sx>>sy>>fx>>fy;
	for(int i = 1; i <= t; i++)
	{
		int tmp1,tmp2;
		cin>>tmp1>>tmp2;
		vis[tmp1][tmp2] = true;
	}
	cp tmp = {sx,sy};
	vis[sx][sy] = true;
	q.push(tmp);
	while(!q.empty())
	{
		cp top = q.front();
		int tx = top.x;
		int ty = top.y;
		q.pop();
		for(int i = 0 ; i < 4; i++)
		{
			int nx = tx+walk[i][0];
			int ny = ty+walk[i][1];
			if(vis[nx][ny]||nx<1||nx>n||ny<1||ny>m)
			{
				continue;
			}
			int d = a[tx][ty];
			a[nx][ny] = d+1;
			cp in = {nx,ny};
			q.push(in);
			vis[nx][ny] = true; 
		}
	}
	cout<<a[fx][fy]<<endl;
	return 0;
}
2022/6/24 19:10
加载中...