UVA - 10557 XYZZY(DFS + BFS)

XYZZY

Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u

[]  [Go Back]  [Status]  

Description

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 deploymenton 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 ofthe rooms is the finish. Each room has an energy value between -100 and +100. One-waydoorways interconnect pairs of rooms.

The player begins in the start room with 100 energy points. She may pass through anydoorway 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 untilshe 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 energyeach 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 nrooms follows. The input for each room consists of one or more lines containing:

1. the energy value for room i;

2. the number of doorways leaving room i;

3. a list of the rooms that are reachable by the doorways leaving room i.

The start and finish rooms will always have energy level 0. A line containing -1 followsthe last test case.

Output

In one line for each case, output "winnable" if it is possible for the player to win, otherwiseoutput "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

题目大意:

给你的第一行的数字是节点数。 节点数不超过100

然后要求你输入每个节点的能量值,和出度,以及出度所对应的节点。

然后初始化的能量值为100,你每走一个节点都会加上该节点对应的能量值,如果当前的能量值小于等于0,就game over


解析:

这题做了好久,其间忘记了初始化WA了好多次,看了网络上的题解才水过的。

主要是先是用DFS,判断是否存在一条每步的能量值大于0的路线,如果存在继续DFS。

还要考虑是否存在正环,如果存在正环的话能无限刷能量,此时用BFS,判断存在正环的点是否能到达终点,如果能到达终点就win。


#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;

const int N = 105;
int E[N];
int v[N];
int edge[N][N];
int n;
bool bfs(int pos) {
	bool vis[N] = {0};
	queue<int> q;
	q.push(pos);
	vis[pos] = true;
	while( !q.empty()) {
		int front = q.front();
		q.pop();
		for(int i = 1; i <= n; i++) {
			if( !vis[i] && edge[front][i]) {
				if( i == n) {
					return true;
				}
				vis[i] = true;
				q.push(i);
			}
		}
	}
	return false;
}
bool dfs(int pos,int val) {
	if(pos == n) {
		return true;
	}
	for(int i = 1; i <= n; i++) {
// 如果该点已经访问过,且满足这次访问该点的能量,大于上次访问该点的能量
// 则表明构成正环,即可以无限获得能量,直接搜索该点能不能到达终点
		if(edge[pos][i] && v[i] + val > 0) {
			if( !E[i] ) {
				E[i] = val + v[i];
				if( dfs(i,E[i]) ) {
					return true;
				}
			} else if( v[i] + val > E[i] && bfs(i)) {
				return true;
			}
		}
	}
	return false;
}
int main() {
	while(scanf("%d",&n) != EOF && n+1) {
		memset(edge,0,sizeof(edge));
		int m,t;
		for(int i = 1; i <= n; i++) {
			scanf("%d%d",&v[i],&m);
			for(int j = 1; j <= m; j++) {
				scanf("%d",&t);
				edge[i][t] = 1;
			}
		}
		memset(E,0,sizeof(E));
		if(dfs(1,100)) {
			printf("winnable\n");
		}
		else {
			printf("hopeless\n");
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值