2048
作者:
懒11111
,
2024-05-26 13:01:38
,
所有人可见
,
阅读 18
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;
/*
input:
3 6
2 1 2 3 2 4
1 14 2
1 6 4
2 10 2
0 3 2
2 17 2
3 14 4
output:
4
12
*/
public class Main {
public static void main(String[] args) throws IOException {
new Main().run();
}
int N = 10;
int[][] g = new int[N][N]; // 原
int[][] t = new int[N][N]; // 暂存状态
int n, m, res, score, r;
void run() throws IOException {
Scanner sc = new Scanner(System.in);
n = sc.nextInt(); m = sc.nextInt();
int x, y, z;
x = sc.nextInt(); y = sc.nextInt(); z = sc.nextInt();
g[x][y] = z;
x = sc.nextInt(); y = sc.nextInt(); z = sc.nextInt();
g[x][y] = z;
while (m -- > 0) {
int op = sc.nextInt();
int k = sc.nextInt();
int v = sc.nextInt();
int t;
if (op == 0) // 上
t = up();
else if (op == 1) // 下
t = down();
else if (op == 2) // 左
t = left();
else // 右
t = right();
if (t == -1)
break;
fill(1 + k % r, v);
res ++;
}
System.out.println(res);
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= n; j ++ )
score += g[i][j] == 2 ? 0 : g[i][j];
System.out.println(score);
sc.close();
}
int up() {
int cnt = 0;
t = new int[N][N];
for (int j = 1; j <= n; j ++ ) {
Deque<Integer> deque = new ArrayDeque<>();
for (int i = 1; i <= n; i ++ ) {
if (g[i][j] == 0) continue;
if (!deque.isEmpty() && g[i][j] == deque.peekLast()) {
deque.pollLast();
deque.addLast(g[i][j] * 2);
} else deque.addLast(g[i][j]);
}
cnt += deque.size();
for (int i = 1; i <= n && !deque.isEmpty(); i ++ ) {
t[i][j] = deque.pollFirst();
}
}
r = n * n - cnt;
if (check()) return -1;
copy();
return 0;
}
int down() {
int cnt = 0;
t = new int[N][N];
for (int j = 1; j <= n; j ++ ) {
Deque<Integer> deque = new ArrayDeque<>();
for (int i = n; i >= 1; i -- ) {
if (g[i][j] == 0) continue;
if (!deque.isEmpty() && g[i][j] == deque.peekLast()) {
deque.pollLast();
deque.addLast(g[i][j] * 2);
} else deque.addLast(g[i][j]);
}
cnt += deque.size();
for (int i = n; i > 0 && !deque.isEmpty(); i -- ) {
t[i][j] = deque.pollFirst();
}
}
r = n * n - cnt;
if (check()) return -1;
copy();
return 0;
}
int left() {
int cnt = 0;
t = new int[N][N];
for (int i = 1; i <= n; i ++ ) {
Deque<Integer> deque = new ArrayDeque<>();
for (int j = 1; j <= n; j ++ ) {
if (g[i][j] == 0) continue;
if (!deque.isEmpty() && g[i][j] == deque.peekLast()) {
deque.pollLast();
deque.addLast(g[i][j] * 2);
} else deque.add(g[i][j]);
}
cnt += deque.size();
for (int j = 1; j <= n && !deque.isEmpty(); j ++ ) {
t[i][j] = deque.pollFirst();
}
}
r = n * n - cnt;
if (check()) return -1;
copy();
return 0;
}
int right() {
int cnt = 0;
t = new int[N][N];
for (int i = 1; i <= n; i ++ ) {
Deque<Integer> deque = new ArrayDeque<>();
for (int j = n; j >= 1; j -- ) {
if (g[i][j] == 0) continue;
if (!deque.isEmpty() && g[i][j] == deque.peekLast()) {
deque.pollLast();
deque.addLast(g[i][j] * 2);
} else deque.add(g[i][j]);
}
cnt += deque.size();
for (int j = n; j >= 1 && !deque.isEmpty(); j -- ) {
t[i][j] = deque.pollFirst();
}
}
r = n * n - cnt;
if (check()) return -1;
copy();
return 0;
}
void fill(int k, int v) {
int cnt = 0;
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= n; j ++ ) {
if (g[i][j] == 0) cnt ++;
if (cnt == k) {
g[i][j] = v;
return;
}
}
}
boolean check() { // 是否结束
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= n; j ++ )
if (g[i][j] != t[i][j]) return false;
return true;
}
void copy() {
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= n; j ++ )
g[i][j] = t[i][j];
}
}