关于 zju 1301

jwd_1_cool 2003-07-16 03:15:38
朋友告诉我说用bfs+hash

但我研究了半天 ,这bfs 还是搞不出来!
不知道如何,请高手具体点谈一下。不甚感激
...全文
26 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
loewe 2003-07-23
mark
回复
tankbattle 2003-07-18
//我的原码

#include<iostream>
#include<queue>
#include<stack>
using namespace std;

#define FROMFILE
#ifdef FROMFILE
#include<fstream>
#define cin infile
ifstream infile("test.txt");
#endif

struct STATE
{
int LastLightState;
int LastPersonPos;
}State[1024][10];

int RoomCount;
int RoomConnected[10][10];
int LightControl[10][10];

int Input()
{
cin>>RoomCount;
if(RoomCount==0)
return 0;
for(int i=0;i<RoomCount;i++)
for(int j=0;j<RoomCount;j++)
RoomConnected[i][j]=LightControl[i][j]=0;
for(int i=0;i<(1<<RoomCount);i++)
for(int j=0;j<RoomCount;j++)
State[i][j].LastLightState=
State[i][j].LastPersonPos=-1;
int DoorCount;
int ControlCount;
cin>>DoorCount>>ControlCount;
for(int i=0;i<DoorCount;i++)
{
int a,b;
cin>>a>>b;
a--,b--;
RoomConnected[a][b]=RoomConnected[b][a]=1;
}
for(int i=0;i<ControlCount;i++)
{
int a,b;
cin>>a>>b;
a--,b--;
LightControl[a][b]=1;
}
return 1;
}

void Calculate()
{
State[1][0].LastLightState=1;
State[1][0].LastPersonPos=0;
queue<int> StateQueue;
StateQueue.push(1);
StateQueue.push(0);
while(!StateQueue.empty())
{
int CurrentLightState=StateQueue.front();
StateQueue.pop();
int CurrentPersonPos=StateQueue.front();
StateQueue.pop();
if(CurrentLightState==(1<<(RoomCount-1))&¤tPersonPos==RoomCount-1)
break;
for(int i=0;i<RoomCount;i++)//Go to the next room
if(RoomConnected[CurrentPersonPos][i]&&
(CurrentLightState&(1<<i)))
{
if(State[CurrentLightState][i].LastLightState<0)
{
State[CurrentLightState][i].LastLightState=CurrentLightState;
State[CurrentLightState][i].LastPersonPos=CurrentPersonPos;
StateQueue.push(CurrentLightState);
StateQueue.push(i);
}
}
for(int i=0;i<RoomCount;i++)
if(LightControl[CurrentPersonPos][i]&&i!=CurrentPersonPos)
{
if((CurrentLightState&(1<<i))==0)//If the light is off
{
if(State[CurrentLightState|(1<<i)][CurrentPersonPos].LastLightState<0)
{
State[CurrentLightState|(1<<i)][CurrentPersonPos].LastLightState=CurrentLightState;
State[CurrentLightState|(1<<i)][CurrentPersonPos].LastPersonPos=CurrentPersonPos;
StateQueue.push(CurrentLightState|(1<<i));
StateQueue.push(CurrentPersonPos);
}
}
else//If the light is on
{
if(State[CurrentLightState&~(1<<i)][CurrentPersonPos].LastLightState<0)
{
State[CurrentLightState&~(1<<i)][CurrentPersonPos].LastLightState=CurrentLightState;
State[CurrentLightState&~(1<<i)][CurrentPersonPos].LastPersonPos=CurrentPersonPos;
StateQueue.push(CurrentLightState&~(1<<i));
StateQueue.push(CurrentPersonPos);
}
}
}
}
}

void Output()
{
if(State[1<<(RoomCount-1)][RoomCount-1].LastLightState<0)
cout<<"The problem cannot be solved."<<endl;
else
{
stack<int> TraceStack;
int Step=0;
int CurrentLightState=(1<<(RoomCount-1));
int CurrentPersonPos=RoomCount-1;
int LastLightState,LastPersonPos;
while(CurrentLightState!=1||CurrentPersonPos!=0)
{
TraceStack.push(CurrentLightState);
TraceStack.push(CurrentPersonPos);
Step++;
LastLightState=State[CurrentLightState][CurrentPersonPos].LastLightState;
LastPersonPos=State[CurrentLightState][CurrentPersonPos].LastPersonPos;
CurrentLightState=LastLightState;
CurrentPersonPos=LastPersonPos;
}
cout<<"The problem can be solved in "<<Step<<" steps:"<<endl;
LastLightState=1;
LastPersonPos=0;
while(!TraceStack.empty())
{
cout<<"- ";
CurrentPersonPos=TraceStack.top();
TraceStack.pop();
CurrentLightState=TraceStack.top();
TraceStack.pop();
if(LastLightState==CurrentLightState)//Walk from one room to another
{
cout<<"Move to room "<<CurrentPersonPos+1<<"."<<endl;
}
else//Turn on/of a light
{
for(int i=0;i<RoomCount;i++)
{
if((LastLightState&(1<<i))==0&&
(CurrentLightState&(1<<i)))
{
cout<<"Switch on light in room "<<i+1<<"."<<endl;
break;
}
if((LastLightState&(1<<i))&&
(CurrentLightState&(1<<i))==0)
{
cout<<"Switch off light in room "<<i+1<<"."<<endl;
break;
}
}
}
LastLightState=CurrentLightState;
LastPersonPos=CurrentPersonPos;
}
}
}

int main()
{
int Test=0;
while(Input())
{
Calculate();
Test++;
cout<<"Villa #"<<Test<<endl;
Output();
cout<<endl;
}
}
回复
mmmcd 2003-07-17
200行内能解决吗?
回复
tankbattle 2003-07-16
用State[1024][10] 保存状态,1024种状态是每个房间(假设是10个)的灯的状态,编成2进制数,10表示人在哪个房间。共10240种状态。初始时人在1号间,灯全亮,结束时人在r号间,灯全灭。用bfs找出从起始到结束的通路就行。
回复
相关推荐
发帖
数据结构与算法
创建于2007-08-27

3.2w+

社区成员

数据结构与算法相关内容讨论专区
申请成为版主
帖子事件
创建了帖子
2003-07-16 03:15
社区公告
暂无公告