Question:
From 0 ~ Room n-1 numbers arrive at room 0 from a starting point, close every time they pass through a door, and ask if they can pass all the doors and reach room 0.
I think the input and output formats of this question are my first time, so I have been reading sscanf for a long time.
Each row corresponds to the room that the current door can reach. If there are duplicates below, there will be blank lines, empty lines here, and numbers in the string one by one should be considered
There are only two successful cases.
Depart from room 0 and go through an Euler loop to 0
Starting from another room, an Euler's path reaches the base node of endpoints.
Code:
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 using namespace STD; 5 Int door [22]; 6 int Readline (char * s) {7 int L; 8 for (L = 0; (s [l] = getchar ())! = '\ N' & S [l]! = EOF; l ++); 9 s [l] = 0; 10 return l; 11} 12 INT main () 13 {14 char Buf [128]; 15 int m, n; 16 while (Readline (BUF) {17 if (BUF [0] = 's') {18 sscanf (BUF, "% * S % d ", & M, & N); 19 memset (door, 0, sizeof (door); 20 21 int numofdoor = 0; // record the number of all doors, for the final result output, the total number of closed doors 22 for (INT I = 0; I <n; I ++) {23 Readline (BUF); 24 int K = 0, j; // read the data in the string pointer position 25 while (sscanf (BUF + k, "% d", & J) = 1) {26 door [I] ++, Door [J] ++; 27 numofdoor ++; 28 while (BUF [k] & Buf [K] = '') K ++; 29 While (BUF [k] & Buf [k]! = '') K ++; 30} 31} 32 Readline (BUF); // read end33 int odd = 0, even = 0; 34 for (INT I = 0; I <n; I ++) {35 if (Door [I] % 2! = 0) odd ++; 36 else even ++; 37} 38 If (odd = 0 & M = 0) printf ("Yes % d \ n ", numofdoor); 39 else if (odd = 2 & door [m] % 2 = 1 & door [0] % 2 = 1 & M! = 0) printf ("Yes % d \ n", numofdoor); 40 else puts ("no"); 41} 42 else if (! Strcmp (BUF, "endofinput") break; 43} 44 return 0; 45}
The most basic Euler Loop problem in poj 1300