鎵懼嚭涓や釜榪峰鐨勫嚭鍙o紝鐒跺悗鍒嗗埆榪涜dfs錛屾眰鍑哄埌姣忎竴涓偣鐨勬渶鐭窛紱匯?br />鐒跺悗瀵規(guī)瘡涓涓偣錛屾眰鍒版渶鐭殑閭d釜鍑哄彛鐨勮窛紱伙紝鐒跺悗鍐嶆眰榪欎釜鍊肩殑鏈澶у煎嵆鍙?br />鎵懼嚭鍙e啓寰楁瘮杈冪箒鐞愩?br />
#include聽<iostream>
#include聽<fstream>
#include聽<queue>
using聽namespace聽std;
ifstream聽fin("maze1.in");
ofstream聽fout("maze1.out");
#ifdef聽_DEBUG
#define聽out聽cout
#define聽in聽cin
#else
#define聽out聽fout
#define聽in聽fin
#endif
char聽maze[2*100+1][2*38+1];
bool聽visited[100][38];
int聽shortest1[100][38];
int聽shortest2[100][38];
bool聽get_first;
int聽exit1r,exit1c;
int聽exit2r,exit2c;
int聽w,h;
struct聽queue_node{
聽聽聽聽int聽i,j,depth;
聽聽聽聽queue_node(int聽i,int聽j,int聽depth){
聽聽聽聽聽聽聽聽this->i聽=聽i;
聽聽聽聽聽聽聽聽this->j聽=聽j;
聽聽聽聽聽聽聽聽this->depth聽=聽depth;
聽聽聽聽}
};
void聽bfs(int聽i,int聽j,int聽shortest[100][38])
{
聽聽聽聽queue<queue_node>q;
聽聽聽聽q.push(queue_node(i,j,1));
聽聽聽聽while(!q.empty()){
聽聽聽聽聽聽聽聽queue_node聽node聽=聽q.front();
聽聽聽聽聽聽聽聽q.pop();
聽聽聽聽聽聽聽聽if(visited[node.i][node.j])
聽聽聽聽聽聽聽聽聽聽聽聽continue;
聽聽聽聽聽聽聽聽visited[node.i][node.j]聽=聽true;
聽聽聽聽聽聽聽聽shortest[node.i][node.j]聽=聽node.depth;
聽聽聽聽聽聽聽聽if(node.i!=0&&maze[2*node.i][2*node.j+1]=='聽'){
聽聽聽聽聽聽聽聽聽聽聽聽q.push(queue_node(node.i-1,node.j,node.depth+1));
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽if(node.i!=h-1&&maze[2*node.i+2][2*node.j+1]=='聽'){
聽聽聽聽聽聽聽聽聽聽聽聽q.push(queue_node(node.i+1,node.j,node.depth+1));
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽if(node.j!=0&&maze[2*node.i+1][2*node.j]=='聽'){
聽聽聽聽聽聽聽聽聽聽聽聽q.push(queue_node(node.i,node.j-1,node.depth+1));
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽if(node.j!=w-1&&maze[2*node.i+1][2*node.j+2]=='聽'){
聽聽聽聽聽聽聽聽聽聽聽聽q.push(queue_node(node.i,node.j+1,node.depth+1));
聽聽聽聽聽聽聽聽}
聽聽聽聽}
}
void聽solve()
{
聽聽聽聽in>>w>>h;
聽聽聽聽for(int聽i=0;i<2*h+1;++i)
聽聽聽聽聽聽聽聽for(int聽j=0;j<2*w+1;++j){
聽聽聽聽聽聽聽聽聽聽聽聽do{
聽聽聽聽聽聽聽聽聽聽聽聽in.get(maze[i][j]);
聽聽聽聽聽聽聽聽聽聽聽聽}while(maze[i][j]=='\n');
聽聽聽聽聽聽聽聽聽聽聽聽if((i==0||i==2*h||j==0||j==2*w)&&maze[i][j]=='聽'){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽if(!get_first){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽if(i==2*h)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽exit1r聽=聽h-1;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽else
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽exit1r聽=聽i/2;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽if(j==2*w)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽exit1c聽=聽w-1;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽else
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽exit1c聽=聽j/2;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽get_first聽=聽true;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}else{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽if(i==2*h)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽exit2r聽=聽h-1;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽else
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽exit2r聽=聽i/2;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽if(j==2*w)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽exit2c聽=聽w-1;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽else
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽exit2c聽=聽j/2;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽}
聽聽聽聽memset(visited,0,sizeof(visited));
聽聽聽聽bfs(exit1r,exit1c,shortest1);
聽聽聽聽memset(visited,0,sizeof(visited));
聽聽聽聽bfs(exit2r,exit2c,shortest2);
聽聽聽聽int聽res聽=聽INT_MIN;
聽聽聽聽for(int聽i=0;i<h;++i)
聽聽聽聽聽for(int聽j=0;j<w;++j){
聽聽聽聽聽聽聽聽聽res聽=聽max(res,聽min(shortest1[i][j],shortest2[i][j])聽);
聽聽聽聽聽}
聽聽聽聽out<<res<<endl;
}
int聽main(int聽argc,char聽*argv[])
{
聽聽聽聽solve();聽
聽聽聽聽return聽0;
}

]]>