uva 10557 XYZZY(DFS+BFS)

来源:互联网 发布:java 递归算法阶乘 编辑:程序博客网 时间:2024/05/16 03:18

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 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 anenergy 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

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
题目大意:一个闯关的游戏,开始满血值为100,闯关的每个房子都有其对应的“能量”,能量有正有负,进去之后就获得能量(你的血量加上房子的能量,结果可能为负数)如果到出口之前你的血量为零或负数,那么这时你就失败输出"hopeless",注意房子可以重复进去,所以可能会有能量一直增长的循环,这里要进行判断。房子之间相不相连在输入时已经有给说明(邻接表的形式说明)

解题思路:最早是用DFS+回溯找环(出现正向环且与终点联通的情况为胜利,负向环和0向环切断),然后再用BFS搜索通路(因为环已经都被切短掉,所以不会有死循环),但是这样做一直超时。然后在网上看到说用spfa算法,可是为又不会,可是后来看到一个牛人用的方法,直接用DFS收索,但是每个房间有自己的能量状态值记录,如果第二次到的时候能量值比当前能量值小就不能满足(这样可以排除环的影响)。

下面给出一些数据:

5

0 3 2 3 4

-1 1 1

2 1 2

-100 1 5

0 0

5

0 1 2

1 2 3 4

-1 1 2

-100 1 5

5

0 1 2

1 2 3 4

-1 1 2

-101 1 5

0 0

4

0 1 2

-1 2 1 3

-98 1 4

0 0

4

0 1 2

-1 2 1 3

-99 1 4

0 0

#include<stdio.h>#include<string.h>using namespace std;#define N 105int n, value[N], map[N][N];int vis[N], energy[N];int que[N];int BFS(int k){memset(vis, 0, sizeof(vis));memset(que, 0, sizeof(que));int front, end;front = end = 0;vis[k] = 1;que[end++] = k;while (front < end){int now = que[front++];for (int i = 1; i <= n; i++){if (map[now][i] && !vis[i]){if (i == n)return 1;que[end++] = i;vis[i] = 1;}}}return 0;}int DFS(int k, int sum){if (k == n)return 1;else{for (int i = 1; i <= n; i++){if (map[k][i] && sum + value[i] > 0){if (!energy[i]){energy[i] = sum + value[i];if (DFS(i, energy[i]))return 1;}else if (sum + value[i] > energy[i] && BFS(i))return 1;}}}return 0;}int main(){        int a, m;    while (scanf("%d", &n), n >= 0){        // Init.        memset(value, 0, sizeof(value));        memset(map, 0, sizeof(map));memset(energy, 0, sizeof(energy));                // Read.        for (int i = 1; i <= n; i++){            scanf("%d%d", &value[i], &m);            for (int j = 0; j < m; j++){                scanf("%d", &a);                map[i][a] = 1;            }        }                // Find.        if (DFS(1, 100))            printf("winnable\n");        else             printf("hopeless\n");    }    return 0;}