超时求助
  • 板块学术版
  • 楼主__钉钉__
  • 当前回复5
  • 已保存回复5
  • 发布时间2021/8/9 21:25
  • 上次更新2023/11/4 11:22:43
查看原帖
超时求助
83594
__钉钉__楼主2021/8/9 21:25

#pragma GCC optimize("Ofast","inline","-ffast-math")
#pragma GCC target("abm,avx,mmx,popcnt,sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include<iostream>
#include<cstdio>
#include<cmath>
#define re 
using namespace std;
int qread() {
	int x(0),y(1);char c=getchar();
	while(c<'0'||c>'9') {
		if(c=='-')y=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9') {
		x=(x<<1)+(x<<3)+(c^48);
		c=getchar();
	}
	return x*y;
}

bool b[1001][1001],f[1001][1001];
int s,N;
void dfs(int x,int y,int tx,int ty,int step) {
	if(x==tx&&y==ty) {
		s=min(step,s);
		return;
	}
	if(step>=s) return; 
	b[x][y]=1;
	if(x+1<=N&&f[x+1][y]==0&&b[x+1][y]==0)
		dfs(x+1,y,tx,ty,step+1);
	if(y+1<=N&&f[x][y+1]==0&&b[x][y+1]==0)
		dfs(x,y+1,tx,ty,step+1);
	if(x-1>=1&&f[x-1][y]==0&&b[x-1][y]==0)
		dfs(x-1,y,tx,ty,step+1);
	if(y-1>=1&&f[x][y-1]==0&&b[x][y-1]==0)
		dfs(x,y-1,tx,ty,step+1);
	b[x][y]=0;
}
int main() {
	int i,j;
	N=qread();
	char c;
	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= N; j++) {
			c = getchar();
			while (c != '0' && c != '1') c = getchar();
			f[i][j] = c - '0';
		}
	}
	int sx(qread()),sy(qread()),tx(qread()),ty(qread());
	s=999999;
	dfs(sx,sy,tx,ty,0);
	cout<<s;
}

请问哪里可以优化?谢谢

2021/8/9 21:25
加载中...