zoukankan      html  css  js  c++  java
  • ZOJ 1301 The New Villa



    The New Villa

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 4   Accepted Submission(s) : 2
    Special Judge
    Problem Description
    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 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 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.

     

    Source
    Southwest Europe 1996
     




    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <queue>

    using namespace std;

    struct Node
    {
        int room,step,state;
    };

    Node path[11][1200];

    bool map[11][11];
    bool vis[11][1200];
    bool turn[11][11];
    int d[11][1200];
    string ans[1200];
    int num[1200];
    queue<Node> q;

    int main()
    {
        int n,m,k,i,j;
        Node ss,tt;
        int cas=1;
        while(cin>>n>>m>>k&&n)
        {
            cout<<"Villa #"<<cas++<<endl;

            memset(map,false,sizeof(map));
            memset(vis,false,sizeof(vis));
            memset(turn,false,sizeof(turn));

            for(i=0;i<m;i++)
            {
                int x,y;
                cin>>x>>y;
                map[x][y]=map[y][x]=true;
            }

            for(i=0;i<k;i++)
            {
                int x,y;
                cin>>x>>y;
                turn[x][y]=true;
            }

            d[1][1]=0;
            vis[1][1]=true;
            ss.room=1; ss.state=1; ss.step=0;
            path[1][1]=ss;

            while(!q.empty()) q.pop();

            ///BFS BFS BFS BFS BFS BFS

            q.push(ss);

            while(!q.empty())
            {
                ss=q.front(); q.pop();
                d[ss.room][ss.state]=ss.step;

                for(i=1;i<=n;i++)///开关灯
                {
                    if(i==ss.room||!turn[ss.room]) continue;
                    tt=ss;
                    tt.state=tt.state^(1<<(i-1));
                    tt.step=ss.step+1;
                    if(!vis[tt.room][tt.state])
                    {
                        vis[tt.room][tt.state]=true;
                        q.push(tt);
                        path[tt.room][tt.state]=ss;
                    }
                }

                for(i=1;i<=n;i++)///换房间
                {
                    tt=ss;
                    if(!map[tt.room]) continue;
                    if((tt.state&(1<<(i-1)))==0) continue;
                    if(!vis[tt.state])
                    {
                        tt.step++;
                        tt.room=i;
                        vis[tt.room][tt.state]=true;
                        q.push(tt);
                        path[tt.room][tt.state]=ss;
                    }
                }
            }

            i=n;
            j=(1<<(n-1));
            if(!vis[j])
            {
                cout<<"The problem cannot be solved."<<endl<<endl;
                continue;
            }

            cout<<"The problem can be solved in "<<d[j]<<" steps:"<<endl;

            int  top=0;
            int x,y;
            while(true)
            {
                if(path[j].room==i&&path[j].state==j)
                {
                    break;
                }

                if(path[j].room!=i)
                {
                    ans[top]="move";
                    num[top]=i;
                    top++;
                }
                else
                {
                    for(k=0;k<=m+2;k++)
                    {
                        x=j&(1<<k);
                        y=path[j].state&(1<<k);
                        if(x!=y)
                            break;
                    }
                    if(x)
                    {
                        ans[top]="on";
                        num[top]=k+1;
                        top++;
                    }
                    else
                    {
                        ans[top]="off";
                        num[top]=k+1;
                        top++;
                    }
                }

                x=path[j].room;
                y=path[j].state;

                i=x; j=y;
            }

             for(i=top-1;i>=0;i--)
            {
                if(ans=="move")
                    cout<<"- Move to room "<<num<<"."<<endl;
                if(ans=="on")
                    cout<<"- Switch on light in room "<<num<<"."<<endl;
                if(ans=="off")
                    cout<<"- Switch off light in room "<<num<<"."<<endl;
            }
            cout<<endl;
        }

        return 0;
    }

  • 相关阅读:
    nodeName,nodeValue未知 xml 入库方案 The ElementTree iterparse Function
    如何:执行大型 XML 文档的流式转换 大XML文件解析入库的一个方法
    python curl_get-pip.py Installing with get-pip.py
    iptables List the rules in a chain or all chains
    goroutine 分析 协程的调度和执行顺序 并发写 run in the same address space 内存地址 闭包 存在两种并发 确定性 非确定性的 Go 的协程和通道理所当然的支持确定性的并发方式(
    数据库业界
    The MEAN stack is a modern replacement for the LAMP (Linux, Apache, MySQL, PHP/Python) stack
    Using Groovy To Import XML Into MongoDB
    虚拟机网络模式 桥接 网桥 交换机
    防止有内存泄漏并尽可能快地释放所有内存是内存管理的重要组成部分 长时间运行进程的内存泄漏
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350976.html
Copyright © 2011-2022 走看看