| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
深度搜索 (附带代码)过不去呀 谁帮我看看#include<iostream>
using namespace std;
struct fountain
{
int p,q;
};
class Hall_of_Fountains
{
public:
bool input();
private:
bool DFS(int);
bool _right(int,int);
bool stay(int,int);
bool _left(int,int);
fountain num[101];
int n, //喷泉个数
count_num, //房间序号
count; //计数
};
bool Hall_of_Fountains::input()
{
scanf("%d",&n);
if(n==0)return false;
num[0].p=0;
num[0].q=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i].p);
}
for(int i1=1;i1<=n;i1++)
{
scanf("%d",&num[i1].q);
}
count_num=0;
count=0;
DFS(0);
return true;
}
bool Hall_of_Fountains::_right(int a,int k) //前进
{
int k1=num[a+1].q-1;
int k2=num[a+1].q+num[a+1].p;
if(num[a+1].p==0)return true;
int i=(k-num[a+1].q)/num[a+1].p;
if(i%2!=0)i=i+1;
if(k1+i*num[a+1].p<=k&&k<k2-1+i*num[a+1].p)
return false;
return true;
}
bool Hall_of_Fountains::stay(int a,int k) //原地
{
int k1=num[a].q-1;
int k2=num[a].q+num[a].p;
if(num[a].p==0)return true;
int i=(k-num[a].q)/num[a].p;
if(i%2!=0)i=i+1;
if(k1+i*num[a].p<=k&&k<k2-1+i*num[a].p)
return false;
return true;
}
bool Hall_of_Fountains::_left(int a,int k) //后退
{
if(a==0)return false;
int k1=num[a-1].q-1;
int k2=num[a-1].q+num[a-1].p;
if(num[a-1].p==0)return true;
int i=(k-num[a-1].q)/num[a-1].p;
if(i%2!=0)i=i+1;
if(k1+i*num[a-1].p<=k&&k<k2-1+i*num[a-1].p)
return false;
return true;
}
bool Hall_of_Fountains::DFS(int k) //深度搜索
{
count++;
if(count>=5000) //是不是这有问题,最大时间能多少呀
{
printf("0\n");
return true;
}
if(count_num==n)
{
printf("%d\n",k+1);
return true;
}
if(_right(count_num,k))
{
count_num++;
k=k+1;
if(DFS(k))return true;
k=k-1;
count_num--;
}
if(stay(count_num,k))
{
k=k+1;
if(DFS(k))return true;
k=k-1;
}
if(_left(count_num,k))
{
count_num--;
k=k+1;
if(DFS(k))return true;
k=k-1;
count_num++;
}
return false;
}
int main()
{
while(1)
{
Hall_of_Fountains bb;
if(!bb.input()) return 0;
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator