This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve04.cc
97 lines (86 loc) · 1.67 KB
/
solve04.cc
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
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <vector>
std::vector<int> draw;
struct Board {
unsigned marks;
int numbers[5][5];
bool done;
};
std::vector<Board> boards;
void rd()
{
int c, i, j;
c = fgetc(stdin);
while (c!='\r' && c !='\n') {
if (c != ',') ungetc(c, stdin);
assert(scanf("%d", &i) > 0);
draw.push_back(i);
c = fgetc(stdin);
}
while (!feof(stdin)) {
Board b;
b.marks = 0;
b.done = false;
for(j = 0; j < 25; ++j) {
if (scanf("%d", &b.numbers[j/5][j%5]) <= 0)
break;
}
assert(j == 0 || j == 25);
if (j == 0) break;
boards.push_back(b);
}
}
long eval(Board const & b)
{
bool house = false;
for(int x = 0; x < 5; ++x) {
if (!((0x108421 << x) & ~b.marks) ||
!((0x1F << (5*x)) & ~b.marks)) {
house = true;
break;
}
}
if (!house) return -1;
unsigned long score = 0;
for(int y = 0; y < 5; ++y) {
for(int x = 0; x < 5; ++x) {
if (!(b.marks & (1 << (5*y+x))))
score += b.numbers[y][x];
}
}
return score;
}
void call(int i)
{
for (unsigned b = 0; b < boards.size(); ++b) {
if (boards[b].done) continue;
bool marked = false;
for(int y = 0; y < 5; ++y) {
for(int x = 0; x < 5; ++x) {
if (boards[b].numbers[y][x] == i) {
boards[b].marks |= (1 << (5*y+x));
marked = true;
}
}
}
if (marked) {
long score = eval(boards[b]);
if (score >= 0) {
score *= i;
printf("Board %d wins with score %d\n", b+1, score);
boards[b].done = true;
}
}
}
}
int main()
{
rd();
printf("There are %u boards and %u numbers\n", boards.size(), draw.size());
for(std::vector<int>::const_iterator ix = draw.begin(); ix != draw.end(); ix++) {
call(*ix);
}
return 0;
}