-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgameofgames.c
More file actions
87 lines (74 loc) · 3.59 KB
/
gameofgames.c
File metadata and controls
87 lines (74 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Name: Lucas Ryan
* Date: April 24, 2016
* Filename: gameofgames.c
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX_GAMES 8 // can have between 2 and 8 games
#define MAX_TIME 100000 // max time to travel to each game
int openTimes[MAX_GAMES + 1][2]; //holds opening and closing time
int travelTimes[MAX_GAMES + 1][MAX_GAMES + 1]; // stores time to get to locations
int numGames;
int solve(int gameSeq[], int playedGame[], int game);
int isPossiblePath(int gameSeq[], int playTime);
int findTime(int gameSeq[], int lowTime, int highTime);
int main() {
int numCases, i;
scanf("%d", &numCases);
for(i = 0; i < numCases; i++) {
int j;
int gameSeq[MAX_GAMES] = {0, 0, 0, 0, 0, 0, 0, 0}; // generate possible games
// keep track of game locations traveled to, set first element to one so know have been to that location
int playedGame[MAX_GAMES + 1] = {1, 0, 0, 0, 0, 0, 0, 0, 0};
scanf("%d", &numGames); // get number games to play
// get the times each games is open to play
for(j = 1; j <= numGames; j++)
scanf("%d%d", &openTimes[j][0], &openTimes[j][1]);
// get all times takes to get to each game
for(j = 0; j <= numGames; j++) {
int k;
for(k = 0; k <= numGames; k++)
scanf("%d", &travelTimes[j][k]);
}
printf("%d\n", solve(gameSeq, playedGame, 0));
}
return 0;
}
// uses associated array to keep track of the games that we
// played and generate the different ways to play the games
int solve(int gameSeq[], int playedGame[], int game) {
if(game == numGames) return findTime(gameSeq, 0, MAX_TIME); // if reached all
int i, maxPlayTime = 0;
for(i = 0; i <= numGames; i++) {
if(!playedGame[i]) {
playedGame[i] = 1; // flag game as played
gameSeq[game] = i; // store next which game playing in current game sequence
int tryTime = solve(gameSeq, playedGame, game + 1);
maxPlayTime = (maxPlayTime < tryTime) ? tryTime : maxPlayTime; // update max time can be at game if needed
playedGame[i] = 0; // unflag game as being played
}
}
return maxPlayTime;
}
// implement recersive binary search to find max average amount
// of time player can stay at the games
int findTime(int gameSeq[], int lowTime, int highTime) {
if(highTime - 1 <= lowTime) return isPossiblePath(gameSeq, lowTime + 1) ? lowTime + 1 : lowTime;
int middleTime = (lowTime + highTime) / 2;
// check to see if we should increase or decrease amount of time can play each game
if(isPossiblePath(gameSeq, middleTime)) return findTime(gameSeq, middleTime, highTime); // increment time if came back as a win
else return findTime(gameSeq, lowTime, middleTime - 1); // else lower score
}
// checks if path we are using and time alloted self can be done
// where the real knapsack algorithm is taken care of
int isPossiblePath(int gameSeq[], int playTime) {
int currentTime = travelTimes[0][gameSeq[0]], i; //not sure about this
for(i = 0; i < numGames; i++) {
if(openTimes[gameSeq[i]][1] < currentTime) return 0; // if arrive after specific game closes then not valid path
if(i == (numGames - 1)) return 1; // reached the last game and its open to play
if(currentTime < openTimes[gameSeq[i]][0]) currentTime = openTimes[gameSeq[i]][0]; // if get to game before opens change time to open time
currentTime += (playTime + travelTimes[gameSeq[i]][gameSeq[i + 1]]); // increment count time took to play this game and get to the next game
}
return 1;
}