Uva-321-brute Force enumeration-implicit graph Search

Source: Internet
Author: User

Test instructions: give you n room, there are many lights of the control switch, I room lamp switch in J room, the room without the lamp can not enter, I room and J room if there is no door, also can not enter from I to J, the beginning of the room is 1, and the lamp is open, asked if you can go to the last room N, And at this time the other room lights are closed. If there are multiple solutions, the sequence of operations with the smallest number of steps is output.

Range: n<=10,

Problem-Solving ideas: Set the status to, the current room number + the current other room lights status. So the total state is n*2^n, the maximum value is 10*1024, and the bare enumeration.

Note that each enumeration of this topic must be encoded from the room 1-n, otherwise WA, and, also, PE WA. Use a bit mark the status of the room light.

#include <iostream>#include<map>#include<memory.h>#include<stdio.h>#include<string>#include<queue>#include<vector>using namespacestd;Const intMAXN = One;classnode{ Public: Vector<string>step; intcur; intCurstepnum; intlights; Node () {//Step.reserve (65535);    }    BOOL operator< (Constnode& node)Const    {        returnCurstepnum >Node.curstepnum; };};intCONN[MAXN][MAXN];intCONTRO[MAXN][MAXN];intR, D, s;intstates[Ten][1025];p riority_queue<Node>Q;intperlights[Ten] = {    1<<0,1<<1,1<<2,1<<3,1<<4,1<<5 ,    1<<6,1<<7,1<<8,1<<9 };voidRead () {intSS, EE;  for(inti =0; I < d;i++) {cin>> SS >>ee; //start with 0CONN[SS-1][ee-1] =1; Conn[ee-1][SS-1] =1; }     for(inti =0; I < s;i++) {cin>> SS >>ee; Contro[ss-1][ee-1] =1;    }}node BFS () {node node; Node.cur=0; Node.curstepnum=0; Node.lights=1;    Q.push (node); //init node, only 0 rooms are lit.states[0][1] =1;  while(Q.empty () = =false) {node=Q.top ();        Q.pop (); intCurindex =node.cur; //cur Lights State        intCurlights =node.lights; intCurstepnum =Node.curstepnum; //Judging is not the end        if(Curindex = = R-1) && Curlights = = Perlights[r-1])        {            returnnode; }            //enumerating the status of Lights         for(inti =0; I <Ten; i++)        {            if(Contro[curindex][i] = =0)                Continue; if(i = =Curindex)Continue; //control of the lamp            intNextcontrolightindex =i; stringdesc =""; intNextlight =0; if((Curlights & perlights[nextcontrolightindex]) = =Perlights[nextcontrolightindex]) {                //turn off the lights in next roomNextlight = (curlights ^Perlights[nextcontrolightindex]); if(States[curindex][nextlight] = =1)                    //Repeat                    Continue; Desc="-Switch off light in room"+ std::to_string (Nextcontrolightindex +1); Desc+="."; }            Else            {                //turn on the lights in next roomNextlight = Curlights |Perlights[nextcontrolightindex]; if(States[curindex][nextlight] = =1)                    //Repeat                    Continue; Desc="-Switch on light in room"+ std::to_string (Nextcontrolightindex +1); Desc+=".";            } Node NewNode; Newnode.curstepnum= Curstepnum +1; Newnode.cur=Curindex; Newnode.lights=Nextlight; Newnode.step=Node.step;            NewNode.step.push_back (DESC); //push to queueQ.push (NewNode); States[curindex][nextlight]=1; }        //end Curindex enum Lights State//Start Door         for(inti =0; I <Ten; i++)        {            if(Curindex = =i)Continue; if(Conn[curindex][i] = =0)                Continue; intNextdoor =i; if((Perlights[nextdoor] & curlights) = =0)                //the lights are off and cannot be entered.                Continue; if(States[nextdoor][curlights] = =1)                //I've seen it, can't go in                Continue; //the light is on, it can go inNode NewNode; Newnode.cur=Nextdoor; Newnode.curstepnum= Curstepnum +1; Newnode.lights=curlights; Newnode.step=Node.step; stringdesc ="-Move to room"+ std::to_string (Nextdoor +1); Desc+=".";            NewNode.step.push_back (DESC); States[nextdoor][curlights]=1;        Q.push (NewNode); }} node.cur= -1; returnnode;}intMain () {intt =0;  while(CIN >> R >> D >>s) {if(r = = d && d = = S && r = =0)             Break; MEMSET (Conn,0,sizeof(conn)); memset (Contro,0,sizeof(conn)); memset (states,0,sizeof(states));  while(Q.empty () = =false) Q.pop ();                Read (); cout<<"Villa #"; cout<< T +1<<Endl; if(r = =1) {cout<<"The problem can solved in"<<0<<"steps:"<<Endl; }        Else{node node=BFS (); //cout << "test" << Endl;            if(Node.cur = =-1) {cout<<"The problem cannot be solved."<<Endl; }            Else{cout<<"The problem can solved in"<< Node.curstepnum <<"steps:"<<Endl; Vector<string> b =Node.step;  for(inti =0; I <= b.size ()-1; i++) cout<< B[i] <<Endl;; }} t++; cout<<Endl; }}

Uva-321-brute Force enumeration-implicit graph Search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.