Description
Someone has a set of toys and wants to name the toys. First he chooses any one of the four letters of wing as the basic name of the toy. Then he will, according to his preference, use any of the letters in the name "WING" to replace any of the two letters, so that his name can be extended very long. Now, he would like to ask you to guess a very long name, which may have been initially distorted by which letters.
Input
The first line is four integers w, I, N, G. Indicates that each letter can be replaced by a few two letters. The next W line, two letters per line, indicates that W can be substituted with these two letters. Next I line, two letters per line, indicating I can be replaced with these two letters. The next n lines, two letters per line, indicate that n can be substituted with these two letters. The next G line, two letters per line, indicates that G can be substituted with these two letters. The last line is a string that does not exceed Len in length. denotes the name of the toy.
Output
A line of strings, which may be distorted by the name of the letter. (output in the order of wing) if the given name cannot be distorted by any one letter, then output "The name is wrong!"
Sample Input1 1 1 1
Ii
Ww
Ww
IG
IIIISample OutputinchHINT
W can become II so IIII can be shrunk into WW in can become WW so WW can be shrunk to I or n so the final answer should be output in the order of "WING"
[Data Range]
100% data satisfies len<=200,w, I, N, g<=16
Source
Breadth-First search for flood questions is not explained.
1#include <algorithm>2#include <queue>3#include <cstdio>4#include <cstdlib>5 using namespacestd;6 7 intst,aim,xx[4] = {0,1,0,-1},yy[4] = {1,0,-1,0};BOOLexist[1<< -];8 9Inline pair<int,int> loc (intID)Ten { One intRx = (id+3)/4, ry = id-rx*4+4; A returnMake_pair (rx,ry); - } - theInlineintOrdintXintY) {return(X-1)*4+y;} - -InlineBOOLOkayintXinty) - { + if(X <1)return false; - if(X >4)return false; + if(Y <1)return false; A if(Y >4)return false; at return true; - } - -InlinevoidWork () - { -Queue < Pair <int,int> >team; inTeam.push (Make_pair (St,0)); EXIST[ST] =true; -Pair <int,int> now,temp;intSTA; to while(!team.empty ()) + { -now =Team.front (); Team.pop (); the if(Now.first = =aim) * { $printf"%d", Now.second);Panax Notoginseng return; - } the for(inti =1; I <= -;++i) + { A if(Now.first & (1<< (I-1))) the { +temp =Loc (i); - intNX = Temp.first,ny =Temp.second; $ for(intj =0; J <4;++j) $ { - intpx = Nx+xx[j],py = ny+Yy[j]; - if(Okay (px,py) &&! (now.first& (1<< (Ord (px,py)-1)))) the { -STA = Now.first; STA ^=1<< (I-1); STA |= (1<< (Ord (px,py)-1));Wuyi if(!Exist[sta]) theTeam.push (Make_pair (sta,now.second+1)), Exist[sta] =true; - } Wu } - } About } $ } - } - -Inlineint Get() A { + intRET =0, A; the for(inti =0; I <4; ++i) scanf ("%1d", &a), ret |= a<<i; - returnret; $ } the the intMain () the { theFreopen ("1054.in","R", stdin); -Freopen ("1054.out","W", stdout); in intA; the for(inti =1; I <=4;++i) the { AboutA =Get(); theSt |= A << ((I-1)*4); the } the for(inti =1; I <=4;++i) + { -A =Get(); theAim |= a << ((I-1)*4);Bayi } the Work (); the return 0; -}View Code
Bzoj 1054 Mobile Toys