Question A of 2014 BNU Invitational competition (constructor)

Source: Internet
Author: User

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;}
      
    
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.