• UVA 321 The New Villa (搜索)


    The New Villa

    Mr. Black recently bought a villa in the countryside. Only one thing bothers him: although there are light switches in most rooms, the lights they control are often in other rooms than the switches themselves. While his estate agent saw this as a feature, Mr. Black has come to believe that the electricians were a bit absent-minded (to put it mildly) when they connected the switches to the outlets.

    One night, Mr. Black came home late. While standing in the hallway, he noted that the lights in all other rooms were switched off. Unfortunately, Mr. Black was afraid of the dark, so he never dared to enter a room that had its lights out and would never switch off the lights of the room he was in.

    After some thought, Mr. Black was able to use the incorrectly wired light switches to his advantage. He managed to get to his bedroom and to switch off all lights except for the one in the bedroom.

    You are to write a program that, given a description of a villa, determines how to get from the hallway to the bedroom if only the hallway light is initially switched on. You may never enter a dark room, and after the last move, all lights except for the one in the bedroom must be switched off. If there are several paths to the bedroom, you have to find the one which uses the smallest number of steps, where ``move from one room to another'', ``switch on a light'' and ``switch off a light'' each count as one step.

    Input

    The input file contains several villa descriptions. Each villa starts with a line containing three integers r, d, and s. r is the number of rooms in the villa, which will be at most 10. d is the number of doors/connections between the rooms and s is the number of light switches in the villa. The rooms are numbered from 1 to r; room number 1 is the hallway, room number r is the bedroom.

    This line is followed by d lines containing two integers i and j each, specifying that room i is connected to room j by a door. Then follow s lines containing two integers k and l each, indicating that there is a light switch in room k that controls the light in room l.

    A blank line separates the villa description from the next one. The input file ends with a villa having r = d = s = 0, which should not be processed.

    Output

    For each villa, first output the number of the test case (`Villa #1', `Villa #2', etc.) in a line of its own.

    If there is a solution to Mr. Black's problem, output the shortest possible sequence of steps that leads him to his bedroom and only leaves the bedroom light switched on. (Output only one shortest sequence if you find more than one.) Adhere to the output format shown in the sample below.

    If there is no solution, output a line containing the statement`The problem cannot be solved.'

    Output a blank line after each test case.

    Sample Input

    3 3 4
    1 2
    1 3
    3 2
    1 2
    1 3
    2 1
    3 2
    
    2 1 2
    2 1
    1 1
    1 2
    
    0 0 0

    Sample Output

    Villa #1
    The problem can be solved in 6 steps:
    - Switch on light in room 2.
    - Switch on light in room 3.
    - Move to room 2.
    - Switch off light in room 1.
    - Move to room 3.
    - Switch off light in room 2.
    
    Villa #2
    The problem cannot be solved.

    这题比较有意思,就是进行简单地搜索。
    注意判重的方法呢。
    还有记录搜索过程。
    要状态压缩记录灯的状态。

    #include <stdio.h>
    #include <iostream>
    #include <algorithm>
    #include <string.h>
    #include <queue>
    using namespace std;
    
    const int MAXN=10;
    
    struct Node
    {
        int state;
        int t;
        int now;
    };
    
    int r;
    bool used[1<<MAXN][MAXN];
    int pre1[1<<MAXN][MAXN];
    int pre2[1<<MAXN][MAXN];
    bool g[MAXN][MAXN];
    bool sw[MAXN][MAXN];
    queue<Node>q;
    
    int bfs()
    {
        while(!q.empty())q.pop();
        memset(used,false,sizeof(used));
        Node tmp;
        tmp.state=1;
        tmp.now=0;
        tmp.t=0;
        used[tmp.state][tmp.now]=true;
        q.push(tmp);
        Node next;
        while(!q.empty())
        {
            tmp=q.front();
            q.pop();
            if(tmp.state==(1<<(r-1)) && tmp.now==r-1)return tmp.t;
            for(int j=0;j<r;j++)
              if(j!=tmp.now && g[tmp.now][j] && (tmp.state&(1<<j)))
              {
                  next=tmp;
                  next.now=j;
                  next.t=tmp.t+1;
                  if(!used[next.state][next.now])
                  {
                      pre1[next.state][next.now]=tmp.state;
                      pre2[next.state][next.now]=tmp.now;
                      q.push(next);
                      used[next.state][next.now]=true;
                  }
              }
            for(int j=0;j<r;j++)
              if(sw[tmp.now][j] && j!=tmp.now )
              {
                  next=tmp;
                  next.state=tmp.state^(1<<j);
                  next.t=tmp.t+1;
                  if(!used[next.state][next.now])
                  {
                      pre1[next.state][next.now]=tmp.state;
                      pre2[next.state][next.now]=tmp.now;
                      q.push(next);
                      used[next.state][next.now]=true;
                  }
              }
        }
        return -1;
    }
    void print(int state,int now)
    {
        if(state==1 && now==0)return;
        print(pre1[state][now],pre2[state][now]);
        if(state==pre1[state][now])printf("- Move to room %d.\n",now+1);
        else
        {
            for(int i=0;i<r;i++)
            {
                if( (state&(1<<i))==0 && (pre1[state][now]&(1<<i))!=0 )
                {
                    printf("- Switch off light in room %d.\n",i+1);
                    return;
                }
                else if( (state&(1<<i))!=0 && (pre1[state][now]&(1<<i))==0 )
                {
                    printf("- Switch on light in room %d.\n",i+1);
                    return;
                }
            }
        }
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
       // freopen("out.txt","w",stdout);
        int iCase=0;
        int d,s;
        int x,y;
        while(scanf("%d%d%d",&r,&d,&s)==3)
        {
            iCase++;
            if(r==0&&d==0&&s==0)break;
            memset(g,false,sizeof(g));
            memset(sw,false,sizeof(sw));
            while(d--)
            {
                scanf("%d%d",&x,&y);
                g[x-1][y-1]=g[y-1][x-1]=true;
            }
            while(s--)
            {
                scanf("%d%d",&x,&y);
                sw[x-1][y-1]=true;
            }
            printf("Villa #%d\n",iCase);
            int ans=bfs();
            if(ans==-1)printf("The problem cannot be solved.\n");
            else
            {
                printf("The problem can be solved in %d steps:\n",ans);
                print(1<<(r-1),r-1);
            }
            printf("\n");
        }
        return 0;
    }
    
    
    
    
    
    人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想
  • 相关阅读:
    查看python中已安装的包有哪些
    error: pathspec 'master' did not match any file(s) known to git.
    【RF库Collections测试】lists should be equal
    Robot Framework配置发送邮件功能
    【转载】浅谈TDD、BDD与ATDD软件开发
    Json转换工具类(基于google的Gson和阿里的fastjson)
    Solr系列三:solr索引详解(Schema介绍、字段定义详解、Schema API 介绍)
    Solr系列二:solr-部署详解(solr两种部署模式介绍、独立服务器模式详解、SolrCloud分布式集群模式详解)
    Solr系列一:Solr(Solr介绍、Solr应用架构、Solr安装使用)
    Lucene系列六:Lucene搜索详解(Lucene搜索流程详解、搜索核心API详解、基本查询详解、QueryParser详解)
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2954664.html
Copyright © 2020-2023  润新知