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

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

题意: 给定n,表示有n个房间,接下去输入n个房间的信息。信息有: 进入该房间会得到(失去)多少HP。和该房间能通到哪些房间。。初始血量为100,血量为0就死了,要求出能否从房间1走到房间n。 如果不能走到就输出hopeless,如果能就输出winnable。

思路: 注意这个游戏是有BUG的,比如如果在两个房间之间来回走可以回血,就可以把血量补到无穷大。。

这样判断,如果遇到回路,并且走完过回路会加血。。并且有路可以到终点房间,就是winnable。

如果回路走完加血量为0或者扣血了。。这条回路就没必要走。。

因此,思路即可以转换为:如果找到一条回路可以补血,并且可以走到终点。或者可以走到终点血量不为0.就输出winnable。

就用个搜索先把回路找出来。。如果有可以补血的回路,在从该点搜索能不能到终点。就成功了。。

#include <stdio.h>
#include <string.h> int n;
int judge;
int vis[105];
int hpp[105];
struct R
{
int hp;
int num;
int troom[105];
} room[105]; void dfs2(int fang)
{
if (fang == n)
{
judge = 1;
return;
}
for (int i = 0; i < room[fang].num; i ++)
{
int m = room[fang].troom[i];
if (vis[m] == 0)
{
vis[m] = 1;
dfs2(m);
vis[m] = 0;
}
}
}
void dfs(int xue, int fang)
{
if (judge)
return;
if (xue <= 0)
return;
if (fang == n && xue > 0)
{
judge = 1;
return;
}
for (int i = 0; i < room[fang].num; i ++)
{
int m = room[fang].troom[i];
if (hpp[m] && hpp[m] < xue + room[m].hp)
{
dfs2(fang);
if (judge)
return;
}
if (!hpp[m] && xue + room[m].hp > 0)
{
hpp[m] = xue + room[m].hp;
dfs(xue + room[m].hp, m);
}
}
} int main()
{
while (scanf("%d", &n) != EOF && n != -1)
{
judge = 0;
memset(room, 0, sizeof(room));
memset(vis, 0, sizeof(vis));
memset(hpp,0,sizeof(hpp));
hpp[1] = 100;
for (int i = 1; i <= n; i ++)
{
scanf("%d%d", &room[i].hp, &room[i].num);
for (int j = 0; j < room[i].num; j ++)
scanf("%d", &room[i].troom[j]);
}
dfs(100, 1);
if (!judge)
printf("hopeless\n");
else
printf("winnable\n");
}
return 0;
}

最新文章

  1. 菜鸟Python学习笔记第一天:关于一些函数库的使用
  2. 【点滴积累,厚积薄发】windows schedule task的最小时间间隔是多少?
  3. C++多重继承带来的问题
  4. 管理windows防火墙
  5. Android电源管理-休眠简要分析
  6. Java Concurrency - ScheduledThreadPoolExecutor
  7. 用jquery-easyui中的combotree实现树形结构的选择
  8. jasperreports-5.6 + jaspersoftstudio-5.6 生成pdf 文件中文无法正常显示问题
  9. css实现的透明三角形
  10. IOS开发 统计XCODE 代码行数
  11. Cocos2d-X研究之v3.x瓦片地图具体解释
  12. 实战-Mysql5.6.36脚本编译安装及初始化
  13. PAT1118. Birds in Forest (并查集)
  14. 【一天一道LeetCode】#17. Letter Combinations of a Phone Number
  15. 多线程thread的使用
  16. jquery.form.js 实现异步上传
  17. Maven 使用了一个标准的目录结构和一个默认的构建生命周期。
  18. Vue 过渡
  19. windows7触屏操作API
  20. 剑指offer五十之数组中重复的数字

热门文章

  1. php练习5——简单的学生管理系统(隐藏控件的使用)
  2. hadoop1 和haddop2 mapperreducer的wordcount详解
  3. windows下python 编码问题
  4. 在Django中使用Mako模板
  5. 如何利用C生成.so供Mono调用
  6. C语言中调用Lua
  7. redis入门教程
  8. bzoj 2852: 强大的区间 辗转相除
  9. Django自定义上传目录
  10. Android的Handler总结