Skip to content

Commit 389ec1e

Browse files
author
Andrey Marchenko
committed
Added old solutions
1 parent cb6ae81 commit 389ec1e

File tree

1,824 files changed

+407998
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,824 files changed

+407998
-0
lines changed

ACM_Problems/Beijing 2007/C/C.suo

15.5 KB
Binary file not shown.

ACM_Problems/Beijing 2007/C/rez.cpp

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
#include <iostream>
2+
#include <cstdio>
3+
#include <string>
4+
#include <vector>
5+
#include <cmath>
6+
#include <queue>
7+
#include <stack>
8+
#include <algorithm>
9+
#include <map>
10+
#include <set>
11+
using namespace std;
12+
13+
int Q[256], R[256], D[5][5];
14+
15+
struct grid{
16+
char A[5][5];
17+
int score;
18+
grid(){
19+
score=0;
20+
for (int i=0; i<5; ++i)
21+
for (int j=0; j<5; ++j)
22+
A[i][j]=' ';
23+
}
24+
int hash(){
25+
int cur=0;
26+
for (int i=1; i<=3; ++i){
27+
for (int j=1; j<=3; ++j){
28+
cur+=Q[A[i][j]]*D[i][j];
29+
}
30+
}
31+
return cur;
32+
}
33+
bool operator < (const grid &t) const{
34+
for (int i=1; i<=3; ++i){
35+
for (int j=1; j<=3; ++j){
36+
if (A[i][j]!=t.A[i][j]){
37+
return A[i][j]<t.A[i][j];
38+
}
39+
}
40+
}
41+
return false;
42+
}
43+
};
44+
45+
queue <grid> q1;
46+
queue <int> q2;
47+
int dx[]={1,-1,0,0};
48+
int dy[]={0, 0,1,-1};
49+
int w1, w2, w3, w4, w;
50+
int ans, cas=0;
51+
bool s[2000000];
52+
int cur_hash;
53+
54+
int main (void)
55+
{
56+
// freopen ("input.txt", "r", stdin);
57+
// freopen ("output.txt", "w", stdout);
58+
Q[' ']=0;
59+
Q['b']=1;
60+
Q['r']=2;
61+
Q['g']=3;
62+
Q['y']=4;
63+
int deg=1;
64+
for (int i=1; i<=3; ++i){
65+
for (int j=1; j<=3; ++j){
66+
D[i][j]=deg;
67+
deg*=5;
68+
}
69+
}
70+
71+
while (true){
72+
++cas;
73+
memset(s, 0, sizeof(s));
74+
scanf ("%d%d%d%d%d", &w1, &w2, &w3, &w4, &w);
75+
if (!w1)
76+
break;
77+
ans=-1;
78+
R[' ']=0;
79+
R['b']=w1;
80+
R['r']=w2;
81+
R['g']=w3;
82+
R['y']=w4;
83+
84+
q1.push(grid());
85+
q2.push(0);
86+
87+
while (!q1.empty()){
88+
grid u=q1.front();
89+
q1.pop();
90+
int v=q2.front();
91+
q2.pop();
92+
for (int i=1; i<=3; ++i){
93+
for (int j=1; j<=3; ++j){
94+
grid tmp=u;
95+
tmp.A[i][j]=' ';
96+
tmp.score=u.score-R[u.A[i][j]];
97+
cur_hash=tmp.hash();
98+
if (!s[cur_hash]){
99+
s[cur_hash]=true;
100+
q1.push(tmp);
101+
q2.push(v+1);
102+
}
103+
tmp=u;
104+
tmp.A[i][j]='b';
105+
tmp.score=u.score-R[u.A[i][j]]+R['b'];
106+
cur_hash=tmp.hash();
107+
if (!s[cur_hash]){
108+
s[cur_hash]=true;
109+
q1.push(tmp);
110+
q2.push(v+1);
111+
}
112+
tmp=u;
113+
int bl,red,gr;
114+
bl=red=gr=0;
115+
for (int k=0; k<4; ++k){
116+
int x=i+dx[k];
117+
int y=j+dy[k];
118+
switch(u.A[x][y]){
119+
case 'b': bl++;
120+
break;
121+
case 'r': red++;
122+
break;
123+
case 'g': gr++;
124+
break;
125+
}
126+
}
127+
if (bl){
128+
tmp.A[i][j]='r';
129+
tmp.score=u.score-R[u.A[i][j]]+R['r'];
130+
cur_hash=tmp.hash();
131+
if (!s[cur_hash]){
132+
s[cur_hash]=true;
133+
q1.push(tmp);
134+
q2.push(v+1);
135+
}
136+
}
137+
tmp=u;
138+
if (bl && red){
139+
tmp.A[i][j]='g';
140+
tmp.score=u.score-R[u.A[i][j]]+R['g'];
141+
cur_hash=tmp.hash();
142+
if (!s[cur_hash]){
143+
s[cur_hash]=true;
144+
q1.push(tmp);
145+
q2.push(v+1);
146+
}
147+
}
148+
tmp=u;
149+
if (bl && red && gr){
150+
tmp.A[i][j]='y';
151+
tmp.score=u.score-R[u.A[i][j]]+R['y'];
152+
cur_hash=tmp.hash();
153+
if (!s[cur_hash]){
154+
s[cur_hash]=true;
155+
q1.push(tmp);
156+
q2.push(v+1);
157+
}
158+
}
159+
}
160+
}
161+
if (u.score>=w){
162+
ans=v;
163+
while (!q1.empty()){
164+
q1.pop();
165+
q2.pop();
166+
}
167+
}
168+
}
169+
if (ans==-1){
170+
cout << "Case " << cas << ": " << "Impossible" << endl;
171+
}
172+
else
173+
cout << "Case " << cas << ": " << ans << endl;
174+
}
175+
return 0;
176+
}

