90分求助
查看原帖
90分求助
419144
chenjunxiu楼主2021/7/10 12:31

RT

#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int n,m,s[N][N],a[10]={1,2,4,8,16,32,64,128,256,512};
bool v[N][N];
int fx[4]={1,-1,0,0};
int fy[4]={0,0,1,-1};
char c[N][N];
struct asdf{
	int x,y,t;
}e,w;
queue<asdf>q;
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    	for(int j=1;j<=m;j++)
    	{
    		cin>>c[i][j];
    		s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+(c[i][j]=='X');
		}
	v[1][1]=1;
	if(c[1][1]=='X')
	{
		cout<<-1<<endl;
		return 0;
	}
	e.x=e.y=1;
	e.t=0;
	q.push(e);
	while(!q.empty())
	{
		e=q.front();
		q.pop();
		if(c[e.x][e.y]=='#')
		{
			cout<<e.t<<endl;
			return 0;
		}
		for(int i=0;i<4;i++)
		{
			for(int j=0;j<10;j++)
			{
				w=e;
				w.x=e.x+fx[i]*a[j],w.y=e.y+fy[i]*a[j];
				int x1=max(w.x,e.x-1),x2=min(w.x,e.x-1);
				int y1=max(w.y,e.y-1),y2=min(w.y,e.y-1);
				if(w.x>0&&w.x<=n&&w.y>0&&w.y<=m&&!(s[x1][y1]-s[x2][y1]-s[x1][y2]+s[x2][y2]))
				{
					if(!v[w.x][w.y])
					{
						v[w.x][w.y]=1;
						w.t++;
						q.push(w);
					}
				}
				else
					break;
			}
		}
	}
	cout<<-1<<endl;
	return 0;
}
2021/7/10 12:31
加载中...