Nyoj-110-Swordsman Duel _ Dynamic Planning

Source: Internet
Author: User

Describe
A duel took place at the time when Louis 13 and Cardinal Richelieu were in power. n individuals stand in a circle and draw lots. The man who smokes fights with the man on his right, and the negative is out of the circle. The final outcome of this duel depends crucially on the order of the duel. There is no information on who can win in any two duels, but "a won B" is not a transitive relationship. For example, A is stronger than B, B is stronger than C, and C is stronger than a. If A and B duel first, C will eventually win, but if the B and C duel is in the preceding, then the last a will win. Obviously, the first duel among the three of them had a direct impact on the final outcome.

Suppose now n individuals surround themselves in a circle, numbered 1~n sequentially. A n-1 duel was carried out altogether. In the first game, one of them (set I) and the person on his right (i.e. i+1 number, if i=n, the right person is number 1th). The negative is eliminated from the circle and the person next to him complements his position. The strong or weak relationship between known n individuals (i.e., the winning relationship between any two people). If there is a lottery to enable the individual to win, then we say that the K-man is likely to prevail, our task is based on the strength of N individuals to determine the number of potential winners.

Input
The first line is an integer N (1<=n<=20) that represents the number of groups to test the data.
The second line is an integer n that represents the total number of duels. (2<=n<=500)
The next n row is a n-row n-column matrix, and the column I in the matrix, in the case of paragraph J, if 1 means that the person I personally will win with the J individual duel, 0 means that the person I personally will fail in the duel with the J individual.
Output
For each set of test data, the output may be the number of winners, each set of output occupies one row
Sample input
1
3
0 1 0
0 0 1
1 0 0
Sample output
3

This question is somewhat similar to the Freudian algorithm, the loop into the chain, through the DP implementation, VIC[I][J] said that the individual can be with the first J individuals exist PK opportunities, when the i=j, that is, if you can and their own PK, if true, it means that this ring can last only the first person, So he does not have the next PK object, can only and his PK, otherwise, that this person can not and their PK, that is to say, will not appear last only the first person's situation.

The specific code implementation is as follows:

#include <stdio.h> #include <string.h> int vic[501][501];  VIC[I][J] The first and the J have the opportunity PK int rel[501][501];
    The winning and losing relationship between the swordsman//Initialize void init (int n) {int i, J;
        for (i = 0; i < n; i++) {for (j = 0; J < N; j +) {scanf ("%d", Rel[i] + j);
    } memset (Vic, 0, sizeof (INT) * 501 * 501);
    for (i = 0; i < n; i++)//initialization can only determine that the adjacent two people are capable of PK {vic[i][(i + 1)% n] = 1;
} return;
    int solve (int n) {int I, J, start, end; for (i = 1; i < n; i++)//The middle is separated by I person {for (start = 0; start < n; start++) {end = (STA
            RT + i + 1)% n;
            if (Vic[start][end])//When the two can be confirmed to PK {continue; for (j = (start + 1)% n; j!= End; J + +, J%= N) {if (vic[start][j) &&
                Vic[j][end] && (Rel[start][j] | | rel[end][j])//When start can and jpk,j can and ENDPK, and at least one side can be larger than J to achieve start and ENDPK
 {                   Vic[start][end] = 1;
                Break
    int cnt = 0;
        for (i = 0; i < n; i++) {if (Vic[i][i])//1 indicates I can win {cnt++;
} return CNT;
    int main (int argc, const char * argv[]) {int n, n;

    scanf ("%d", &n);
        while (n--) {scanf ("%d", &n);
        Init (n);
    printf ("%d\n", Solve (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.