广搜真的会超时。。。
给定起点和终点,规定只能向右走,问走到终点的方案数 注意:不一定要是最优路径!
因为不需要是最优路径,所以广搜的优势没了,变成了慢得要死的深搜。。。 所以要用记搜或者 dp d p
时间复杂度:
就是这么慢
#include<cstdio> #include<queue> using namespace std;int ans,n,m; struct node{int x,y;}a,b; queue<node>q; const short dx[4]={-2,-1,1,2}; const short dy[4]={1,2,2,1}; int abs(int x){return x<0?-x:x;} bool check(node x) { if(x.y>b.y) return false;//优化1 if(x.y==b.y&&abs(x.x-b.x)&1==0) return false;//优化2 if(x.x<1||x.y<1||x.x>n||x.y>m) return false; return true; } void bfs(node s,node t) { q.push(s); while(q.size()) { node x=q.front();q.pop(); for(int i=0;i<4;i++) { node y=(node){x.x+dx[i],x.y+dy[i]}; if(check(y)) { q.push(y); if(y.x==t.x&&y.y==t.y) {ans++;break;}//累计答案 } } } } int main() { scanf("%d%d%d%d%d%d",&n,&m,&a.y,&a.x,&b.y,&b.x); bfs(a,b); printf("%d",ans);//输出 }