郑yz @ 2018-05-25 13:31:02
`1.
cpp
using namespace std; int m,n,m1,m2,be1,be2,en1,en2,total,a[35][35]; bool b[35][35],c[35][35];
void dfs(int x,int y,int step){ if(x==en1 && y==en2){ total=min(total,step); return ; } if(step>total)return ; if(x<1 || y<1 || x>m || y>n)return ; if(b[x][y]==false || c[x][y]==true)return ; c[x][y]=true; dfs(x+m1,y+m2,step+1); dfs(x+m1,y-m2,step+1); dfs(x-m1,y+m2,step+1); dfs(x-m1,y-m2,step+1); dfs(x+m2,y+m1,step+1); dfs(x+m2,y-m1,step+1); dfs(x-m2,y+m1,step+1); dfs(x-m2,y-m1,step+1); c[x][y]=false; }
int main(){ total=20; memset(b,true,sizeof(b)); cin>>m>>n>>m1>>m2; for(int i=1;i<=m;i++) for(int j=1;j<=n;j++){ cin>>a[i][j]; if(a[i][j]==0 || a[i][j]==2)b[i][j]=false; if(a[i][j]==3){be1=i;be2=j;} if(a[i][j]==4){en1=i;en2=j;} } dfs(be1,be2,0); cout<<total<<endl; return 0; }
by 郑yz @ 2018-05-25 13:31:19
#include <bits/stdc++.h>
using namespace std;
int m,n,m1,m2,be1,be2,en1,en2,total,a[35][35];
bool b[35][35],c[35][35];
void dfs(int x,int y,int step){
if(x==en1 && y==en2){
total=min(total,step);
return ;
}
if(step>total)return ;
if(x<1 || y<1 || x>m || y>n)return ;
if(b[x][y]==false || c[x][y]==true)return ;
c[x][y]=true;
dfs(x+m1,y+m2,step+1);
dfs(x+m1,y-m2,step+1);
dfs(x-m1,y+m2,step+1);
dfs(x-m1,y-m2,step+1);
dfs(x+m2,y+m1,step+1);
dfs(x+m2,y-m1,step+1);
dfs(x-m2,y+m1,step+1);
dfs(x-m2,y-m1,step+1);
c[x][y]=false;
}
int main(){
total=20;
memset(b,true,sizeof(b));
cin>>m>>n>>m1>>m2;
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++){
cin>>a[i][j];
if(a[i][j]==0 || a[i][j]==2)b[i][j]=false;
if(a[i][j]==3){be1=i;be2=j;}
if(a[i][j]==4){en1=i;en2=j;}
}
dfs(be1,be2,0);
cout<<total<<endl;
return 0;
}
by 郑yz @ 2018-05-25 13:31:34
没办法了,只能抄题解
by UKE自动稽 @ 2018-05-25 13:59:55
祝您早日
by 起名真的很难 @ 2018-05-25 14:17:21
且慢!还是有一些卡常的方法的
1:i++变++i
2:cincout变scanfprintf
3:XXX==0(false)变!XXX
4:数据多就读入输出优化!
by Sirius_X @ 2018-10-13 09:37:03
c[x][y]不用回溯
by xiaoyang111 @ 2023-09-01 20:39:57
考咕