zoukankan      html  css  js  c++  java
  • 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;
    }
    
    
    
    
    
    人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想
  • 相关阅读:
    EF支持的注解
    c#数组去重复超级简单
    终于开通博客了。
    理解C#中参数
    一般处理程序中session问题
    小菜的面试所感
    为IOS生成opencv framework时遇到的2个问题
    纯真IP地址数据库qqwry.dat解析
    scala纯函数式编程-排序算法实现
    跳跃列表原理和实现
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2954664.html
Copyright © 2011-2022 走看看