比赛 收心赛 评测结果 WWWWWWWWWWMMMMMMMMMM
题目名称 矩阵游戏 最终得分 0
用户昵称 LikableP 运行时间 6.496 s
代码语言 C++ 内存使用 506.49 MiB
提交时间 2026-02-24 11:26:15
显示代码纯文本
#include <cstdio>
#include <cctype>

struct IO {
  static const int BUFSIZE = 1 << 20;
  char buf[BUFSIZE], *p1, *p2;
  char pbuf[BUFSIZE], *pp;
  
  IO() : p1(buf), p2(buf), pp(pbuf) {}
  ~IO() { fwrite(pbuf, 1, pp - pbuf, stdout); }
  
  char getchar() {
    if (p1 == p2) {
      p2 = (p1 = buf) + fread(buf, 1, BUFSIZE, stdin);
      if (p1 == p2) return EOF;
    }
    return *p1++;
  }
  
  void putchar(char ch) {
    if (pp - pbuf == BUFSIZE) fwrite(pbuf, 1, BUFSIZE, stdout), pp = pbuf;
    *pp++ = ch;
  }
  
  template <typename T> T read() {
    T res = 0, f = 1;
    char ch = getchar();
    for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1;
    for (; isdigit(ch); ch = getchar()) res = (res << 3) + (res << 1) + (ch ^ 48);
    return res * f;
  }
  
  template <typename T> void write(T x, char ed = '\n') {
    if (x < 0) x = -x, putchar('-');
    static int sta[sizeof(T) << 2], top = 0;
    do {
      sta[++top] = x % 10;
      x /= 10;
    } while (x);
    while (top) {
      putchar(sta[top--] ^ 48);
    }
    putchar(ed);
  }
} io;

#include <algorithm>
#include <vector>
#include <cmath>

const int MAXN = 110;
const double eps = 1e-5;

int GaussJordan(std::vector<std::vector<double>> &a) {
  int n = a.size() - 1;
  for (int i = 1; i <= n; ++i) {
    int row = i;
		for(int j = 1; j <= n; j++){
			if(abs(a[j][j]) > eps && j < i) continue;
			if(abs(a[j][i]) > abs(a[row][i])){
				row = j;
			}
		}
		std::swap(a[row], a[i]);
    
    if (fabs(a[i][i]) < eps) continue;
    
    for (int k = 1; k <= n; ++k) {
      if (i == k) continue;
      double delta = a[k][i] / a[i][i];
      for (int j = i; j <= n + 1; ++j) {
        a[k][j] -= delta * a[i][j];
      }
    }
  }
  
  for (int i = 1; i <= n; ++i) {
    if (!a[i][i]) continue;
    a[i][n + 1] /= a[i][i];
  }

  int res = 1;
  for (int i = 1; i <= n; ++i) {
    if (fabs(a[i][i]) < eps) {
      if (fabs(a[i][n + 1]) > eps) res = -1;
      if (fabs(a[i][n + 1]) < eps && res != -1) res = 0;
    }
  }
  return res;
}

void Work() {
  int n = io.read<int>(), m = io.read<int>();
  std::vector<std::vector<int>> b(n, std::vector<int>(m, 0));
  for (int i = 1; i <= n - 1; ++i) {
    for (int j = 1; j <= m - 1; ++j) {
      b[i][j] = io.read<int>();
    }
  }
  
  auto tr = [&](int x, int y) {
    return (x - 1) * m + y;
  };
  
  std::vector<std::vector<double>> a(n * m + 1, std::vector<double>(n * m + 2, 0.0));
  int equationCnt = 0;
  for (int i = 1; i <= n - 1; ++i) {
    for (int j = 1; j <= m - 1; ++j) {
      ++equationCnt;
      a[equationCnt][tr(i, j)] = 1;
      a[equationCnt][tr(i, j + 1)] = 1;
      a[equationCnt][tr(i + 1, j)] = 1;
      a[equationCnt][tr(i + 1, j + 1)] = 1;
      a[equationCnt][n * m + 1] = b[i][j];
    }
  }
  
  int res = GaussJordan(a);
  if (res == -1) {
    io.putchar('N'), io.putchar('O'), io.putchar('\n');
    return ;
  }
  
  for (int i = 1; i <= n * m; ++i) if (fabs(a[i][i]) < eps) {
    a[i][i] = a[i][n * m + 1] = 1.0;
  }
  GaussJordan(a);
  
  for (int i = 1; i <= n * m; ++i) if (a[i][n * m + 1] < 0 && fabs(a[i][n * m + 1]) > eps) {
    io.putchar('N'), io.putchar('O'), io.putchar('\n');
    return ;    
  }
  
  io.putchar('Y'), io.putchar('E'), io.putchar('S'), io.putchar('\n');
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      io.write<long long>(a[tr(i, j)][n * m + 1], " \n"[j == m]);
    }
  }
}

int main() {
  #ifdef LOCAL
    freopen("!input.in", "r", stdin);
    freopen("!output.out", "w", stdout);
  #else
    freopen("matrix.in", "r", stdin);
    freopen("matrix.out", "w", stdout);
  #endif
  
  int T = io.read<int>();
  while (T--) {
    Work();
  }
  return 0;
}