LIXE_115 @ 2023-06-27 15:43:37
#include<bits/stdc++.h>
using namespace std;
char aa;
int a[101][101];
int n,m;
int f[4][2]={1,0,-1,0,0,1,0,-1};
struct s{
int x;
int y;
}no,ne;
bool bfs(){
no.x=0;
no.y=0;
queue<s>q;
q.push(no);
while(q.empty()==false){
//cout<<"-----"<<endl;
no=q.front();
q.pop();
int xx,yy;
for(int i=0;i<4;i++){
xx=no.x+f[i][0];
yy=no.y+f[i][1];
if(xx>=0&&xx<n&&yy>=0&&yy<m&&a[xx][yy]!=1){
ne.x=xx;
ne.y=yy;
if(xx==n-1&&yy==m-1){
return true;
}
q.push(ne);
}
}
}
return false;
}
int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>aa;
if(aa=='.'){
a[i][j]==0;
}
if(aa=='#'){
a[i][j]==1;
}
}
}
bool ans=bfs();
if(ans==true){
cout<<"Yes";
}
else{
cout<<"No";
}
return 0;
}
样例过了,全MLE,发生甚吗事儿了??
by wangzhanchen @ 2023-06-27 15:45:08
我也跟你一样 你是不是也去看了那个“0分求助”
by wangzhanchen @ 2023-06-27 15:45:57
#include<bits/stdc++.h>
using namespace std;
int n,m,f;
int c[105][105];
char a[105][105];
int dx[5]={0,0,0,-1,1};
int dy[5]={0,-1,1,0,0};
void dfs(int x,int y)
{
if(x==n&&y==m)
{
cout<<"Yes\n";
f=1;
return;
}
for(int i=1;i<=4;i++)
{
int vy,vx;
vy=y+dy[i];
vx=x+dx[i];
if(vx>=1&&vx<=n&&vy>=1&&vy<=m&&a[vx][vy]!='#'&&c[vx][vy]==0)
{
c[vx][vy]=1;
dfs(vx,vy);
c[vx][vy]=0;
}
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>a[i][j];
dfs(1,1);
if(f==0)cout<<"No\n";
return 0;
}
by LIXE_115 @ 2023-06-27 16:28:42
@wangzhanchen 是的(悲,感谢
by coldy_rainy @ 2023-06-27 17:01:23
@LIXE_115
AC代码
#include<bits/stdc++.h>
using namespace std;
char aa;
int a[101][101];
int n,m;
int f[4][2]={1,0,-1,0,0,1,0,-1};
struct s{
int x;
int y;
}no,ne;
bool bfs(){
no.x=0;
no.y=0;
queue<s>q;
q.push(no);
while(q.empty()==false){
no=q.front();
q.pop();
int xx,yy;
for(int i=0;i<4;i++){
xx=no.x+f[i][0];
yy=no.y+f[i][1];
if(xx>=0&&xx<n&&yy>=0&&yy<m&&a[xx][yy]!=1){
ne.x=xx;
ne.y=yy;
if(xx==n-1&&yy==m-1){
return true;
}
a[xx][yy]=1; //标记当前节点已访问
q.push(ne);
}
}
}
return false;
}
int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>aa;
if(aa=='.'){
a[i][j]=0;//用赋值符号
}
if(aa=='#'){
a[i][j]=1;//同
}
}
}
bool ans=bfs();
if(ans){
cout<<"Yes";
}
else{
cout<<"No";
}
return 0;
}
by coldy_rainy @ 2023-06-27 17:02:20
@LIXE_115
赋值符号是“=”而不是“==”
要标记当前节点已访问来保证不会重复经过
by LIXE_115 @ 2023-06-29 21:08:53
@coldy_rainy 才看到,万分感谢!
by coldy_rainy @ 2023-06-30 14:21:37
@LIXE_115
不谢