Description
After you've cracked the puzzle, you know that kid will steal at least one gem within T minutes after the show starts and leave the pavilion. The entire pavilion is rectangular, divided into n*m areas, with unique entrances and exits (which cannot be accessed from the exits, nor from the entrances). A region can be moved directly to one of the adjacent four regions, and the quickest one minute. Imagine a kid entering a gem-free area to steal gems without time-consuming. Ask at least a few areas to block (you can block the area where gems are placed, but you cannot block entrances and exits) to ensure that kid cannot complete the task.
Input
The first line of input has an integer c, which indicates that there is a C set of test data. The first line of each set of test data has three integer n,m,t (2<=n,m<=8,t>0). The next n rows M is listed as the Pavilion Layout, which includes:
' S ': entrance
' E ': Export
' J ': The area where the gem is placed, appears at least once
‘.‘ : Blank Area
' # ': Wall
Output
For each set of test data, output at least the number of zones to block.
Sample Input
2 5 5 5SJJJJ.. # #J. JJJJ. J... Ej... 5 5 6SJJJJ.. # #J. JJJJ. J... Ej...
Sample Output
0 2 This topic is used with DFS, have to use BFS. DFS search to block the number of areas, BFS search kid can steal gems. It is easy to know that there are up to 4 blocked areas, as the maximum value can be set to 4 if the inlet or outlet is enclosed for up to 4 areas, and then searched.
#include <cstdio> #include <cstring> #include <queue>using namespace std;struct p{int x,y,t,su; int s1[90],s2[90];}; int N,m,sx,sy,ans,time;char map[10][10];int v[10][10][2];int yi[4][2]={{1,0},{0,1},{-1,0},{0,-1}};void dfs (int k) { queue<p>q; P F,r; int vis=-1,i,j; if (K>ans) return; F.X=SX; F.y=sy; f.t=0; f.su=0; memset (v,0,sizeof (v)); while (!q.empty ()) Q.pop (); V[sx][sy][0]=1; Q.push (f); while (!q.empty ()) {R=q.front (); Q.pop (); if (r.t>time) continue; if (map[r.x][r.y]== ' E ' &&r.su) {vis=r.t;break;} f.t=r.t+1; f.su=r.su; for (i=0;i<4;i++) {f.x=r.x+yi[i][0]; F.Y=R.Y+YI[I][1]; if (f.x>=0&&f.x<n&&f.y>=0&&f.y<m&&map[f.x][f.y]!= ' # ') { if (map[f.x][f.y]== ' J ') f.su=1; else f.su=r.su; if (v[f.x][f.y][f.su]) continue; V[f.x][f.y][f.su]=1; for (j=1;j<=r.t;j++) {f.s1[j]=r.s1[j]; F.S2[J]=R.S2[J]; } f.s1[f.t]=f.x; F.S2[F.T]=F.Y; Q.push (f); }}} if (Vis==-1) {if (ans>k) ans=k; return; } for (i=1;i<=r.t;i++) {char c=map[r.s1[i]][r.s2[i]]; if (c== ' S ' | | c== ' E ') continue; map[r.s1[i]][r.s2[i]]= ' # '; DFS (K+1); Map[r.s1[i]][r.s2[i]]=c; }}int Main () {int c,i,j; scanf ("%d", &c); while (c--) {scanf ("%d%d%d", &n,&m,&time); for (i=0;i<n;i++) for (j=0;j<m;j++) {scanf ("%c", &map[i][j]); if (map[i][j]== ' S ') {sx=i;sy=j;} } ans=4; DFS (0); printf ("%d\n", ans); } return 0;}
HDU 1983 Kaitou Kid-the Phantom Thief (2) BFS and DFS