编了两个下午了,还是不行…
查看原帖
编了两个下午了,还是不行…
564427
SiXinchen楼主2021/10/5 21:55

这是最开始的:

#include<bits/stdc++.h>
using namespace std;
int b[2],c[2],ans=0;
int gh(int x,int y){
  if(x==b[0]&&y==b[1]){
    ans++;
    return 0;
  }
  else if(c[0]-2==x&&c[1]-1==y)return 0;
  else if(c[0]-1==x&&c[1]-2==y)return 0;
  else if(c[0]-2==x&&c[1]+1==y)return 0;
  else if(c[0]-1==x&&c[1]+2==y)return 0;
  else if(c[0]+2==x&&c[1]-1==y)return 0;
  else if(c[0]+1==x&&c[1]-2==y)return 0;
  else if(c[0]+2==x&&c[1]+1==y)return 0;
  else if(c[0]+1==x&&c[1]+2==y)return 0;
  gh(x+1,y);
  gh(x,y+1);
}
int main(){
  cin>>b[0]>>b[1]>>c[0]>>c[1];
  gh(0,0);
  cout<<ans;
  return 0;
}

结果全是MLE。 改进之后是:

#include<bits/stdc++.h>
using namespace std;
int b[2],c[2],ans[23][23]={0};
bool a[23][23]={0},d[23][23]={0};
int gh(int x,int y){
  if(d[x][y])return ans[x][y];
  if(a[x][y]==1||x<2||y<2){
    d[x][y]=1;
    return 0;
  }
  if(x==2&&y==2){
    return 1;
  }
  ans[x][y]=gh(x-1,y)+gh(x,y-1);
  return ans[x][y];
}
int main(){
  cin>>b[0]>>b[1]>>c[0]>>c[1];
  b[0]+=2;
  b[1]+=2;
  c[0]+=2;
  c[1]+=2;
  a[c[0]][c[1]]=1;
  a[c[0]-2][c[1]-1]=1;
  a[c[0]-1][c[1]-1]=1;
  a[c[0]-2][c[1]+1]=1;
  a[c[0]-1][c[1]+2]=1;
  a[c[0]+2][c[1]-1]=1;
  a[c[0]+1][c[1]-2]=1;
  a[c[0]+2][c[1]+1]=1;
  a[c[0]+1][c[1]+2]=1;
  cout<<gh(b[0],b[1]);
  return 0;
}

结果只有一个点AC,还有一个WA,3个TLE。 最后又换了一个解法:

#include<bits/stdc++.h>
using namespace std;
long long b[2],c[2],ans[25][25]={0};
bool a[25][25]={0};
int main(){
  ans[2][2]=1;
  cin>>b[0]>>b[1]>>c[0]>>c[1];
  b[0]+=2;
  b[1]+=2;
  c[0]+=2;
  c[1]+=2;
  a[c[0]][c[1]]=1;
  a[c[0]-2][c[1]-1]=1;
  a[c[0]-1][c[1]-1]=1;
  a[c[0]-2][c[1]+1]=1;
  a[c[0]-1][c[1]+2]=1;
  a[c[0]+2][c[1]-1]=1;
  a[c[0]+1][c[1]-2]=1;
  a[c[0]+2][c[1]+1]=1;
  a[c[0]+1][c[1]+2]=1;
  for(int i=2;i<=b[0];i++)
    for(int j=2;j<=b[1];j++)
      if(!a[i][j])
        ans[i][j]=ans[i-1][j]+ans[i][j-1];
  cout<<ans[b[0]][b[1]];
  return 0;
}

然后就爆零了...(输出似乎一直是0) 哪位大佬来看看啊,谢谢

2021/10/5 21:55
加载中...