UVA_10557&POJ_1932 XYZZY 正环负环搜索

XYZZY
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 3633   Accepted: 1046

Description

The prototypical computer adventure game, first designed by Will Crowther on the PDP-10 in the mid-1970s as an attempt at computer-refereed fantasy gaming, and expanded into a puzzle-oriented game by Don Woods at Stanford in 1976. (Woods had been one of the authors of INTERCAL.) Now better known as Adventure or Colossal Cave Adventure, but the TOPS-10 operating system permitted only six-letter filenames in uppercase. See also vadding, Zork, and Infocom. 

It has recently been discovered how to run open-source software on the Y-Crate gaming device. A number of enterprising designers have developed Advent-style games for deployment on the Y-Crate. Your job is to test a number of these designs to see which are winnable. 

Each game consists of a set of up to 100 rooms. One of the rooms is the start and one of the rooms is the finish. Each room has an energy value between -100 and +100. One-way doorways interconnect pairs of rooms. 

The player begins in the start room with 100 energy points. She may pass through any doorway that connects the room she is in to another room, thus entering the other room. The energy value of this room is added to the player's energy. This process continues until she wins by entering the finish room or dies by running out of energy (or quits in frustration). During her adventure the player may enter the same room several times, receiving its energy each time. 

Input

The input consists of several test cases. Each test case begins with n, the number of rooms. The rooms are numbered from 1 (the start room) to n (the finish room). Input for the n rooms follows. The input for each room consists of one or more lines containing: 
  • the energy value for room i 
  • the number of doorways leaving room i 
  • a list of the rooms that are reachable by the doorways leaving room i

The start and finish rooms will always have enery level 0. A line containing -1 follows the last test case.

Output

In one line for each case, output "winnable" if it is possible for the player to win, otherwise output "hopeless".

Sample Input

5
0 1 2
-60 1 3
-60 1 4
20 1 5
0 0
5
0 1 2
20 1 3
-60 1 4
-60 1 5
0 0
5
0 1 2
21 1 3
-60 1 4
-60 1 5
0 0
5
0 1 2
20 2 1 3
-60 1 4
-60 1 5
0 0
-1

Sample Output

hopeless
hopeless
winnable

winnable


题意:有n个房间,编号1--n,每个房间有一定数值的能量(可正可负可0),开始有100点能量,问能否从房间1走到房间n。


理解:只有100个点,图也给好了,那就直接暴力搜索就行。


有几个点需要注意,是否有正环,如果有正环,那么就可以无限循环的跑这个正环,使能量点数达到无限大,后面dfs的过程中就不需要考虑能量点的问题,只看能否到达n点就行了。


如果不是正环,就不用跑这个环(能量点只能多不能少),我用一个energy数组记录上次跑过这个点的能量,顺便还可以起个标记的作用。


CODE:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>

using namespace std;
const int maxn = 100+10;

int n;
int roomen[maxn];    ///每个房间的能量
int energy[maxn];    ///记录走到每个房间剩余的能量
vector<int> v[maxn]; ///存图
bool vis[maxn];      ///发现有正环的时候用来标记的数组
bool flag;           ///true能到达,false不能到达

void INIT()          ///初始化
{
    flag = false;
    for(int i = 1;i < maxn;i++)
    {
        v[i].clear();
        vis[i] = false;
        energy[i] = 0;
    }
    energy[1] = 100;
}

void dfs1(int en)     ///发现有正环了后只需要dfs一遍能否到达就行
{
    if(en == n || flag)
    {
        flag = true;
        return;
    }
    vis[en] = true;
    for(int i = 0;i < v[en].size();i++)
    {
        int t = v[en][i];
        if(!vis[t])
            dfs1(t);
    }
}

void dfs(int st)   ///起点开始跑dfs
{
    if(st == n || flag)
    {
        flag = true;
        return;
    }
    for(int i = 0;i < v[st].size();i++)
    {
        int en = v[st][i];
        int nextgy = energy[st]+roomen[en];   ///下一个点的能量
        if(energy[en] && energy[en] < nextgy) ///发现有正环
        {
            dfs1(en);
            if(flag) return;
        }
        if(!energy[en] && nextgy > 0)         ///不是正环直接跑就是了
        {
            energy[en] = nextgy;
            dfs(en);
        }
    }
}

int main(void)
{
    while(scanf("%d",&n) && n != -1)
    {
        INIT();
        for(int i = 1;i <= n;i++)
        {
            int cnt;
            scanf("%d%d",&roomen[i],&cnt);
            while(cnt--)
            {
                int t;
                scanf("%d",&t);
                v[i].push_back(t);
            }
        }
        dfs(1);
        if(!flag)
            printf("hopeless\n");
        else
            printf("winnable\n");
    }
    return 0;
}


你可能感兴趣的:(poj,DFS,uva)