-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpour_water.c
141 lines (118 loc) · 4.87 KB
/
pour_water.c
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <stdio.h>
#include <stdbool.h>
#define MAX_CAPACITY 3
int stt = 0;
typedef struct {
int state[MAX_CAPACITY];
} State;
bool is_target_state(State state, int target, int vase) {
if (state.state[vase] == target) {
return true;
}
return false;
}
bool is_valid_state(State state) {
for (int i = 0; i < MAX_CAPACITY; i++) {
if (state.state[i] < 0 || state.state[i] > 10) {
return false;
}
}
return true;
}
bool is_duplicate_state(State state, State* visited_states, int num_visited_states) {
for (int i = 0; i < num_visited_states; i++) {
bool is_same = true;
for (int j = 0; j < MAX_CAPACITY; j++) {
if (state.state[j] != visited_states[i].state[j]) {
is_same = false;
break;
}
}
if (is_same) {
return true;
}
}
return false;
}
void print_solution(State* visited_states, int num_visited_states) {
printf("digraph G {\n");
for (int i = 0; i < num_visited_states - 1; i++) {
printf("\"(%d, %d, %d)\" -> ", visited_states[i].state[0], visited_states[i].state[1], visited_states[i].state[2]);
}
printf("\"(%d, %d, %d)\"", visited_states[num_visited_states - 1].state[0], visited_states[num_visited_states - 1].state[1], visited_states[num_visited_states - 1].state[2]);
printf("\n}");
}
void pour_water_dfs(State state, State* visited_states, int num_visited_states, int target, int vase) {
if (is_duplicate_state(state, visited_states, num_visited_states) || !is_valid_state(state)) {
return;
}
visited_states[num_visited_states] = state;
num_visited_states++;
if (is_target_state(state, target, vase) && stt == 0) {
print_solution(visited_states, num_visited_states);
stt = 1;
return;
}
State new_state;
// Pour water from jug 7l to jug 4l
if (state.state[1] > 0 && state.state[0] < 10) {
int pour_amount = (state.state[1] > 10 - state.state[0]) ? (10 - state.state[0]) : state.state[1];
new_state.state[0] = state.state[0] + pour_amount;
new_state.state[1] = state.state[1] - pour_amount;
new_state.state[2] = state.state[2];
pour_water_dfs(new_state, visited_states, num_visited_states, target, vase);
}
// Pour water from jug 7l to jug 4l
if (state.state[1] > 0 && state.state[2] < 4) {
int pour_amount = (state.state[1] > 4 - state.state[2]) ? (4 - state.state[2]) : state.state[1];
new_state.state[0] = state.state[0];
new_state.state[1] = state.state[1] - pour_amount;
new_state.state[2] = state.state[2] + pour_amount;
pour_water_dfs(new_state, visited_states, num_visited_states, target, vase);
}
// Pour water from jug 10l to jug 7l
if (state.state[0] > 0 && state.state[1] < 7) {
int pour_amount = (state.state[0] > 7 - state.state[1]) ? (7 - state.state[1]) : state.state[0];
new_state.state[0] = state.state[0] - pour_amount;
new_state.state[1] = state.state[1] + pour_amount;
new_state.state[2] = state.state[2];
pour_water_dfs(new_state, visited_states, num_visited_states, target, vase);
}
// Pour water from jug 10l to jug 4l
if (state.state[0] > 0 && state.state[2] < 4) {
int pour_amount = (state.state[0] > 4 - state.state[2]) ? (4 - state.state[2]) : state.state[0];
new_state.state[0] = state.state[0] - pour_amount;
new_state.state[1] = state.state[1];
new_state.state[2] = state.state[2] + pour_amount;
pour_water_dfs(new_state, visited_states, num_visited_states, target, vase);
}
// Pour water from jug 4l to jug 7l
if (state.state[2] > 0 && state.state[1] < 7) {
int pour_amount = (state.state[2] > 7 - state.state[1]) ? (7 - state.state[1]) : state.state[2];
new_state.state[0] = state.state[0];
new_state.state[1] = state.state[1] + pour_amount;
new_state.state[2] = state.state[2] - pour_amount;
pour_water_dfs(new_state, visited_states, num_visited_states, target, vase);
}
// Pour water from jug 4l to jug 10l
if (state.state[2] > 0 && state.state[0] < 10) {
int pour_amount = (state.state[2] > 10 - state.state[0]) ? (10 - state.state[0]) : state.state[2];
new_state.state[0] = state.state[0] + pour_amount;
new_state.state[1] = state.state[1];
new_state.state[2] = state.state[2] - pour_amount;
pour_water_dfs(new_state, visited_states, num_visited_states, target, vase);
}
}
int main() {
int target = 2;
State initial_state = {0, 7, 4};
State visited_states[100];
int num_visited_states = 0;
int vase;
scanf("%d", &vase);
//1 la binh 4l chua 2l nuoc
//2 la binh 7l chua 2l nuoc
printf("\n");
pour_water_dfs(initial_state, visited_states, num_visited_states, target, vase);
return 0;
}