C language · 3000 m ranking forecast

Source: Internet
Author: User

Algorithm increases the 3000-meter rank forecast time limit: 1.0s memory limit: 256.0MBThe question describes the 3000-metre long-distance run when the party happily forecasts the final rankings. Because they come from different classes, they don't necessarily know all the players, so they evaluate the strength of some of the athletes they know, that is, a prediction of the relative rankings of some of the athletes, and tell the poor remaining monitor. Because of boredom, so they went to the group to play DotA. After the game they asked the monitor for the final ranking, but the monitor did not remember, only remember which of them the prediction is correct, which people's prediction is wrong. They want to know what the ranking of the game might be. Input format the first line two integers n, m,n for the number of athletes, m for the number of onlookers party. Athlete number from 0 to n-1.
Next M-line, each behavior a spectator party's relative ranking forecast. The first number in each line C indicates the number of people he predicts, followed by a different number of C 0~n-1, indicating the relative rank of the athlete he predicts, and finally a number, 0 means the prediction is wrong and 1 is correct. Output format the first row one number k is the possible number of rankings.
The following K line, each line a 0~n-1 arrangement, for a certain possible ranking, adjacent to the number of spaces separated by a space. All rankings are sequentially output in dictionary order. Sample inputs Input Sample 1:
3 2
2 0 1 1
2 1 2 0

Input Sample 2:
3 2
2 0 1 1
2 2 1 0 Sample outputs Output Sample 1:
2
0 2 1
2 0 1

Output Sample 2:
1
0 1 2 data size and conventions 1<=n<=10, 2<=c<=n, 1<=m<=10, ensure that the data is legitimate, and that the number of answers may not exceed 20000. For a ranking sequence, a prediction is correct when and only if the relative order of the predicted rank is a subsequence of the rank sequence. A prediction is wrong when and only if this prediction is incorrect. Author comments: Seriously looked under DFS, search when the retrospective recursion summarized as follows:
1 voidDFS (current state) {2     if(the current state is a boundary state) {3 record or output4         return; 5     }  6      for(i=0; i<n;i++) {//traverse the solution tree all child nodes horizontally7 Modify the global variables to extend a sub-state; 8         if(sub-state satisfies the constraint) {9 DFS (sub-state); Ten         }   OneRestore global variables;//Backtracking Section A     }   -}

The subject code is as follows:

1 /*2 Data size and conventions: 1<=n<=10, 2<=c<=n, 1<=m<=103 */ 4#include <stdio.h>5#include <string.h>6 intN,m;//N table number of athletes; M table predicted data groups7 BOOLuse[ -];//whether the tag node is selected8 intstr[ -][ -]={0};9 intflag[ -];Ten intmap[20000][ the]; One intK//how many possible rankings of a table satisfy the forecast A intJudgedfs () { -     intx1=1, x2=1; -      for(intI=0; i<m;i++){ the         //table The group predicts that the data is right -         if(str[i][str[i][0]+1]==1&&x1) { -             intj=1;//start traversing from the second element of each set of data -             //&& to ensure traversal depth; X<n table does not predict the ranking of all athletes +              for(intx=0; j<=str[i][0] && x<n; X + +){ -                 if(str[i][j]==Flag[x]) { +J + +; A                 } at             } -             if(j<str[i][0]+1){//table This group data traversal completed -x1=0; -             } -}Else{//table The group predicts that the data is wrong -             intj=1; in              for(intx=0; j<=str[i][0] && x<n; X + +){ -                 if(str[i][j]==Flag[x]) { toJ + +; +                 } -             } the             if(j==str[i][0]+1){  *X2=0; $             }Panax Notoginseng         }  -         if(!x1 | |!x2)//iterate through a set of prediction data jumps out of the loop the              Break; +     } A     //are true return True, otherwise return false the     if(X1 &&x2) +         return 1; -     Else $         return 0; $ } - voidDfsintbegin) { -     if(Begin==n && Judgedfs ()) {//recursive exit: The last athlete has been searched the          for(intI=0; i<n;i++){ -Map[k][i]=flag[i];//record the athlete that will be currently traversedWuyi         } thek++;//Possible situation +1 -     } Wu     if(begin<n) {//criteria for performing a recursive search -          for(intI=0; i<n;i++){ About             if(Use[i]) {//If True, is not selected $Flag[begin]=i;//The current traverse position records the athlete's subscript -use[i]=false;//at this time select -DFS (begin+1);//recursive call, search for next athlete -use[i]=true;//returns the initial value, or backtracking A             } +         } the     } - } $  the intMain () { thescanf"%d%d",&n,&m); theGetChar ();//handle the carriage return here the      for(intI=0; i<m;i++) {//M-Group prediction data -         //table This group predicts the number of people to predict, the back of the loop to use inscanf"%d", &str[i][0]); the         //last column (that is, str[i][str[i][0]+1) table This group predicts whether it is correct the          for(intj=1; j<=str[i][0]+1; j + +){ Aboutscanf"%d",&str[i][j]); the         } the     } the     /* + marks whether the current position has been selected: false, No: true, and the initial value is true. -     */  thememset (use,true,sizeof(use));Bayik=0;//Set the initial value for possible situations theDfs0);//start searching from the first athlete the     //Formatting Output Results -printf"%d\n", k); -      for(intI=0; i<k;i++){ the          for(intj=0; j<n;j++){ theprintf"#df", Map[i][j]); the         } theprintf"\ n"); -     } the     return 0; the}

C language · 3000 m ranking forecast

Related Article

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.