题目:
1.Bouncy Ball
解法就是模拟。。。另外就是注意怎么停止dfs!详见代码。
//>>>Qiansui
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<deque>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x,y,sizeof(x))
//#define int long long
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*f;
}
using namespace std;
const int maxm=5e5+5,inf=0x3f3f3f3f,mod=998244353;
int n,m,i[2],j[2],ans;
int fx[4]={1,1,-1,-1},fy[4]={1,-1,1,-1};
bool flag=false;
string d;
map<int,map<string,int>> a;//利用这个对搜索进行标记,去重
void dfs(int x,int y,string ss){
int id;
if(ss=="DR") id=0;
else if(ss=="DL") id=1;
else if(ss=="UR") id=2;
else if(ss=="UL") id=3;
int xx=x,yy=y;
if(a[m*(xx-1)+yy][ss]){
return ;
}else a[m*(xx-1)+yy][ss]=1;
if(xx==i[1]&&yy==j[1]){
flag=true;
return ;
}
xx=xx+fx[id];
yy=yy+fy[id];
while(xx>=1&&xx<=n&&yy>=1&&yy<=m){
if(xx==i[1]&&yy==j[1]){
flag=true;
return ;
}
if(xx==x&&yy==y){
return ;
}
if(a[m*(xx-1)+yy][ss]){
return ;
}else a[m*(xx-1)+yy][ss]=1;
xx=xx+fx[id];
yy=yy+fy[id];
}
string s;
if(xx==0) s="D";
else if(xx==n+1) s="U";
else s=ss[0];
if(yy==0) s+="R";
else if(yy==m+1) s+="L";
else s+=ss[1];
// cout<<" s: "<<s<<" "<<xx<<" yy: "<<yy<<"\n";
// cout<<xx-fx[id]<<" xx yy: "<<yy-fy[id]<<"\n";
++ans;
dfs(xx-fx[id],yy-fy[id],s);
return ;
}
void solve(){
cin>>n>>m>>i[0]>>j[0]>>i[1]>>j[1]>>d;
ans=0;
flag=false;
a.clear();
dfs(i[0],j[0],d);
if(flag) cout<<ans<<'\n';
else cout<<-1<<'\n';
return ;
}
signed main(){
// ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int _=1;
cin>>_;
while(_--){
solve();
}
return 0;
}
标签:ch,ss,DFS,yy,xx,include,id
From: https://www.cnblogs.com/Qiansui/p/17238018.html