10557 - XYZZY 4446
23.50%
817
74.54%

题目链接

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=1498

题目类型: 搜索

题目:

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

题目翻译:

背景:典型的电脑冒险游戏,最早出现在1970年代中期的PDP-10电脑(数字设备公司DEC生产的)上,是由Will Crowther设计的。这是一种尝试由电脑裁判的奇幻游戏。1976年在斯坦福大学的Don Woods把它拓展成一个拼图游戏(Don Woods是INTERCAL的作者之一)。现在更出名的是冒险或巨大的洞穴探险游戏。但是PDP-10的操作系统只允许6个大写字母的文件名。更多信息请查询vadding, Zork,和Infocom.

最近人们发现如何在Y-Crate游戏设备上运行开源软件。很多企业的设计师都有部署开发好的Advent-Style风格游戏在Y-Crate上。你的工作就是测试这些设计是否可能获胜的。

每场比赛由一组100个房间组成。其中一个房间是起点,另一个房间是终点。 每个房间有一个能量值 ,从 -100到+ 100。其中有一些只能单向行走的门把这些房间连接起来。

玩家开始时会自动获得100点能量值。 她可以通过单向房间门进入另一个门,并且可以任意重复次数进入一个房间,而且每次进入那个房间都可以获得那个能量值(正的能量值是增加她的能量,负值减少她的能量)。如果在能量用完(变为0或者负数)之前能够走到终点房间,那么就可以赢。否则,就是输。

样例输入:

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

样例输出:

hopeless
hopeless
winnable
winnable

分析与总结:

这题首先想到的就是用深搜来做。

仔细分析题目可以看出, 可以重复进入一个房间,也就是可以任意次数来往两个房间。这样的话,这个游戏就有个“BUG”:只要有两个相连的房间(循环的), 例如,其中一个为10, 另一个为-2, 那么便可以反复来玩这两个房间,从而可以获得一个无限大的能量! 这样的话,如果可以找到通往终点的路,由于可以获得无限能量,那么就一定可以赢的!

利用这一点,在进行深搜时,一旦发现可以进入之前走过的房间(即有环图),那么就可以进行判断,是否可以获得无限能量。如果可以获得无限能量的话,就再从当前这点开始搜索,看看能否到达终点,如果可以的话,那么就直接判定为胜利!

在网上搜这题解题报告时,会发现很多都是用什么SPFA做的,其实用DFS+特判就可以了。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#define INF 2147483646
using namespace std;
int nRoom, energy[105];
struct Node{int val;int nLeave;int reach[105];
};
Node room[105];
bool vis[105], flag;
int que[100000];// 这个搜索能获得无限能量的点能不能直接到达终点
void bfs(int pos){memset(vis, 0, sizeof(vis));vis[pos] = true;int front=0, rear=1;que[0] = pos;while(front < rear){int q = que[front++];if(q == nRoom){ flag=true; return; }for(int i=0; i<room[q].nLeave; ++i){int m = room[q].reach[i];if(!vis[m] && m>=1 && m<=nRoom) {vis[m] = true;que[rear++] = m;}}}
}void dfs(int pos, int val){if(flag || pos<=0 || pos>nRoom) return;//   printf("Room %d : left = %d\n", pos, val+room[pos].val);if(val <= 0) return;if(pos==nRoom && val>0) { flag = true; return; }for(int i=0; i<room[pos].nLeave; ++i){int m=room[pos].reach[i];// 如果目标是访问过的,并且这次到目标比上次到目标的能量大// 那么可以获得无限能量, 直接搜这个点有没有路径到终点if(energy[m] && energy[m]<val+room[m].val) {bfs(pos);if(flag) return;}if(!energy[m] && val+room[m].val>0){energy[m] = val+room[m].val;dfs(m, val+room[m].val);}}
}int main(){
#ifdef LOCALfreopen("input.txt", "r", stdin);
#endifwhile(~scanf("%d", &nRoom) && nRoom!=-1){for(int i=1; i<=nRoom; ++i){scanf("%d %d", &room[i].val, &room[i].nLeave);for(int j=0; j<room[i].nLeave; ++j)scanf("%d", &room[i].reach[j]);}memset(vis, 0, sizeof(vis));memset(energy, 0, sizeof(energy));energy[1] = 100;flag = false;dfs(1, 100);if(flag) printf("winnable\n");else printf("hopeless\n");}return 0;
}

