UVaOJ10557---XYZZY

10557 - XYZZY

Time limit: 3.000 seconds

ADVENT: /ad�vent/, n.
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 thefinish. 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.

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.

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

Output for Sample Input

hopeless
hopeless
winnable
winnable

G. V. Cormack

这个题目实际上是求一个从起点到终点的最长路。

但是由于路过每个点时权值都不能为负,所以我们在初始化距离数组d[]时要初始化0。之后就是用队列优化的Bellman-Ford算法去求最长路了,但是还有一些细节需要注意。

我们当然可以在用这个算法时选择d[n]>0是就跳出循环来减少计算量,但也会找到使这个算法崩溃的例子,比如在某个位置存在一个正圈,而由这个正圈却不能到达终点,那么程序就会一直跑下去。但同样的,我们也不能见圈就跳,因为毕竟还是有些正圈可以到达终点的。这便要求我们去判断哪些正圈可以到达终点。

于是我们可以选择在找最长路之前先做一下预处理,把所有能够到达终点的点找出来,一个比较好的办法就是从终点开始逆向深搜,然后依次标记搜到的点即可,如果最后起点没有被标记,那就直接输出hopeless就可以了。

后面在使用队列优化的Bellman-Ford算法时,我们在判断条件上多加一个是否可达终点的判断即可,如果该点可达终点我们再进行求最长路的操作。


#include<stdio.h>
#include<string.h>
int G[110][110],w[110],d[110],n;
int q[110],inq[110],inedq[110];
int vis[110],reach[110]; 
void dfs(int v)
{
    int u;
    for(u=1;u<=n;u++)
        if(G[u][v]&&!vis[u])
        {
            vis[u]=1;
            reach[u]=1;
            dfs(u);
        }
}
int main()
{
    int i,j,k,u,v,num,front,rear,flag;
    while(1)
    {
        scanf("%d",&n);
        if(n==-1)
            break;
        memset(G,0,sizeof(G));
        for(u=1;u<=n;u++)
        {
            scanf("%d",&w[u]);
            scanf("%d",&num);
            for(i=0;i<num;i++)
            {
                scanf("%d",&v);
                G[u][v]=1;
            }
        }
        memset(vis,0,sizeof(vis));
        memset(reach,0,sizeof(reach));
        reach[n]=1;
        dfs(n);
        if(!reach[1])
        {        
            printf("hopeless\n");
            continue;
        }
        memset(inq,0,sizeof(inq));
        memset(inedq,0,sizeof(inedq));
        memset(d,0,sizeof(d));
        front=rear=0;
        d[1]=100;
        q[rear++]=1;
        inq[1]=1;
        inedq[1]++;
        flag=0;
        while(front!=rear)
        {
            u=q[front++];
            if(front>n)
                front=0;
            inq[u]=0;
            for(v=1;v<=n;v++)
                if(G[u][v]&&reach[v]&&d[u]+w[v]>d[v])
                {
                    d[v]=d[u]+w[v];
                    if(!inq[v])
                    {
                        q[rear++]=v;
                        if(rear>n)
                            rear=0;
                        inq[v]=1;
                        if(inedq[v]++>n)
                        {
                            flag=1;
                            break;
                        }
                    }
                }
            if(d[n]>0||flag)
                break;
        }
        if(d[n]>0||flag)
            printf("winnable\n");
        else
            printf("hopeless\n");
    }
    return 0;    
}

转载自:http://www.cnblogs.com/staginner/archive/2011/09/09/2172821.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值