Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11225 | Accepted: 4850 |
Description
Input
Output
Sample Input
2 8 8 ######## #......# #.####.# #.####.# #.####.# #.####.# #...#..# #S#E#### 9 5 ######### #.#.#.#.# S.......E #.#.#.#.# #########
Sample Output
37 5 5 17 17 9
Source
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <queue> using namespace std; const int maxn = 100+10; int dx[] = {0, -1, 0, 1}; int dy[] = {-1, 0, 1, 0}; int dl[][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}}; int dr[][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}}; int sx, sy, ex, ey, n, m; char G[maxn][maxn]; struct Pos { int x, y, s; }; int dfs(int x, int y, int d, int step, int dir[][2]) { for(int i=0; i<4; i++) { int j = ((d-1+4)%4+i)%4; int nx = x+dir[j][0]; int ny = y+dir[j][1]; if(nx == ex && ny == ey) return step+1; if(nx < 0 || ny < 0 || nx > n || ny > m) continue; if(G[nx][ny] == '#') continue; return dfs(nx, ny, j, step+1, dir); } } int BFS(int sx, int sy) { bool vis[maxn][maxn]; memset(vis, false, sizeof(vis)); queue<Pos> Q; Q.push((Pos){sx, sy, 1}); vis[sx][sy] = true; while(!Q.empty()) { Pos p = Q.front(); Q.pop(); if(p.x == ex && p.y == ey) return p.s; Pos np; for(int d=0; d<4; d++) { np.x = p.x + dx[d]; np.y = p.y + dy[d]; np.s = p.s + 1; if(np.x < 0 || np.x > n || np.y < 0 || np.y > m) continue; if(vis[np.x][np.y]) continue; if(G[np.x][np.y] != '#') { vis[np.x][np.y] = true; Q.push(np); } } } return -1; } int main() { int T, d1, d2; //freopen("my.txt", "r", stdin); scanf("%d", &T); while(T--) { scanf("%d%d", &m, &n); for(int i=0; i<n; i++) { scanf("%s", G[i]); for(int j=0; j<m; j++) { if(G[i][j] == 'S') { sx = i; sy = j; } else if(G[i][j] == 'E') { ex = i; ey = j; } } } if(sx == 0) { d1 = 3; d2 = 3; } else if(sx == n-1) { d1 = 1; d2 = 1; } else if(sy == 0) { d1 = 2; d2 = 0; } else if(sy == m-1) { d1 = 0; d2 = 2; } printf("%d ", dfs(sx, sy, d1, 1, dl)); printf("%d ", dfs(sx, sy, d2, 1, dr)); printf("%d\n", BFS(sx, sy)); } return 0; }
#include<iostream> #include<cstring> #include<cstdio> using namespace std; string c[45]; int ans=0,n,num=10000,m; struct node { int x; int y; } s,e; int vis[45][45],Map[45][45]; int count[50][50]; int fx[]= {0,0,-1,1}; int fy[]= {-1,1,0,0}; int DFS(int x,int y) { if(x==e.x&&y==e.y) { return 0; } for(int i=0; i<4; i++) { int xx=x+fx[i]; int yy=y+fy[i]; if(xx<m&&yy<n&&xx>=0&&yy>=0&&vis[xx][yy]==0&&Map[xx][yy]&&count[x][y]+1<count[xx][yy]) { vis[xx][yy]=1; count[xx][yy]=count[x][y]+1; DFS(xx,yy); vis[xx][yy]=0; } } return 0; } int Lvis[1606],Rvis[1606]; int Lnum,Rnum;//1上2下3左4右 int Left(int x,int y) { while((x!=e.x||y!=e.y)) { if(Lvis[Lnum]==1) { if(Map[x][y-1]) { y--; Lvis[++Lnum]=3; } else if(Map[x-1][y]) { x--; Lvis[++Lnum]=1; } else if(Map[x][y+1]) { y++; Lvis[++Lnum]=4; } else if(Map[x+1][y]) { x++; Lvis[++Lnum]=2; } continue; } if(Lvis[Lnum]==2) { if(Map[x][y+1]) { y++; Lvis[++Lnum]=4; } else if(Map[x+1][y]) { x++; Lvis[++Lnum]=2; } else if(Map[x][y-1]) { y--; Lvis[++Lnum]=3; } else if(Map[x-1][y]) { x--; Lvis[++Lnum]=1; } continue; } if(Lvis[Lnum]==3) { if(Map[x+1][y]) { x++; Lvis[++Lnum]=2; } else if(Map[x][y-1]) { y--; Lvis[++Lnum]=3; } else if(Map[x-1][y]) { x--; Lvis[++Lnum]=1; } else if(Map[x][y+1]) { y++; Lvis[++Lnum]=4; } continue; } if(Lvis[Lnum]==4) { if(Map[x-1][y]) { x--; Lvis[++Lnum]=1; } else if(Map[x][y+1]) { y++; Lvis[++Lnum]=4; } else if(Map[x+1][y]) { x++; Lvis[++Lnum]=2; } else if(Map[x][y-1]) { y--; Lvis[++Lnum]=3; } continue; } } } int Right(int x,int y) { while(x!=e.x||y!=e.y) { if(Rvis[Rnum]==1) { if(Map[x][y+1]) { y++; Rvis[++Rnum]=4; } else if(Map[x-1][y]) { x--; Rvis[++Rnum]=1; } else if(Map[x][y-1]) { y--; Rvis[++Rnum]=3; } else if(Map[x+1][y]) { x++; Rvis[++Rnum]=2; } continue; } if(Rvis[Rnum]==2) { if(Map[x][y-1]) { y--; Rvis[++Rnum]=3; } else if(Map[x+1][y]) { x++; Rvis[++Rnum]=2; } else if(Map[x][y+1]) { y++; Rvis[++Rnum]=4; } else if(Map[x-1][y]) { x--; Rvis[++Rnum]=1; } continue; } if(Rvis[Rnum]==3) { if(Map[x-1][y]) { x--; Rvis[++Rnum]=1; } else if(Map[x][y-1]) { y--; Rvis[++Rnum]=3; } else if(Map[x+1][y]) { x++; Rvis[++Rnum]=2; } else if(Map[x][y+1]) { y++; Rvis[++Rnum]=4; } continue; } if(Rvis[Rnum]==4) { if(Map[x+1][y]) { x++; Rvis[++Rnum]=2; } else if(Map[x][y+1]) { y++; Rvis[++Rnum]=4; } else if(Map[x-1][y]) { x--; Rvis[++Rnum]=1; } else if(Map[x][y-1]) { y--; Rvis[++Rnum]=3; } continue; } } } int main() { int T; while(cin>>T) { while(T--) { num=0x3f3f3f3f; Lnum=1; Rnum=1; cin>>n>>m; for(int i=0; i<m; i++) cin>>c[i]; for(int i=0; i<m; i++) for(int j=0; j<n; j++) { if(c[i][j]=='#') Map[i][j]=0; else Map[i][j]=1; if(c[i][j]=='S') { s.x=i; s.y=j; } if(c[i][j]=='E') { e.x=i; e.y=j; } } if(s.x==0) { Lvis[Lnum]=2; Rvis[Rnum]=2; } else if(s.x==m-1) { Lvis[Lnum]=1; Rvis[Rnum]=1; } else if(s.y==0) { Lvis[Lnum]=4; Rvis[Rnum]=4; } else if(s.y==n-1) { Lvis[Lnum]=3; Rvis[Rnum]=3; } for(int i=0;i<m;i++) for(int j=0;j<n;j++) count[i][j]=10000000; count[s.x][s.y]=1; vis[s.x][s.y]=1; memset(vis,0,sizeof(vis)); DFS(s.x,s.y); Left(s.x,s.y); Right(s.x,s.y); cout<<Lnum<<" "<<Rnum<<" "<<count[e.x][e.y]<<endl; } } }