ACM_Problems/Beijing 2007/C/rez2.cpp

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
#include <iostream>
2+
#include <cstdio>
3+
#include <string>
4+
#include <vector>
5+
#include <cmath>
6+
#include <queue>
7+
#include <stack>
8+
#include <algorithm>
9+
#include <map>
10+
#include <set>
11+
using namespace std;
12+
13+
int Q[256], R[256], D[3][3];
14+
int cnt=0;
15+
16+
struct grid{
17+
char A[3][3];
18+
int score;
19+
grid(){
20+
score=0;
21+
for (int i=0; i<3; ++i)
22+
for (int j=0; j<3; ++j)
23+
A[i][j]=' ';
24+
}
25+
int hash(){
26+
int cur=0;
27+
for (int i=0; i<3; ++i){
28+
for (int j=0; j<3; ++j){
29+
cur+=Q[A[i][j]]*D[i][j];
30+
}
31+
}
32+
return cur;
33+
}
34+
char GetCell(int &x, int &y){
35+
if (x<0 || y<0 || x>2 || y>2)
36+
return ' ';
37+
return A[x][y];
38+
}
39+
bool operator < (const grid &t) const{
40+
for (int i=0; i<3; ++i){
41+
for (int j=0; j<3; ++j){
42+
if (A[i][j]!=t.A[i][j]){
43+
return A[i][j]<t.A[i][j];
44+
}
45+
}
46+
}
47+
return false;
48+
}
49+
};
50+
51+
//queue <grid> q1;
52+
//queue <int> q2;
53+
int dx[]={1,-1,0,0};
54+
int dy[]={0, 0,1,-1};
55+
int w1, w2, w3, w4, w;
56+
int ans, cas=0;
57+
bool s[2000000];
58+
grid q1[130000];
59+
int q2[130000];
60+
int head, tail;
61+
int cur_hash;
62+
63+
void push (const int& it, const grid& gt){
64+
q1[tail]=gt;
65+
q2[tail++]=it;
66+
if (tail==130000)
67+
tail=0;
68+
}
69+
70+
int main (void)
71+
{
72+
freopen ("input.txt", "r", stdin);
73+
freopen ("output.txt", "w", stdout);
74+
Q[' ']=0;
75+
Q['b']=1;
76+
Q['r']=2;
77+
Q['g']=3;
78+
Q['y']=4;
79+
int deg=1;
80+
for (int i=0; i<3; ++i){
81+
for (int j=0; j<3; ++j){
82+
D[i][j]=deg;
83+
deg*=5;
84+
}
85+
}
86+
87+
int maxx=0;
88+
while (true){
89+
++cas;
90+
head=tail=0;
91+
memset(s, 0, sizeof(s));
92+
scanf ("%d%d%d%d%d", &w1, &w2, &w3, &w4, &w);
93+
if (!w1)
94+
break;
95+
ans=-1;
96+
R[' ']=0;
97+
R['b']=w1;
98+
R['r']=w2;
99+
R['g']=w3;
100+
R['y']=w4;
101+
102+
push(0, grid());
103+
104+
while (head!=tail){
105+
grid u=q1[head];
106+
int v=q2[head++];
107+
if (head==130000)
108+
head=0;
109+
for (int i=0; i<3; ++i){
110+
for (int j=0; j<3; ++j){
111+
grid tmp=u;
112+
tmp.A[i][j]=' ';
113+
tmp.score=u.score-R[u.A[i][j]];
114+
cur_hash=tmp.hash();
115+
if (!s[cur_hash]){
116+
s[cur_hash]=true;
117+
push(v+1, tmp);
118+
}
119+
tmp=u;
120+
tmp.A[i][j]='b';
121+
tmp.score=u.score-R[u.A[i][j]]+R['b'];
122+
cur_hash=tmp.hash();
123+
if (!s[cur_hash]){
124+
s[cur_hash]=true;
125+
push(v+1, tmp);
126+
}
127+
tmp=u;
128+
int bl,red,gr;
129+
bl=red=gr=0;
130+
for (int k=0; k<4; ++k){
131+
int x=i+dx[k];
132+
int y=j+dy[k];
133+
switch(u.GetCell(x, y)){
134+
case 'b': bl++;
135+
break;
136+
case 'r': red++;
137+
break;
138+
case 'g': gr++;
139+
break;
140+
}
141+
}
142+
if (bl){
143+
tmp.A[i][j]='r';
144+
tmp.score=u.score-R[u.A[i][j]]+R['r'];
145+
cur_hash=tmp.hash();
146+
if (!s[cur_hash]){
147+
s[cur_hash]=true;
148+
push(v+1, tmp);
149+
}
150+
}
151+
tmp=u;
152+
if (bl && red){
153+
tmp.A[i][j]='g';
154+
tmp.score=u.score-R[u.A[i][j]]+R['g'];
155+
cur_hash=tmp.hash();
156+
if (!s[cur_hash]){
157+
s[cur_hash]=true;
158+
push(v+1, tmp);
159+
}
160+
}
161+
tmp=u;
162+
if (bl && red && gr){
163+
tmp.A[i][j]='y';
164+
tmp.score=u.score-R[u.A[i][j]]+R['y'];
165+
cur_hash=tmp.hash();
166+
if (!s[cur_hash]){
167+
s[cur_hash]=true;
168+
push(v+1, tmp);
169+
}
170+
}
171+
}
172+
}
173+
if (u.score>=w){
174+
ans=v;
175+
head=tail;
176+
}
177+
}
178+
if (ans==-1){
179+
cout << "Case " << cas << ": " << "Impossible" << endl;
180+
}
181+
else
182+
cout << "Case " << cas << ": " << ans << endl;
183+
}
184+
185+
return 0;
186+
}

0 commit comments

Comments
 (0)