Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

WTF,交了7、8次啦!超级简单的BFS,尼玛就是要剪枝,利用哈希剪枝,还有如果不用循环队列的话,将数组要开到200000

Posted by xijunlee93 at 2012-12-03 22:31:43 on Problem 3278
附源代码
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct node{
     int time,l;
}Q;
Q queue[200000];
int hash[100001];
int main()
{
       int n,k,tou=0,wei=0,location;
       cin>>n>>k;
       queue[wei].time=0;queue[wei].l=n;
       wei++;hash[n]=1;
       while(tou<wei&&queue[tou].l!=k)
       {
           location=queue[tou].l;
           if (location-1>=0&&location-1<=100000&&hash[location-1]==0)
           {
             queue[wei].time=queue[tou].time+1;
             queue[wei].l=location-1;
             hash[location-1]=1;
             wei++;
           }
           if (location+1>=0&&location+1<=100000&&hash[location+1]==0)
           {
             queue[wei].time=queue[tou].time+1;
             queue[wei].l=location+1;
             hash[location+1]=1;
             wei++;
           }
           if (location*2>=0&&location*2<=100000&&hash[location*2]==0)
           {
             queue[wei].time=queue[tou].time+1;
             queue[wei].l=location*2;
             hash[location*2]=1;
             wei++;
           } 
           tou++;
       }
       cout<<queue[tou].time<<endl;
       system("pause");
       return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator