G-EscapeTime
limit:MS
Memory Limit:32768KB
64bit IO Format:%i64d &%i64u SubmitStatusPracticeFzu 2196
Description
Xiao Ming into the underground maze to find treasures, found the treasure but there was an earthquake, the maze produced magma everywhere, Xiao Ming hurriedly fled to the exit. If you leave the treasure, Xiaoming can quickly leave the maze, but Xiao Ming does not want to easily give up his hard earned. So he hurried in contact with the programmer's friend (of course, by phone) and told you what he was facing, hoping you could tell him if he could succeed in escaping with the treasure.
Input
There are multiple sets of test data.
The first line of each set of test data is an integer t, representing the number of examples to be followed. (0<=t<=10)
Next is the T-group example.
The first line of each set of examples is two integers n and M. Represents the size of the maze with N rows M column (0<=n,m<=1000).
Next is a description of the maze of n*m.
s represents the location of Xiaoming.
E for export, only one export.
. Represents a place where you can walk.
! Represents the place where magma is produced. (This place will have multiple, with a number less than or equal to 10000)
#代表迷宫中的墙, it can not only stop Xiao Ming forward can also block the spread of magma.
Xiao Ming carrier treasure can only move around one cell per second, xiaoming can not touch the magma (Xiao Ming can not and magma in the same lattice).
Magma spreads a grid every second to places that are not walls.
After Xiao Ming first move, the magma will spread to the corresponding lattice.
Xiao Ming can move to the exit, then Xiao Ming escaped smoothly.
Output
Each set of test data outputs has only one line of "Yes" or "No". "Yes" means Xiao Ming can escape successfully. Otherwise output "No".
Sample Input
35 5....! s....#....! # ... #E ... 2 2s.! E2 2se!.
Sample Output
YesnoyesMagma spreads a grid every second to places that are not walls. This sentence is still very important, in case the next lattice reached the end, even if the magma spread over the success, if the ordinary lattice can not be so, this did not notice, WA a few hair.
Code:
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include < algorithm> #include <string> #include <cmath> #include <queue> #include <vector> #include <map> #include <set> #define INF 0x3f3f3f3f#define Mem (A, B) memset (A,b,sizeof (a)) using namespace Std;const int maxd=1000+5; #define Lson l,m,rt<<1#define Rson m+1,r,rt<<1 | 1typedef long ll;typedef pair<int,int> pii;//---------------------------int dx[]= {0,0,1,-1};int dy[]= {1,- 1,0,0};typedef struct node{int x,y,t; node (int x_ = 0, int y_ = 0, int t_ = 0) {x = X_; y = y_; t = t_; }};int n,m,stx,sty,enx,eny;int vis[maxd][maxd],mz[maxd][maxd];queue<node> f;bool ok (int x,int y) {if (x<0 | | y& lt;0 | | X>=n | | Y>=M) return false; return true;} void Init () {cin>>n>>m; for (int i=0, i<=n; ++i) for (int j=0; j<=m; ++j) mz[i][j]=-1; MEM (vis,0); for (int i=0; i<N ++i) for (int j=0; j<m; ++j) {char ch; cin>>ch; if (ch== '. ') mz[i][j]=0; else if (ch== '! ') {mz[i][j]=0; Node tmp (i,j,0); F.push (TMP), vis[i][j]=1; } else if (ch== ' S ') stx=i,sty=j,mz[i][j]=0; else if (ch== ' E ') enx=i,eny=j,mz[i][j]=0; } while (!f.empty ()) {node Now=f.front (); F.pop (); for (int i=0; i<4; ++i) {int xx=now.x+dx[i]; int yy=now.y+dy[i]; int tt=now.t+1; if (OK (xx,yy) && mz[xx][yy]!=-1 && vis[xx][yy]==0) {if (mz[xx][yy]==0 | | mz[xx][y Y]> tt) {mz[xx][yy]=tt; Vis[xx][yy]=1; Node tmp (XX,YY,TT); F.push (TMP); }}}} mem (vis,0); for (int i=0, i<n; ++i) {for (int j=0; j<m; ++j) cout<<mz[i][j]<< '; cout<<endl; }}void BFS (int x,int y,int t) {queue<node> q; Node tmp (X,Y,T); Q.push (TMP); Vis[x][y]=1; while (!q.empty ()) {node Now=q.front (); Q.pop (); if (Now.x==enx && now.y==eny) {cout<< "Yes" <<endl; Return } for (int i=0; i<4; ++i) {int xx=now.x+dx[i]; int yy=now.y+dy[i]; int tt=now.t+1; if (OK (xx,yy) && vis[xx][yy]==0 && mz[xx][yy]!=-1) if (Tt<mz[xx][yy]) { Node tmp (XX,YY,TT); Q.push (TMP); Vis[xx][yy]=1; } else if (Tt==mz[xx][yy] && xx==enx && yy==eny) {cout<< "Yes" &L t;<endl; Return }}} cout<< "No"<<endl; return;} int main () {int kase; Freopen ("1.txt", "R", stdin); cin>>kase; while (kase--) {init (); BFS (stx,sty,0); } return 0;}
Fzu 2196 (BFS)