如果你也DFS TLE 9个点,删一行代码解决
查看原帖
如果你也DFS TLE 9个点,删一行代码解决
926432
Chengjintian楼主2023/4/9 22:22

我的原TLE代码:

#include <bits/stdc++.h>
using namespace std;
int n,m,temp,tx,ty;
int dx[12]={1,0,0,-1};
int dy[12]={0,1,-1,0};
bool ans;
char t[1145][1145];
bool used[114][114];
bool cheak(int x,int y){
	if(x>n or x<1 or y>m or y<1)return true;
	return false;
}
void mg(){
	if (tx==n and ty==m){
		ans=true;
		return ;
	}
	if(cheak(tx,ty))return ;
	
	for(int i=0;i<=3;i++){
		tx+=dx[i];
		ty+=dy[i];
		if(!used[tx][ty] and t[tx][ty]=='.'){
			used[tx][ty]=true;
			mg();
			used[tx][ty]=false;
		}
		tx-=dx[i];
		ty-=dy[i];
	}
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	for(int j=1;j<=m;j++)
		cin>>t[i][j];
	tx=1,ty=1;
	mg();
	if(ans)cout<<"Yes";
	else cout<<"No";
	return 0;
}

只要把回溯,也就是 “used[tx][ty]=false;”删了就A了

我也不到为什么

2023/4/9 22:22
加载中...