——      生命的意义,在于赋予它意义。 

                   原创  http://blog.csdn.net/shuangde800  , By   D_Double

UVa 10557 - XYZZY相关推荐

  1. Uva 10557 XYZZY

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. π-Algorithmist分类题目(2)

    原题网站:Algorithmist,http://www.algorithmist.com/index.php/Main_Page π-Algorithmist分类题目(2) Set Theory U ...

  3. 初学者acm的练习题指南

    上机练习题参考题 忘了在哪找的啦~~希望对大家有帮助呦 <!--[if !supportLists]-->1.    <!--[endif]-->Programming Bas ...

  4. 提取了下刘汝佳推荐的题号...

    今天闲来没事上uva oj提取了下刘汝佳推荐的acm题号,原始数据如下: Volume 0. Getting Started    10055 - Hashmat the Brave Warrior ...

  5. [搜索]UVa 129 困难的串

    题意:将一个包含两个相邻的重复子串的子串,称为"容易的串",其他为"困难的串". 输入正整数n和l,输出由前l个字符组成的,字典序第n小的困难的串. 输入样例: ...

  6. uva 401.Palindromes

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  7. Uva 3767 Dynamic len(set(a[L:R])) 树套树

    Dynamic len(set(a[L:R])) Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://uva.onlinejudge.org/in ...

  8. UVA 11752 超级幂

    UVA 11752 超级幂 Z - The Super Powers Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & ...

  9. UVa 11174 - Stand in a Line

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

最新文章

  1. 【SQL】sql语句LEFT JOIN(拼接表)详解
  2. 用python画玫瑰花代码-Python | 用turtle库画玫瑰花
  3. [C++]VisualAssistX中文注释提示错误 解决办法
  4. assert宏的用法
  5. Codeforces Round #335 (Div. 1)--C. Freelancer's Dreams 线性规划对偶问题+三分
  6. SVG脚本编程简介(转)
  7. hihocoder 第113周 Fibonacci(动态规划)
  8. react native命令行全局安装
  9. java导入数据 neo4j_java-neo4j-使用neo4j剩余图数据库进行批量插...
  10. buntu下shell脚本运行异常:bash和…
  11. 更换介质:请把标有“Debian GNU/Linux 10.2.0 _Buster_ - Officia的盘片插入驱动器“/media/cdrom/”再按「回车」键
  12. idea如何导出maven项目
  13. fences卸载_win10系统卸载fences的操作方法
  14. 戏人看戏,苏旭博客网-学无止尽
  15. OAuth 2.0设计(以微信登录为例)
  16. python爬取58同城租房信息,用selenium爬取58同城租房信息(万级数据)
  17. 【方法论】从入门到精通是怎样一种体验
  18. 王传福的梦想,不止7000亿
  19. c调用python第三方库_c/c++调用python程序且含tensorflow第三方库
  20. [原创] 在MFC中大家都习惯用CStdioFile来处理文本文件,可是为什么CStdioFile不叫CTextFile?

热门文章

  1. 全球气候数据下载方式
  2. 雪城大学信息安全试验_国际信息安全系列讲座之二:雪城大学Heng Yin教授讲座通知...
  3. 游戏服务器之防御式开发
  4. ISTJ型人格的性格缺陷和心理问题分析
  5. aardio控件字体大小随控件缩放
  6. 数字化工厂车间条形码应用视频
  7. window环境下部署hbase(仅测试环境)
  8. SpringCloud学习Day1
  9. 使用html仿支付宝首页,jQuery实现仿Alipay支付宝首页全屏焦点图切换特效
  10. 2021-11-20~2021-11-21 叕是一个躺尸的周末