UVA - 10557 XYZZY

来源:互联网 发布:java jar 选择 jdk 编辑:程序博客网 时间:2024/05/16 16:56

Problem D: XYZZY


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 developedAdvent-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) ton (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

50 1 2-60 1 3-60 1 420 1 50 050 1 220 1 3-60 1 4-60 1 50 050 1 221 1 3-60 1 4-60 1 50 050 1 220 2 1 3-60 1 4-60 1 50 0-1

Output for Sample Input

hopelesshopelesswinnablewinnable题意:

输入n有n个点,从1到n标号,起点为1,终点为n

第一个数字是该点所带的能量值,第二个数字是该点的出度m,接下来的m个数字表示和该点接连的点;

刚开始的能量值是100,到达新的点,新的能量值等于之前的能量值加上该点的能量值,

另外一个点是可以多次到达的winnable的条件:

任何一个的时候能量值都必须大于0,而且最后能到达终点;

当发现存在正环的时候,只要判断是否能到达终点既可以;



#include <iostream>#include <cstdio>#include <cstring>#include <queue>#define N 110using namespace std;int a[N][N];int e[N], d[N];int vis[N];int n;int bfs(int u) {queue<int> q;q.push(u);vis[u] = 1;while (!q.empty()) {int t = q.front();q.pop();for (int i = 1; i <= a[t][0]; i++) {int v = a[t][i];if (vis[v]== 0) {q.push(v);vis[v] = 1;if ( v == n)return 1;}}}return 0;}int dfs(int u,int E){    if(u==n)  return 1;    d[u]=E+e[u];    for(int i=1; i<=a[u][0]; i++)    {        int v = a[u][i];        if( d[u] + e[v] > 0 )  //到达点v的话还有能量,存在递归的可能        {            if(d[v] == 0)   //即点v还没有被访问过,那么直接访问即可            {                if(dfs(v,d[u]))                    return 1;            }            else if( d[u] + e[v] > d[v] )            //点v已经被访问,如果循环,值更大,那么就是正环,那么就从点v出发,能到达终点即可            {                 memset(vis,0,sizeof(vis));                if(bfs(v))                    return 1;            }        }    }    return 0;}int main() {while (scanf ("%d",&n) != EOF && n != -1) {memset(a,0,sizeof(a));memset(e,0,sizeof(e));memset(d,0,sizeof(d));for (int i = 1; i <= n; i++) {scanf("%d",&e[i]);scanf("%d",&a[i][0]);for (int j = 1; j <= a[i][0]; j++)scanf("%d",&a[i][j]);}if (dfs(1,100))printf("winnable\n");elseprintf("hopeless\n");}return 0;}



 
0 0