With the provincial election Day2 said a question like ...
#include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <cstring >using namespace Std;inline char nc () {static char buf[100000],*p1=buf,*p2=buf;if (P1==P2) {p2= (p1=buf) +fread (buf, 1,100000,stdin); if (P1==P2) return EOF; }return *p1++;} inline void read (int &x) {char c=nc (), b=1;for (;! (c>= ' 0 ' && c<= ' 9 '); C=nc ()) if (c== '-') b=-1;for (x=0;c>= ' 0 ' && c<= ' 9 '; x=x*10+c-' 0 ', C=NC ()) ; X*=b;} inline void Read (char *s) {char c=nc (); int len=0;for (; c!= ' F ' && c!= ' R ' && c!= '. '; C=NC ()); for (; c== ' F ' | | c== ' R ' | | c== '. '; S[++LEN]=C,C=NC ()); s[++len]=0;} const int N=4005;int n,m;char map[n][n];int dis[n][n];const int dx[]={0,0,1,-1};const int dy[]={1,-1,0,0};struct abcd{ int x,y;abcd (int x=0,int y=0): X (x), Y (y) {}ABCD nxt (int k) {return ABCD (X+dx[k],y+dy[k]);} Char CLR () {return map[x][y];}; queue<abcd> q;inline void BFs (ABCD s) {queue<abcd> Q; ABCD U;q.push (s); while (!q.empty ()) {U=q.front (); Q.pop (); for (int k=0;k<4;k++) if (u.x+dx[k]>0 && u.x+dx[k]<=n && u.y+dy[k]>0 && u.y+dy[k]<=m ) if (U.NXT (k). CLR () ==u.clr () &&!dis[u.x+dx[k]][u.y+dy[k]]) {DIS[U.X+DX[K]][U.Y+DY[K]]=DIS[S.X][S.Y]; Q.push (U.NXT (k)); Q.push (U.NXT (k));}}} inline void BFs () {ABCD u; Q.push (ABCD ()); DIS[1][1]=1;BFS (ABCD);//for (int i=1;i<=n;i++,putchar (' \ n ')) for (int J=1;j<=m;j++,putchar (")) printf ("% D ", Dis[i][j]); printf ("\ n"); Q.empty ()) {U=q.front (); Q.pop (); for (int k=0;k<4;k++) if (u.x+dx[k]>0 && u.x+dx[k]<=n && u.y+dy[k]>0 && u.y+ DY[K]<=M) if (U.NXT (k). CLR ()! = '. ' && u.nxt (k). CLR ()!=u.clr () &&!dis[u.x+dx[k]][u.y+dy[k]]) {dis[ u.x+dx[k]][u.y+dy[k]]=dis[u.x][u.y]+1; Q.push (U.NXT (k)), BFS (U.NXT (k)),//for (int i=1;i<=n;i++,putchar (' \ n '))) for (int J=1;j<=m;j++,putchar (")) printf ("%d", dis[i][j]); printf ("\ n");}}} int main () {freopen ("t.in", "R", stdin), Freopen ("T.out", "w", stdout); Read (n); read (m); for (inti=1;i<=n;i++) read (Map[i]); BFS (); int ans=0;for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) Ans=max (ans,dis[i][j ]);p rintf ("%d\n", ans); return 0;}
[Bfs Mess] Bzoj 3137 [Baltic2013]tracks