A Matrix
Question: Give You A matrix based on the method given in the question and find the string that transforms the matrix.
Idea: Build a problem. You can draw several groups on the paper to find a path from top to bottom, and finally output these paths, which are the largest at the beginning and at the latest, in the process of searching, you only need to keep looking for a large one at the next layer, and it is an error if there is a non-incrementing line at the beginning.
Code:
#include
#include
#include
#include
using namespace std;const int N = 100005;int t, n, m, ans[N], an, flag, num[N];vector
g[N];void find(int i, int j) { if (i + 1 != m && g[i + 1][num[i + 1] - 1] > g[i][j]) { if (num[i + 1] - 1 < 0) return; ans[an++] = g[i + 1][num[i + 1] - 1]; find(i + 1, num[i + 1] - 1); num[i + 1]--; }}bool solve() { if (flag) return false; an = 0; int len = g[0].size(); for (int j = len - 1; j >= 0; j--) { ans[an++] = g[0][j]; find(0, j); } return an == n;}int main() { int cas = 0; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &m); flag = 0; for (int i = 0; i < m; i++) { g[i].clear(); scanf("%d", &num[i]); int tmp; for (int j = 0; j < num[i]; j++) { scanf("%d", &tmp); if (j && tmp < g[i][j - 1]) flag = 1; g[i].push_back(tmp); } } printf("Case #%d:", ++ cas); if (!solve()) printf(" No solution\n"); else { for (int i = n - 1; i >= 0; i--) printf(" %d", ans[i]); printf("\n"); } } return 0;}