Title Description
The heavy rain should have lasted for several days, but it still didn't stop. Tuhao CCY has just returned from earning 1e yuan from other places, and knows that in the near future, except for his villa, other places will be flooded.
CCY's city can be represented by an N*M (N, M<=50) map with five symbols: "* X D S". Among them, "X" means stone, water and people can not pass through it. It means that plains, CCY and floods can pass through. "*" means the place where the flood started (there may be multiple places where the flood started). "D" means CCY's villa. "S" means the current position of CCY.
CCY can move to adjacent locations every minute, and floods will submerge adjacent unoccupied land (from submerged land) after CCY moves.
Seek CCY to return to the villa for the minimum time. If Cong Ge could not return home, he would probably be drowned, so he would worship the Golden God Rising RP to call the helicopter, so output "ORZ hzwer!!!".
Input and Output Format
Input format:3 3 D.*
… .S.
Output format:3
Input and Output Samples
3 3 D.* … ..S
ORZ hzwer!!!
3 6 D…*. .X.X.. ….S.
6
Simple extensive search problem, according to the requirements of the title can be simulated.
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 using namespace std; 7 int n,m; 8 int xx[7]={-1,+1,0,0}; 9 int yy[7]={0,0,-1,+1}; 10 struct peo 11 { 12 int juli;// 13 int x,y; 14 int step; 15 }now,nxt; 16 int map[201][201]; 17 int bgx,bgy,homex,homey; 18 int vis[201][201];// Where flooded, be careful map and vis Simultaneous judgement 19 int ans=438438; 20 int watercishu=1; 21 int flag=0; 22 int vis2[201][201]; 23 int calca(int xxx,int yyy) 24 { 25 return max(xxx,homex)-min(xxx,homex)+max(yyy,homey)-min(yyy,homey); 26 } 27 void init() 28 { 29 scanf("%d%d",&n,&m); 30 for(int i=1;i<=n;i++) 31 for(int j=1;j<=m;j++) 32 { 33 char p; 34 cin>>p; 35 if(p=='.') 36 map[i][j]=0;// It's all passable. 37 else if(p=='X') 38 map[i][j]=1;// Neither can pass 39 else if(p=='S') 40 {map[i][j]=2;//Man's Present Position 41 bgx=i;bgy=j;} 42 else if(p=='*') 43 map[i][j]=3,vis[i][j]=1;// Where the flood started 44 else if(p=='D') 45 { 46 map[i][j]=4;// home 47 homex=i; 48 homey=j; 49 } 50 51 } 52 } 53 void ex() 54 { 55 int flag=0; 56 for(int i=1;i<=n;i++) 57 { 58 for(int j=1;j<=m;j++) 59 { 60 if(vis[i][j]==watercishu) 61 { 62 for(int k=0;k<4;k++) 63 { 64 int wx=i+xx[k]; 65 int wy=j+yy[k]; 66 if(vis[wx][wy]==0&&map[wx][wy]!=1&&map[wx][wy]!=4&&wx>=1&&wx<=n&&wy>=1&&wy<=m) 67 { 68 vis[wx][wy]=vis[i][j]+1; 69 } 70 } 71 } 72 } 73 } 74 watercishu++; 75 } 76 void bfs() 77 { 78 queue<peo>q; 79 now.x=bgx;now.y=bgy;now.step=0;now.juli=calca(bgx,bgy); 80 q.push(now); 81 int last=0;// Record the number of steps taken during the last flood expansion 82 while(q.size()!=0) 83 { 84 peo p=q.front(); 85 if(vis[p.x][p.y]!=0) 86 { 87 q.pop(); 88 continue; 89 } 90 if(p.juli==0) 91 { 92 printf("%d",p.step); 93 flag=1; 94 return ; 95 } 96 97 q.pop(); 98 if(p.step>last) 99 { 100 ex();// Flood propagation 101 last=p.step; 102 } 103 if(vis[p.x][p.y]!=0) 104 { 105 continue; 106 } 107 for(int i=0;i<4;i++) 108 { 109 int wx=p.x+xx[i]; 110 int wy=p.y+yy[i]; 111 if(vis2[wx][wy]==0&&vis[wx][wy]==0&&map[wx][wy]!=1&&wx>=1&&wx<=n&&wy>=1&&wy<=m) 112 { 113 vis2[wx][wy]=1; 114 nxt.x=wx; 115 nxt.y=wy; 116 nxt.step=p.step+1; 117 nxt.juli=calca(wx,wy); 118 q.push(nxt); 119 } 120 } 121 122 } 123 } 124 int main() 125 { 126 /// freopen("sliker.in","r",stdin); 127 // freopen("sliker.out","w",stdout); 128 init(); 129 bfs(); 130 if(flag==0) 131 printf("ORZ hzwer!!!"); 132 return 0; 133 }