标签:span lin auto har 输出 ons fir void define
#include<stdio.h> #include<iostream> #include<vector> #include<queue> #define mp make_pair #define pb emplace_back #define fi first #define se second using namespace std; typedef pair<int,int> pir; const int N=55; char da[N][N];vector<pir>dir[6]; int ans,n,m;;bool tim,v1[N][N],v2[N][N]; inline bool border(const int x,const int y){ return x<1||y<1||x>n||y>m||da[x][y]==‘#‘; } inline int value(const int x,const int y){ const char &c=da[x][y];int v=0; switch(c){ case ‘+‘:v=1;break; case ‘S‘:v=1;break; case ‘T‘:v=1;break; case ‘-‘:v=2;break; case ‘|‘:v=3;break; case ‘.‘:v=4;break; } return v; } void bfs(pir S){//(Sx,Sy)->(i,j) available queue<pir>q;q.push(S); for(int cx,cy,nx,ny;!q.empty();){ pir t=q.front();q.pop(); cx=t.fi;cy=t.se; if(border(cx,cy)) continue; if(v1[cx][cy]) continue;v1[cx][cy]=1; int whe=value(t.fi,t.se); if(whe) for(auto &i:dir[whe]){ nx=cx+i.fi; ny=cy+i.se; if(v1[nx][ny]) continue; q.push(mp(nx,ny)); } } } void BFS(pir S){//(i,j)->(Tx,Ty) available queue<pir>q;q.push(S); for(int cx,cy,nx,ny;!q.empty();){ pir t=q.front();q.pop(); cx=t.fi;cy=t.se; if(border(cx,cy)) continue; if(v2[cx][cy]) continue;v2[cx][cy]=1; for(int i=0;i<4;i++){ nx=cx+dir[1][i].fi; ny=cy+dir[1][i].se; if(da[nx][ny]==‘S‘||da[nx][ny]==‘T‘||da[nx][ny]==‘+‘|| i==0&&da[nx][ny]==‘.‘||i<2&&da[nx][ny]==‘|‘||i>1&&da[nx][ny]==‘-‘){ q.push(mp(nx,ny)); } } } } void work1(){ for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(da[i][j]==‘S‘){ bfs(mp(i,j)); return ; } } } } void work2(){ for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(da[i][j]==‘T‘){ if(!v1[i][j]){puts("I‘m stuck!");exit(0);} BFS(mp(i,j)); return ; } } } } int main(){ dir[1].pb(mp(-1,0));dir[1].pb(mp(1,0));dir[1].pb(mp(0,-1)); dir[1].pb(mp(0,1)); dir[2].pb(mp(0,1)); dir[2].pb(mp(0,-1)); dir[3].pb(mp(1,0)); dir[3].pb(mp(-1,0)); dir[4].pb(mp(1,0)); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%s",da[i]+1); work1(); work2(); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(v1[i][j]&&!v2[i][j]) ans++; } } printf("%d\n",ans); return 0; }
标签:span lin auto har 输出 ons fir void define
原文地址:https://www.cnblogs.com/shenben/p/12271175.html