输出为0,求调
查看原帖
输出为0,求调
928783
zjs123bc_楼主2023/10/4 19:44
#include <algorithm>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <math.h>
#include <string>
#include <vector>
#include <cmath>
#include <deque>
#include <queue>//q.front 访问第一个
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef double dl;
typedef long long ll;
typedef long double ld;//小根堆:priority_queue<int,vector<int>,greater<int>> a;
typedef unsigned long long ull;//大根堆:priority_queue<int> a;
string s[1005];
bool book[1005][1005];
int n,fx[4][2]={0,1,1,0,0,-1,-1,0},x1,x2,y2,qy,ans;
struct node{
	int x;
	int y;
	int d;
};
queue<node> q;
void bfs()
{
	q.push({x1,qy,0});
	book[x1][qy]=1;
	while(!q.empty())
	{
		node t=q.front();
		q.pop() ;
		for(int i = 0;i <4;i++)
		{
			int xx=t.x +fx[i][0];
			int yy=t.y +fx[i][1]; 
            if (xx<0||yy<0||xx>=n||yy>=n) continue;
	        //cerr << xx << " " <<yy <<" " << s[xx][yy] << " " <<book[xx][yy]<<" " << i <<endl; 
            //if(xx==2&&yy==2) cerr << t.d+1 << endl;
			if(xx>=0&&yy>=0&&xx<n&&yy<n&&s[xx][yy]=='0'&&book[xx][yy]==0)
			{
				if(xx==x2&&yy==y2)
				{
					ans = t.d+1;
					return ;
				}
				book[xx][yy]=1;
				q.push({xx,yy,t.d+1});
			}
		}
	}
}
int main()
{
 	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >>n;
	for(int i = 1;i<=n;i++) cin >> s[i];
	cin >> x1 >> qy >> x2 >> y2; 
	x1--;
	qy--;
	x2--;
	y2--;
	bfs();
	cout << ans;
	return 0;
}
2023/10/4 19:44
加载中...