小p的2048
题目大意:2048游戏规则如下:balbalbala...但是小p为了降低难度,把规则改了一下,具体呢就是给定你操作的方向和新生成的2或者4的位置,问你最后结束的时候有效操作了几次和最高得分(每次合并的分数加起来)
我是2048的专业户...
所以做这个题只要稍微联系一个游戏就好了,然后就没了.(我代码玄学90pts,如果后续有学弟愿意深究这个题的话,不介意你们帮我D一下)
代码如下: 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using namespace std;
inline ll read()
{
ll s(0), w(1);
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
inline void put(ll x)
{
if (! x) putchar('0');
if (x < 0) putchar('-'), x = -x;
int num(0); char c[66];
while (x) c[++ num] = x % 10 + 48, x /= 10;
while (num) putchar(c[num --]);
return (void)(putchar('\n'));
}
const int N = 66;
int n, m, sx, sy, sv, tx, ty, tv;
int D, K, V, opt, res, SB1, SB2;
int a[N][N], b[N][N];
bool vis[N][N];
inline void yhm_Born()
{
int i, j, cnt(0), r(0);
for (i = 1; i <= n; ++ i) for (j = 1; j <= n; ++ j) if (! a[i][j]) ++ r;
int who = (1 + K % r);
for (i = 1; i <= n; ++ i) for (j = 1; j <= n; ++ j) if (! a[i][j])
{
++ cnt;
if (cnt == who)
{
a[i][j] = V;
SB1 = i, SB2 = j;
break;
}
}
return;
}
inline void yhm_up()
{
int i, j; memset(vis, 0, sizeof vis);
for (j = 1; j <= n; ++ j)
{
for (i = 2; i <= n; ++ i)
{
int cnt = i;
if (! a[i][j]) continue;
while (cnt > 1 && a[cnt - 1][j] == 0) -- cnt;//跳到空格
if (cnt == i);
else a[cnt][j] = a[i][j], a[i][j] = 0;
if (a[cnt - 1][j] == a[cnt][j] && ! vis[cnt - 1][j])
a[cnt - 1][j] *= 2, res += a[cnt - 1][j], a[cnt][j] = 0, vis[cnt - 1][j] = 1;
}
}
return;
}
inline void yhm_dn()
{
int i, j; memset(vis, 0, sizeof vis);
for (j = 1; j <= n; ++ j)
{
for (i = n - 1; i >= 1; -- i)
{
int cnt = i;
if (! a[i][j]) continue;
while (cnt < n && a[cnt + 1][j] == 0) ++ cnt;//跳到空格
if (cnt == i);
else a[cnt][j] = a[i][j], a[i][j] = 0;
if (a[cnt + 1][j] == a[cnt][j] && ! vis[cnt + 1][j])
a[cnt + 1][j] *= 2, res += a[cnt + 1][j], a[cnt][j] = 0, vis[cnt + 1][j] = 1;
}
}
return;
}
inline void yhm_lt()
{
int i, j; memset(vis, 0, sizeof vis);
for (i = 1; i <= n; ++ i)
{
for (j = 2; j <= n; ++ j)
{
int cnt = j;
if (! a[i][j]) continue;
while (cnt > 1 && a[i][cnt - 1] == 0) -- cnt;
if (cnt == j);
else a[i][cnt] = a[i][j], a[i][j] = 0;
if (a[i][cnt - 1] == a[i][cnt] && ! vis[i][cnt - 1])
a[i][cnt - 1] *= 2, res += a[i][cnt - 1], a[i][cnt] = 0, vis[i][cnt - 1] = 1;
}
}
return;
}
inline void yhm_rt()
{
int i, j; memset(vis, 0, sizeof vis);
for (i = 1; i <= n; ++ i)
{
for (j = n - 1; j >= 1; -- j)
{
int cnt = j;
if (! a[i][j]) continue;
while (cnt < n && a[i][cnt + 1] == 0) ++ cnt;
if (cnt == j);
else a[i][cnt] = a[i][j], a[i][j] = 0;
if (a[i][cnt + 1] == a[i][cnt] && ! vis[i][cnt + 1])
a[i][cnt + 1] *= 2, res += a[i][cnt + 1], a[i][cnt] = 0, vis[i][cnt + 1] = 1;
}
}
return;
}
inline void yhm_Func()
{
if (D == 0) return (void)(yhm_up());
if (D == 1) return (void)(yhm_dn());
if (D == 2) return (void)(yhm_lt());
if (D == 3) return (void)(yhm_rt());
return;
}
inline bool yhm_Check()
{
int i, j, pd(0);
for (i = 1; i <= n; ++ i) for (j = 1; j <= n; ++ j)
{
if (i == SB1 && j == SB2) continue;
if (a[i][j] == b[i][j]) ++ pd;
}
if (pd == n * n - 1) return true;
return false;
}
inline void yhm_chenge()
{
int i, j;
for (i = 1; i <= n; ++ i) for (j = 1; j <= n; ++ j) b[i][j] = a[i][j];
return;
}
signed main()
{
n = read(), m = read();
sx = read(), sy = read(), sv = read(), tx = read(), ty = read(), tv = read();
a[sx][sy] = sv, a[tx][ty] = tv;
while (m --)
{
D = read(), K = read(), V = read();
yhm_Func();
if (yhm_Check())
{
put(opt), put(res);
return 0;
}
yhm_Born();
++ opt;
yhm_chenge();
}
put(opt), put(res);
return 0;
}
后记:
这是继红心大战之后又一道模拟题
2048的原型就很好,自己的码力还是可以的,要兢兢业业的保持到noip!