_Vistion_ @ 2024-12-02 20:22:13
TLE #4,#6,#8,#10
调了好久了,求大佬帮忙看看
#include <bits/stdc++.h>
#define int long long
#define ll ((i)*2)
#define rr ((i)*2+1)
using namespace std;
const int M=1e2+10;
const int mod=1e9+7;
int n,m;
int nxt[5][3]={{1,0},{0,1},{-1,0},{0,-1}};
char Map[M][M];
int vis[M][M];
void dfs(int x,int y)
{
if(x<1||x>n||y<1||y>m) return;
if(x==n&&y==m){
cout<<"Yes";
exit(0);
}
for(int i=0; i<4; i++){
int nx=nxt[i][0]+x,ny=nxt[i][1]+y;
if(nx<1||nx>n||ny<1||ny>m||Map[nx][ny]=='#'||vis[nx][ny]) continue;
else{
vis[nx][ny]=1;
dfs(nx,ny);
vis[nx][ny]=0;
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
cin>>Map[i][j];
}
}
vis[1][1]=1;
dfs(1,1);
cout<<"No";
return 0;
}
by TODAYS @ 2024-12-02 20:34:14
#include <bits/stdc++.h>
#define int long long
#define ll ((i)*2)
#define rr ((i)*2+1)
using namespace std;
const int M=1e2+10;
const int mod=1e9+7;
int n,m;
int nxt[5][3]={{1,0},{0,1},{-1,0},{0,-1}};
char Map[M][M];
int vis[M][M];
void dfs(int x,int y)
{
if(x<1||x>n||y<1||y>m) return;
if(x==n&&y==m){
cout<<"Yes";
exit(0);
}
for(int i=0; i<4; i++){
int nx=nxt[i][0]+x,ny=nxt[i][1]+y;
if(nx<1||nx>n||ny<1||ny>m||Map[nx][ny]=='#'||vis[nx][ny]) continue;
else{
vis[nx][ny]=1;
dfs(nx,ny);
vis[nx][ny]=0;
//不用回溯
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
cin>>Map[i][j];
}
}
vis[1][1]=1;
dfs(1,1);
cout<<"No";
return 0;
}
by _Vistion_ @ 2024-12-02 20:56:38
@TODAYS
谢谢大佬,已关