1703 pressure Releaser

Source: Internet
Author: User
Description
There is a lot of pressure to be a TA, and it is too difficult to be scolded by the students who don't do it. More questions have been done to encourage the students to be scolded by the students who are sure to do exercises on the spot, the questions you have ever seen Will be scolded by all colleagues, and the code query will be scolded by those who refer to other people's code... recently, the two TAS were under too much psychological pressure, so they ran to the psychological counseling room and wanted to ask the doctor for some advice. The doctor quickly came up with something called a pressure Releaser, an M * n board. Each piece on the board represents a pressure value.

The Releaser is used like this:

Start from any piece in the leftmost column, you can take three pieces in the right column, that is, top right, right bottom, and bottom right. If the number of pawns in row 1st or Row M is used, we call this situation the sum of the pawns obtained by traversing is the total pressure value.

Naturally, we want the minimum stress value and the minimum Lexicographic Order of the path (the Lexicographic Order is in the data structure, and you may remember). Can you help the two TAS to use the Releaser?

Input

T group data (T <= 10)

Data guarantee for each group M <= 100 & n <= 100

Always within int range

Output

The first behavior path. Each number pi indicates that the part of the PI row in column I is removed.

As shown in the sample, 1st columns and 1st columns are removed.

Sample Input
1
5 2
5 1
6 2
4 3
5-1
9-3
Sample output
1 5
2
#include <iostream>using namespace std;int a[102][102];int p[102][102];void printPath(int m, int i, int n){    cout << p[m][i] << ' ';    if(i < n-1)        printPath(p[m][i], i+1, n);}int main(){    int t;    int m, n;    cin >> t;    int i, j;    while(t --)    {        cin >> m >> n;        for(i = 1; i <= m; i ++)            for(j = 1; j <= n; j ++)                cin >> a[i][j];        for(i = n-1; i >= 1; i --)            for(j = 1; j <= m; j ++)            {                int temp = j;                if(j == m)                {                    if(j-1 > 0 && a[j-1][i+1] <= a[temp][i+1])                        temp = j-1;                    if(a[1][i+1] <= a[temp][i+1])                        temp = 1;                }                else if(j == 1)                {                    if(j+1 < m && a[j+1][i+1] < a[temp][i+1])                        temp = j + 1;                    if(a[m][i+1] < a[temp][i+1])                        temp = m;                }                else                {                    if(a[j-1][i+1] <= a[temp][i+1])                        temp = j - 1;                    if(a[j+1][i+1] < a[temp][i+1])                        temp = j + 1;                }                p[j][i] = temp;                a[j][i] += a[temp][i+1];            }        int mi = a[1][1];        int mm = 1;        for(i = 2; i <= m; i ++)            if(mi > a[i][1])            {                mi = a[i][1];                mm = i;            }        if(n == 0 || m == 0)        {            cout << "0\n";            continue;        }        cout << mm << ' ';        if(n > 1)            printPath(mm, 1, n);        cout << '\n';        cout << mi << '\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.