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.
(neglect above !!)
题目类似于一个闯关的游戏,开始满血值为100,闯关的每个房子都有其对应的“能量”,能量有正有负,进去之后就获得能量(你的血量加上房子的能量,结果可能为负数)如果到出口之前你的血量为零或负数,那么这时你就失败输出"hopeless",注意房子可以重复进去,所以可能会有能量一直增长的循环,这里要进行判断。房子之间相不相连在输入时已经有给说明(邻接表的形式说明)
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".
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
G. V. Cormack
#include#include #include #define MAXN 110typedef struct{ int value, visit;}maze; // value的作用:1是可以存储能量,2是存储跟当前房子连接的房子的数量;visit的作用是在遍历的时候查看是否已被遍历, // 并且存储将要遍历时当前闯关人的血的能量maze game[MAXN][MAXN];int flag = 0;/*void PrintMaze(int n){ int i, j; for(i=1; i<=n; ++i) { printf("[%d]: energy = %d, listnumber = %d\n", i, game[0][i].value, game[i][0].value); for(j=1; j<=game[i][0].value; ++j) { printf("%d[%d] ", game[i][j].value, game[i][j].visit); } printf("\n"); } return;}*/int TraverseOut(int u, int n){//TraverseOut函数的作用是通过广度查找,查看u节点跟出口是否连接,如果连接返回1, 否则返回0 int visit[MAXN], queue[MAXN]; int front, rear, i, t, j; memset(visit, 0, sizeof(visit)); front = rear = 0; visit[u] = 1; queue[rear++] = u; while(front < rear) { t = queue[front++]; for(i=1; i<=game[t][0].value; ++i) if(!visit[game[t][i].value]) { if(n == game[t][i].value) return 1; queue[rear++] = game[t][i].value; visit[game[t][i].value] = 1; } } return 0;}void Traverse(int current, int n, int energy){// Traverse函数的作用是通过深度查找模拟题目情景~~ int i, j, cnt, m; energy = energy + game[0][current].value; if(energy > 0 && current == n) {//如果能量大于零而且到达了重点,则flag = 1并返回 flag = 1; return; } else if(energy <= 0) return; //夭折了也要返回 for(i=1; i<=game[current][0].value; ++i) { // m = game[0][game[current][i].value].value; if(game[current][i].visit != 0) {//不等于零说明之前已经有走过这间房子,而且里面保存的是将要进去这间房子时的血量 if(game[current][i].visit < energy && TraverseOut(game[current][i].value, n)) {//满足:回来的时候血量比之前增加了,而且这间房到终点有通路 flag = 1; return; } else continue; } else game[current][i].visit = energy; Traverse(game[current][i].value, n, energy); if(flag) return; /* game[current][i].visit = 0; */ //因为这里让超时不断啊 } return; }int main(){ int n, m, i, j, cnt, sum; while(scanf("%d", &n) != EOF && n != -1) { for(i=1; i<=n; ++i) {//game矩阵中第一行存储对应号数房子的能量值 scanf("%d", &game[0][i].value); scanf("%d", &game[i][0].value); //第二行开始每一行的第一个位置存储跟它连接的房子的数量,后面紧接着存储房子的“地址” game[i][0].visit = 0; for(j=1; j<=game[i][0].value; ++j) { scanf("%d", &game[i][j].value); game[i][j].visit = 0; } }/* PrintMaze(n); */ sum = 100; flag = 0, cnt = 1; Traverse(cnt, n, sum); if(flag) printf("winnable\n"); else printf("hopeless\n"); } return 0;}
解题思路:
DFS+BFS