/*This problem is really a bit of a reference to the solution of some of the methods of judgement---------because the state compresses all the gems so that the states compress the f[i][j][s] to the binary every 10 means that the gem has no 1 indicates that the gem has a state whether the Gem collection state is present at this point (i,j) */#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespacestd;intt,n,m,k,f[ About][ About][ -],sx,sy,ex,ey,num;intxx[5]={0,0,0,1,-1};intyy[5]={0,1,-1,0,0};Chars[ About][ About];structpoint{intmx, my, step, sum; Point (intXxintYyintTtintss): mx (xx), my (yy), step (TT), SUM (ss) {};};structdoor{intxi, Yi;} door[ the];BOOLCheckints) { intCnt=0; for(intI=0; i<=4; i++) if((s>>i) &1==1) CNT++; return(cnt>=k);}intMain () {scanf ("%d",&T); while(t--) {memset (F,0,sizeof(f)); memset (s),0,sizeof(s)); Num=0; Queue<point>Q; intfalg=0; scanf ("%d%d%d",&n,&m,&k); for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) {cin>>S[i][j]; if(s[i][j]=='S') {S[i][j]='.'; SX=i; Sy=J; } if(s[i][j]=='E') {S[i][j]='.'; Ex=i; EY=J; } if(s[i][j]=='$') {num++; Door[num].xi=i; Door[num].yi=J; }} q.push (Point (Sx,sy,0,0)); f[sx][sy][0]=1;//Initial state exists while(!Q.empty ()) {Point TMMP=Q.front (); Q.pop (); intnx=tmmp.mx; intny=tmmp.my; intsu=tmmp.sum; intTime=Tmmp.step; if(Nx==ex&&ny==ey&&check (SU))//determine if the condition is met{printf ("%d\n", Tmmp.step); Falg=1;//multiple sets of data kill people. Return 0 at first. } if(FALG) Break; if(s[nx][ny]=='.')//the general situation directly around the point of the queue for(intI=1; i<=4; i++) { intox=nx+Xx[i]; intoy=ny+Yy[i]; if(ox>0&&ox<=n&&oy>0&&oy<=m&&f[ox][oy][su]==0&&s[ox][oy]!='#') {F[OX][OY][SU]=1; Q.push (Point (Ox,oy,time+1, SU)); } } if(s[nx][ny]=='$')//the portal will queue all the points around the portal. { for(intI=1; i<=num;i++) for(intj=1; j<=4; j + +) { intox=door[i].xi+Xx[j]; intoy=door[i].yi+Yy[j]; if(ox>0&&ox<=n&&oy>0&&oy<=m&&f[ox][oy][su]==0&&s[ox][oy]!='#') {F[OX][OY][SU]=1; Q.push (Point (Ox,oy,time+1, SU)); } } } if(s[nx][ny]>='0'&&s[nx][ny]<='4')//started collecting gems. { intsi=su| (1<< (s[nx][ny]-'0'));//To transform a new state for(intI=1; i<=4; i++) { intox=nx+Xx[i]; intoy=ny+Yy[i]; if(ox>0&&ox<=n&&oy>0&&oy<=m&&f[ox][oy][si]==0&&s[ox][oy]!='#') {F[ox][oy][si]=1; Q.push (Point (Ox,oy,time+1, si)); } } } } if(!falg) printf ("oop!\n"); } return 0; }
Noi 7221 Save the Princess (state compression +bfs)