QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#172223#5472. Secure the Top SecretPhantomThreshold#AC ✓5ms8064kbC++204.7kb2023-09-09 18:17:302023-09-09 18:17:31

Judging History

你现在查看的是最新测评结果

  • [2023-09-09 18:17:31]
  • 评测
  • 测评结果:AC
  • 用时:5ms
  • 内存:8064kb
  • [2023-09-09 18:17:30]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
namespace mcmf
{
	const int MAXN=11111,MAXM=111111;
	const int S=11100,T=11101;
	struct edge
	{
		int v,f,w;
		edge *next,*rev;
	}*h[MAXN],pool[MAXM*2];
	int top;
	void addedge(int u,int v,int c,int w)
	{
//		cerr<<"addedge "<<u<<' '<<v<<' '<<c<<' '<<w<<endl;
		edge *tmp=&pool[top++];tmp->v=v;tmp->f=c;tmp->w=w;tmp->next=h[u];h[u]=tmp;
		edge *pmt=&pool[top++];pmt->v=u;pmt->f=0;pmt->w=-w;pmt->next=h[v];h[v]=pmt;
		tmp->rev=pmt;pmt->rev=tmp;
	}
	deque<int> q;
	int last[MAXN],inq[MAXN];
	long long dis[MAXN],maxf[MAXN];
	edge *laste[MAXN];
	long long totcost=0,totflow=0;
	bool spfa()
	{
		memset(dis,0x3f,sizeof(dis));
		memset(last,0,sizeof(last));
		memset(maxf,0,sizeof(maxf));
		memset(inq,0,sizeof(inq));
		while(not q.empty())q.pop_front();
		q.push_front(S);
		maxf[S]=0x3f3f3f3f3f3f3f3fll;
		inq[S]=1;
		dis[S]=0;
		while(not q.empty())
		{
			int u=q.front();q.pop_front();
			inq[u]=0;
			for(edge *tmp=h[u];tmp;tmp=tmp->next)
			{
				if(tmp->f and dis[tmp->v]>dis[u]+tmp->w)
				{
					dis[tmp->v]=dis[u]+tmp->w;
					last[tmp->v]=u;
					maxf[tmp->v]=min(maxf[u],(long long)tmp->f);
					laste[tmp->v]=tmp;
					if(not inq[tmp->v])
					{
						if(q.empty() or dis[q.front()]>dis[tmp->v])q.push_front(tmp->v);
						else q.push_back(tmp->v);
						inq[tmp->v]=1;
					}
				}
			}
		}
		if(dis[T]>=0x3f3f3f3f3f3f3f3fll)return false;
//		cerr<<"mincost "<<dis[T]<<endl;
		int u=T;
		while(last[u])
		{
			laste[u]->f--;
			laste[u]->rev->f++;
			u=last[u];
		}
		totcost+=dis[T];
		totflow++;
		return true;
	}
}
using mcmf::addedge;
void addedgeu(int u,int v,int c,int w)
{
	addedge(u,v,c,w);
	addedge(v,u,c,w);
}
using mcmf::S;
using mcmf::T;
using mcmf::spfa;
int main()
{
	ios_base::sync_with_stdio(false);
	int n,m;
	cin>>n>>m;
	vector<string> s(n+5);
	for(int i=1;i<=n;i++)
	{
		string t;
		cin>>t;
		s[i]=' '+t;
	}
	vector<int> dx={-1,0,1,0},dy={0,1,0,-1};
	int sx=0,sy=0,tx=0,ty=0,ux=0,uy=0,sp=0,tp=0,up=0;
	auto gao=[&](int x,int y)
	{
		if(x==1)return y;
		else if(y==m)return x+m;
		else if(x==n)return n+m+m-y;
		else if(y==1) return n+m+m+n-x;
		else return 0;
	};
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(s[i][j]=='S')
				sx=i,sy=j,sp=gao(i,j);
			else if(s[i][j]=='T')
				tx=i,ty=j,tp=gao(i,j);
			else if(s[i][j]=='U')
				ux=i,uy=j,up=gao(i,j);
		}
	}
	int flip=0;
	if(sp>tp)swap(sp,tp),swap(sx,tx),swap(sy,ty);
	if(sp<up and up<tp)
	{
		flip=1;
		swap(sx,sy);
		swap(tx,ty);
		swap(ux,uy);
		vector<string> tmps(m+5,string(n+5,' '));
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
				tmps[j][i]=s[i][j];
		s.swap(tmps);
		swap(n,m);
	}
	set<tuple<int,int,int,int>> ban;
	int nx=sx,ny=sy,nd;
	if(nx==1)nd=0;
	else if(ny==m)nd=1;
	else if(nx==n)nd=2;
	else nd=3;
	auto addban=[&](int x1,int y1,int x2,int y2)
	{
//		cerr<<"addban "<<x1<<' '<<y1<<' '<<x2<<' '<<y2<<endl;
		if(x1>x2)swap(x1,x2);
		if(y1>y2)swap(y1,y2);
		ban.emplace(x1,y1,x2,y2);
	};
	while(nx!=tx or ny!=ty)
	{
		//go!
		if(1<=nx+dx[nd] and nx+dx[nd]<=n and 1<=ny+dy[nd] and ny+dy[nd]<=m and s[nx+dx[nd]][ny+dy[nd]]!='#')
		{
			addban(nx,ny,nx+dx[nd],ny+dy[nd]);
			nx+=dx[nd];
			ny+=dy[nd];
			nd=(nd+3)%4;
		}
		else nd=(nd+1)%4;
	}
	auto hs=[&](int x,int y)
	{
		return x*(m+1)+y+1;
	};
	int D;
	cin>>D;
	for(int i=1;i<=D;i++)
	{
		int x,y;
		char dir;
		cin>>x>>y>>dir;
		if(flip)swap(x,y),dir='r'+'b'-dir;
		int td;
		if(dir=='r')td=1;
		else td=2;
		if(not ban.contains({x,y,x+dx[td],y+dy[td]}))
		{
			if(td==1)addedgeu(hs(x-1,y),hs(x,y),1,1);
			else addedgeu(hs(x,y-1),hs(x,y),1,1);
		}
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		{
			if(s[i][j]=='#')
			{
				addedgeu(hs(i-1,j-1),hs(i,j-1),inf,0);
				addedgeu(hs(i,j-1),hs(i,j),inf,0);
				addedgeu(hs(i-1,j-1),hs(i-1,j),inf,0);
				addedgeu(hs(i-1,j),hs(i,j),inf,0);
			}
		}
	nx=tx,ny=ty;
	if(nx==1)nd=0;
	else if(ny==m)nd=1;
	else if(nx==n)nd=2;
	else nd=3;
	vector<int> dx2={-1,-1,0,0},dy2={-1,0,0,-1};
	while(nx!=ux or ny!=uy)
	{
		if(1<=nx+dx[nd] and nx+dx[nd]<=n and 1<=ny+dy[nd] and ny+dy[nd]<=m)
		{
			addedge(S,hs(nx+dx2[nd],ny+dy2[nd]),inf,0);
			nx+=dx[nd];
			ny+=dy[nd];
			nd=(nd+3)%4;
		}
		else nd=(nd+1)%4;
	}
	while(nx!=sx or ny!=sy)
	{
		if(1<=nx+dx[nd] and nx+dx[nd]<=n and 1<=ny+dy[nd] and ny+dy[nd]<=m)
		{
			addedge(hs(nx+dx2[nd],ny+dy2[nd]),T,inf,0);
			nx+=dx[nd];
			ny+=dy[nd];
			nd=(nd+3)%4;
		}
		else nd=(nd+1)%4;
	}
	if(not spfa())
	{
		cout<<-1<<endl;
		return 0;
	}
	if(not spfa())
	{
		cout<<-1<<endl;
		return 0;
	}
	cout<<mcmf::totcost<<endl;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3952kb

input:

3 3
S..
#..
U.T
7
1 2 b
1 3 b
2 2 b
2 2 r
2 3 b
3 1 r
3 2 r

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 0ms
memory: 4112kb

input:

2 2
ST
.U
4
1 1 r
1 1 b
1 2 b
2 1 r

output:

-1

result:

ok single line: '-1'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3804kb

input:

7 10
U.........
..........
###.......
..........
.......###
..........
S........T
18
4 4 r
5 4 r
6 7 r
7 7 r
3 4 b
3 5 b
3 6 b
3 7 b
3 8 b
3 9 b
3 10 b
5 1 b
5 2 b
5 3 b
5 4 b
5 5 b
5 6 b
5 7 b

output:

14

result:

ok single line: '14'

Test #4:

score: 0
Accepted
time: 1ms
memory: 5892kb

input:

2 5
.T.#S
....U
10
1 3 b
1 1 r
1 1 b
2 1 r
1 2 b
1 5 b
2 2 r
1 2 r
2 3 r
2 4 r

output:

-1

result:

ok single line: '-1'

Test #5:

score: 0
Accepted
time: 0ms
memory: 3856kb

input:

5 5
U.S..
.....
.....
.....
.T...
12
2 4 b
4 1 b
2 2 r
1 5 b
2 2 b
4 3 b
5 3 r
1 2 b
3 2 r
2 1 r
3 3 r
2 4 r

output:

-1

result:

ok single line: '-1'

Test #6:

score: 0
Accepted
time: 1ms
memory: 3956kb

input:

5 4
....
...U
....
S#..
.#T.
12
3 4 b
2 1 b
4 3 r
2 2 b
4 3 b
3 3 r
2 3 r
1 1 b
2 2 r
4 4 b
3 1 b
1 3 r

output:

-1

result:

ok single line: '-1'

Test #7:

score: 0
Accepted
time: 1ms
memory: 5640kb

input:

3 3
UST
###
.#.
2
1 1 r
1 2 r

output:

-1

result:

ok single line: '-1'

Test #8:

score: 0
Accepted
time: 1ms
memory: 3852kb

input:

4 2
U.
..
.T
S.
8
1 1 b
3 1 r
4 1 r
1 1 r
2 2 b
3 2 b
3 1 b
2 1 r

output:

5

result:

ok single line: '5'

Test #9:

score: 0
Accepted
time: 1ms
memory: 5872kb

input:

2 2
S.
UT
4
1 1 b
1 2 b
2 1 r
1 1 r

output:

-1

result:

ok single line: '-1'

Test #10:

score: 0
Accepted
time: 1ms
memory: 3920kb

input:

3 4
#U.T
#...
.S#.
3
1 2 r
2 2 b
3 1 r

output:

-1

result:

ok single line: '-1'

Test #11:

score: 0
Accepted
time: 1ms
memory: 3856kb

input:

3 5
.U.S.
.#.##
#.T#.
7
1 3 r
1 2 r
1 4 r
1 1 b
1 3 b
2 3 b
3 2 r

output:

-1

result:

ok single line: '-1'

Test #12:

score: 0
Accepted
time: 1ms
memory: 3924kb

input:

4 4
.T.S
#.#.
....
U...
6
3 1 b
1 4 b
2 4 b
3 2 b
4 1 r
3 2 r

output:

-1

result:

ok single line: '-1'

Test #13:

score: 0
Accepted
time: 1ms
memory: 4152kb

input:

5 5
.##T.
##.#.
.#...
##.##
#SU..
6
2 3 b
3 3 r
4 3 b
1 5 b
3 4 r
5 4 r

output:

-1

result:

ok single line: '-1'

Test #14:

score: 0
Accepted
time: 1ms
memory: 5840kb

input:

97 97
S...............................................................................................T
.................................................................................................
.................................................................................................
...

output:

130

result:

ok single line: '130'

Test #15:

score: 0
Accepted
time: 1ms
memory: 4192kb

input:

100 94
S............................................................................................T
..............................................................................................
..............................................................................................
...........

output:

126

result:

ok single line: '126'

Test #16:

score: 0
Accepted
time: 1ms
memory: 4452kb

input:

95 92
S..........................................................................................T
............................................................................................
............................................................................................
..................

output:

124

result:

ok single line: '124'

Test #17:

score: 0
Accepted
time: 0ms
memory: 5912kb

input:

92 92
S..........................................................................................T
............................................................................................
............................................................................................
..................

output:

124

result:

ok single line: '124'

Test #18:

score: 0
Accepted
time: 1ms
memory: 5804kb

input:

96 100
S..................................................................................................T
....................................................................................................
..............................................................................................

output:

134

result:

ok single line: '134'

Test #19:

score: 0
Accepted
time: 1ms
memory: 4320kb

input:

92 90
S........................................................................................T
..........................................................................................
..........................................................................................
........................

output:

120

result:

ok single line: '120'

Test #20:

score: 0
Accepted
time: 2ms
memory: 4324kb

input:

96 90
S........................................................................................T
..........................................................................................
..........................................................................................
........................

output:

120

result:

ok single line: '120'

Test #21:

score: 0
Accepted
time: 1ms
memory: 5980kb

input:

92 94
S............................................................................................T
..............................................................................................
..............................................................................................
............

output:

126

result:

ok single line: '126'

Test #22:

score: 0
Accepted
time: 1ms
memory: 4360kb

input:

99 94
S............................................................................................T
..............................................................................................
..............................................................................................
............

output:

126

result:

ok single line: '126'

Test #23:

score: 0
Accepted
time: 1ms
memory: 4272kb

input:

93 91
S.........................................................................................T
...........................................................................................
...........................................................................................
.....................

output:

122

result:

ok single line: '122'

Test #24:

score: 0
Accepted
time: 2ms
memory: 6060kb

input:

50 83
........................#.............................#........#......#.....#......
...........................................#........#................#........#....
....................#.....#........................................................
................................#............

output:

22

result:

ok single line: '22'

Test #25:

score: 0
Accepted
time: 1ms
memory: 6180kb

input:

58 98
.........#.....#...U............#..............#.......#....................##...........#........
.........#........#.....#...#..#........#.#.........#..................#.#........................
.#.##.......#........#......#..#.....#.........#..#.........#...##.........###..##......#..........

output:

-1

result:

ok single line: '-1'

Test #26:

score: 0
Accepted
time: 1ms
memory: 6752kb

input:

80 48
..##.#.........#...#.....#............#.#.#.#...
....#........#.......#...#.##.#.##....#.##.#...#
#......##.#..#.......#.....##..#..#.......##.#.#
.##.#...#.#.#.#.....###...#......#............##
.....#.#.....#.#..##.......##..#...#.#..#..##...
.......#....#.#...##.##.##....#...#......#..####
...

output:

2

result:

ok single line: '2'

Test #27:

score: 0
Accepted
time: 0ms
memory: 4440kb

input:

40 49
#.....................#..................#.......
..................#......#....#..#....#.#.......U
......#.................................#........
.................................................
#.......#.......#..........#.....................
....#.......#.......#............#.............

output:

9

result:

ok single line: '9'

Test #28:

score: 0
Accepted
time: 0ms
memory: 3908kb

input:

25 39
.......................#...............
...................................#...
.......................................
##.......................#.............
..................#.........#..........
..#...................................U
...............................#.......
.................

output:

-1

result:

ok single line: '-1'

Test #29:

score: 0
Accepted
time: 1ms
memory: 4008kb

input:

17 47
..#.....#...#...#........................T.#...
.........................#.........#......#....
.....#..........##..#.......#...#........#.....
......#.......................#................
##...#........#.......##.#.#.......#.##........
...............................................
#..#.....

output:

-1

result:

ok single line: '-1'

Test #30:

score: 0
Accepted
time: 1ms
memory: 5680kb

input:

18 66
...#..#...#..#.#...#U#.#....##...#.#.#.......#.#..........#......#
....##...#...##..##.....#..#.##.#..##...........#....#.....#...#.#
#...##..........##.....#........#...##.#.##............#.#...#.#..
#.#..#..#........#..#....#...#..#.....##..#..#..............#....#
.........#...##.#..#.#.###...

output:

4

result:

ok single line: '4'

Test #31:

score: 0
Accepted
time: 3ms
memory: 5172kb

input:

69 81
.....#..........#.##..U....#..#.S#....#..###..##...........#....##..####.#T.##.##
##........##...##.#.#.##...#.###.#...#....##..#...#..###.....#.................#.
.......##...###..#....#..###.....#..##...#....#..#..#...#.#...#.#....#...##...#.#
#...#...........##..###.#.#.##.#...#.#....#..#.#...

output:

2

result:

ok single line: '2'

Test #32:

score: 0
Accepted
time: 2ms
memory: 4516kb

input:

87 26
.##....#.U.#.#.#......#...
...#...##..#...#.##...#..T
#..........#...#.....#.#..
##.#..##...##.#..#..#.....
................##.....##.
......###..##.#..#..#.....
...#.....#....##..#.......
#.......###..#.....#......
######..##......#.....#...
#.#.#....#.#.........#..#.
.#.....#..###..#......#....

output:

4

result:

ok single line: '4'

Test #33:

score: 0
Accepted
time: 2ms
memory: 5896kb

input:

19 92
.#....#.#.....#.#.........###..S##.#...#..#.#..#...#..####.......#.......#...#.##.##...##...
.......#.#.#.#..#...#..###..#...#..#....#......##........#.#.........#..#.###....#.....#....
......#...#..#.#...#.....##..##......##.###....#...#....#.#.###..#...#####.##..#....#..#.###
.##...##.#.#..#...

output:

2

result:

ok single line: '2'

Test #34:

score: 0
Accepted
time: 2ms
memory: 6760kb

input:

92 96
...#..##.#..##.......#.....#..#.#S#....#.........##...#.#..#...#.......#..#............#..#..#..
...#.#.#.#....#.##....#....#.#....#...#...#....#..##....#..#....#...#..#....#....##....#..#.....
.#...#..#.........#..........#.##.#..#..#.#..#..#.....#.#....#.#.#..........##.......#.#....##..
......

output:

-1

result:

ok single line: '-1'

Test #35:

score: 0
Accepted
time: 2ms
memory: 6048kb

input:

90 90
##....#........#..#..#.#...#...#.#....#.#....##..#.#......#...#......T.#..####........#.#.
..#...............#.#...#...#......#.......#.#..#.#.......#...#.....##....#..##.....#.....
.###.......##.##.........#.#.#..##.##..#........#.#......#......#.#.#..###..##..#.#.....#.
..##......#.........#...

output:

6

result:

ok single line: '6'

Test #36:

score: 0
Accepted
time: 2ms
memory: 6300kb

input:

98 93
..#...........##..........##.#......#....#.....#.......#..#.#.#.......#..................U.#.
..#........#..#..#...#...##.................#.#........#.......##.#..#..#.....#.......#.###.#
...........###..#####.#..##.......#.#........#.#.....#.......#......#....#..#..#.............
#...#.#........

output:

-1

result:

ok single line: '-1'

Test #37:

score: 0
Accepted
time: 0ms
memory: 5592kb

input:

96 97
#............#.................#....#.#.#...#....#..###..#.#....#.##...#..#...........#..........
..#.........##........#...#.##....##....###...#.###..#.#..............##.#.....................#.
..#..........#.#.#..#...........#...##............#......##...................#......##....#.....
...

output:

-1

result:

ok single line: '-1'

Test #38:

score: 0
Accepted
time: 4ms
memory: 6376kb

input:

91 94
#...........#.#.##.#..#.....#.#........S.##.......##....#..#........#..#...#..#..#........#U#.
...#.....#..#.....##.#..#.##....#...#.##....#....#......##.#.#............##.......#....#....#
#..#...#..#...#.#..###.#...#....#..##....#....##....#.#.##.........#..#...#.#........#.#.#..##
....##......

output:

4

result:

ok single line: '4'

Test #39:

score: 0
Accepted
time: 3ms
memory: 6036kb

input:

94 91
...........#......#...#.....#..###.....#.......#....##.#....#........#......####.......#...
..#...###...##.......#..#....#..#.###.#....#......#.#..####.##...#...#..##..#.#...###..#...
..#.#.##..#...........#.........#.#.......#...........#.###......#..#....#......#...#......
.#.....##..#.....#...

output:

21

result:

ok single line: '21'

Test #40:

score: 0
Accepted
time: 5ms
memory: 7860kb

input:

90 94
##..........#.#.#.....#.....#..#..#......#.#..##..##....##..#.#.#..#.###.#...#.........T.#....
....#.#.....#.#...............##..#..##.##.#..#....##....###.#.#.#...#..###.........#.##....##
...##.............#..#.....#..#..#.......#....#...###....#.#..#...#.#....#...###..####........
#..##...#...

output:

24

result:

ok single line: '24'

Test #41:

score: 0
Accepted
time: 2ms
memory: 6544kb

input:

93 98
.............#.........#.....U..#......................##.........................................
...........#............#...........................................................###...#.......
....................................................................................#..............

output:

-1

result:

ok single line: '-1'

Test #42:

score: 0
Accepted
time: 4ms
memory: 6152kb

input:

96 100
..........#........#..#...#..#.........#....#....#.#.....##......##....##............#.............#
.......#.#..#.#.#..#....................#..............##.....#.........#...#...#.....#.....#.....#.
##.....##.#.###...#...#.#.............###.#.......#.#................#........#........##.....

output:

12

result:

ok single line: '12'

Test #43:

score: 0
Accepted
time: 4ms
memory: 6164kb

input:

100 99
................#..#............T.....#.....#.....#............#.#.....U.#..#.....#..........#.....
.................................#.#...............................................................
............#..........#..#..#.........#...................#............................#.......

output:

100

result:

ok single line: '100'

Test #44:

score: 0
Accepted
time: 1ms
memory: 3976kb

input:

13 22
..#...#U#......#....##
......##.............#
......#....#...#.....#
.........#..#.........
...................#..
......###.........#..#
....#..............#.T
.#..#...#.............
#.....#.....##...#....
.#.........#.##.......
......#..#............
.#.....#...#...#......
..S..#.#.............

output:

0

result:

ok single line: '0'

Test #45:

score: 0
Accepted
time: 1ms
memory: 6032kb

input:

84 35
...#.#.#.......###..#......##.###..
...#....##.#..##......###.#..#.##..
...###.....#..###.#..#.#..#...#...#
#...###.#..#..#....#...#.#.#.....#.
###...##......#.##.#.....#...#.#..#
...#.#...###......#.....#....#....#
#.#..##.##.###...#..###....##.##...
###..#.#........#...#.#.##.#.#.....
...#.....

output:

0

result:

ok single line: '0'

Test #46:

score: 0
Accepted
time: 1ms
memory: 3984kb

input:

3 82
#..#.#.....#........#.##........####...#...#..##...#......#..S.##..#..#..#..##..#.
...#..##.............#...#..#....#...............#.#....##....#...................
.#..............U................#...#...##.......#..#...T.#..#......#...#......#.
250
2 73 b
2 19 r
3 11 r
2 13 r
2 35 b
3 64 r
...

output:

0

result:

ok single line: '0'

Test #47:

score: 0
Accepted
time: 3ms
memory: 5136kb

input:

85 67
...##....##..#..#....##..##S.##...#....#.#............#............
.......#.#..#....#..#..#.#...##..##.#...#.#.#...#......#.....##..#.
..#.....#.....#.#.......#.....#.......#..#.###..#.........#........
##.#.#.#.#.###.#..........##....#.....#.#..##.#...........#...###.#
...........#.......#.....

output:

0

result:

ok single line: '0'

Test #48:

score: 0
Accepted
time: 2ms
memory: 5872kb

input:

42 30
##.....##.#.#T.#.#.#.#....#.#.
..#.......##...#.#.#...#......
#.........#...#.##....#..#....
.....#..#....#..#...##...####.
#.......###...#..#......#..#..
...#.....#......#..#......#.##
##.#.#.#..#...#.......####....
....#...##.....#....#.#.#.....
........#..#.#......#....#.#..
.#.##.#...###.....

output:

0

result:

ok single line: '0'

Test #49:

score: 0
Accepted
time: 2ms
memory: 4948kb

input:

62 62
..........##...#.#.............#.#..........#.................
...............#.#............#..........................#....
...................#.........................##...............
........#............#..................#................#....
..........................#..#.......##......

output:

8

result:

ok single line: '8'

Test #50:

score: 0
Accepted
time: 0ms
memory: 7004kb

input:

89 74
...#....#.#....#...........#..#T.#....................##.U.............##.
...##.....#......##......................#....#.#..#....#..#........##....
...#.##............#...#.........#........#.#..........##.#..#............
.#.......#.....#.....#..#..............##.#................#............

output:

4

result:

ok single line: '4'

Test #51:

score: 0
Accepted
time: 2ms
memory: 6084kb

input:

100 37
.....................#..#.........#..
.............#........#........#.....
...........#.........................
..##..#.....#.............#..........
............#..................#.....
#..#................##...#...........
..#...........#...#..................
..................#.#.....#...

output:

6

result:

ok single line: '6'

Test #52:

score: 0
Accepted
time: 2ms
memory: 5052kb

input:

44 89
...#.#.#...##.#.#.....#..##..#.#......##.........#..##....#.....##....##.#.........#..#..
##....##.#...........#...#..##.#....#...#.#.#......#........#..#..........#.......##....#
....#..#...#.#......#.#...###.#......#....#.#.#......#...##..............##.#.##...#..#..
.#.....###.#..#...#..#.....

output:

4

result:

ok single line: '4'

Test #53:

score: 0
Accepted
time: 1ms
memory: 4196kb

input:

68 17
##.....#....#....
.#.##...#...#...#
...#....#....##..
..#...#........#.
..#..####.#...##.
.....###.........
.#.........#..#.#
....#.#.#.#.....#
...#.....#.......
#..#.###...####..
..##.##....#.#...
##.....#.#.....#.
....#....#......#
..##.............
.....#.........#.
....#...........#
..##.....

output:

6

result:

ok single line: '6'

Test #54:

score: 0
Accepted
time: 0ms
memory: 5872kb

input:

16 23
..#..##.#U.#...#....#.#
...#...##.##.#...##...#
...#.#..#..##.#..#.#...
...#..#.#.#..#....#...#
##..#..#........#......
.......#.#.#........#..
.#.....#...#..#.#......
........#..###.....###.
.##.......#.###..##.#..
.....#..######......#..
#..#....#....#.#....#.#
....#.#..#....##....##.
.#.#.....

output:

-1

result:

ok single line: '-1'

Test #55:

score: 0
Accepted
time: 1ms
memory: 4036kb

input:

43 67
.............#....#.............................S.#............#...
..........#.................#.............#........................
................................#................................#.
.........................#..........#...........#..................
....#....................

output:

-1

result:

ok single line: '-1'

Test #56:

score: 0
Accepted
time: 0ms
memory: 4336kb

input:

79 22
.#......#..#...#.#...#
............#.........
.###.......#.#..#....#
..#..#...........##...
......#.....###....#.#
#.......#..#........#.
.....#.....#..........
......#.#.#.......#...
....#......##..#....#.
...#...#......##......
.....#...#.#..###.....
.....###.......#......
#...........#..#.....

output:

-1

result:

ok single line: '-1'

Test #57:

score: 0
Accepted
time: 1ms
memory: 3948kb

input:

9 18
...#...#....#....#
.........##.......
..#.....#.#..###.U
...#..#..####...#T
.#.###.##.......#.
......#....####.#.
#....#....##.#..##
S............#.#..
###..#....#...#..#
0

output:

-1

result:

ok single line: '-1'

Test #58:

score: 0
Accepted
time: 1ms
memory: 6004kb

input:

40 72
#...#...#......#.#.#....#.##..##.#..##.#####..#.##...###.#.####..##.....
.###.#..#.......#..#..#..#.#.#....##.....#..###.#..#.##..#.#.#....#....#
..#.#..#..#..#....#..####....#.#....##............#..#...##..#.#.#..#..#
####..#.##.....#..#........#.#......#...#..###.......##..#..............
.....

output:

-1

result:

ok single line: '-1'

Test #59:

score: 0
Accepted
time: 0ms
memory: 5540kb

input:

100 100
....................................................................................................
.........#..........................................................................................
.........................................................................................#...

output:

-1

result:

ok single line: '-1'

Test #60:

score: 0
Accepted
time: 2ms
memory: 4876kb

input:

100 100
...........#..........................##...#....#.#..............##.....#.#..........#.......#...#..
.....#.....#...#...........#....##............#.....##................#..##.......###...#...........
..#......##.....#.#.#.....#...#.#.....#..#........................#.#.#........#.#..###..#...

output:

-1

result:

ok single line: '-1'

Test #61:

score: 0
Accepted
time: 1ms
memory: 5996kb

input:

100 100
................##.....#........................................#...##.......#...#..S...##.......U..
...#.....#..................................#.............###......................#................
.........#....#......#.....................................#......#.............#............

output:

-1

result:

ok single line: '-1'

Test #62:

score: 0
Accepted
time: 4ms
memory: 6756kb

input:

100 100
..........................#........#..............#.........##.#.#....#....#...#....................
.......................#.................#..##..................#.....................#..#.#........
...........#....................#....................#....#...................#..............

output:

17

result:

ok single line: '17'

Test #63:

score: 0
Accepted
time: 1ms
memory: 6904kb

input:

100 100
.......................................U............................................................
............................................................................##......................
.............................................................#...............................

output:

42

result:

ok single line: '42'

Test #64:

score: 0
Accepted
time: 0ms
memory: 8064kb

input:

100 100
....................................................................................................
....................................................................................................
.............................................................................................

output:

10

result:

ok single line: '10'

Test #65:

score: 0
Accepted
time: 5ms
memory: 8036kb

input:

100 100
#........................................#.......................#..................................
..............................................#......#..................#......................#....
....#...........#................#......#....................................................

output:

10

result:

ok single line: '10'

Test #66:

score: 0
Accepted
time: 2ms
memory: 6932kb

input:

100 100
...##.##......##.........#....###...#.#..#.#..##..###..#..#.#..#...#.#.........#.#.#....#.#..#.#...T
...#.#..##.#..#.#.................#..............#.#.....#........##....#....#..........#..#.#......
#.#.....#...#...#..##...................#.#..#..##.#......#..#........#.#....#...#...#.......

output:

3

result:

ok single line: '3'

Test #67:

score: 0
Accepted
time: 5ms
memory: 7568kb

input:

100 100
.............................#.......................#..............................................
...............................................................................#....................
......#...###.......................................................##............#..........

output:

6

result:

ok single line: '6'

Test #68:

score: 0
Accepted
time: 4ms
memory: 6936kb

input:

100 100
.......#......................#.....#.............#.....T.......#.............#.....................
.....................................#..............................#...................#.......##..
................................#....#........................#..............................

output:

7

result:

ok single line: '7'

Test #69:

score: 0
Accepted
time: 3ms
memory: 6512kb

input:

100 100
S..................................................................................................T
....................................................................................................
.............................................................................................

output:

9902

result:

ok single line: '9902'

Test #70:

score: 0
Accepted
time: 3ms
memory: 6596kb

input:

100 100
S..................................................................................................T
....................................................................................................
.............................................................................................

output:

9506

result:

ok single line: '9506'

Test #71:

score: 0
Accepted
time: 3ms
memory: 6520kb

input:

100 100
S..................................................................................................T
....................................................................................................
.............................................................................................

output:

2280

result:

ok single line: '2280'

Test #72:

score: 0
Accepted
time: 0ms
memory: 7252kb

input:

100 100
S..................................................................................................T
....................................................................................................
.............................................................................................

output:

1470

result:

ok single line: '1470'

Test #73:

score: 0
Accepted
time: 0ms
memory: 6076kb

input:

100 100
S..................................................................................................T
....................................................................................................
.............................................................................................

output:

102

result:

ok single line: '102'

Test #74:

score: 0
Accepted
time: 1ms
memory: 3888kb

input:

2 3
STU
###
2
1 1 r
1 2 r

output:

-1

result:

ok single line: '-1'

Test #75:

score: 0
Accepted
time: 1ms
memory: 3916kb

input:

2 4
ST.U
####
3
1 1 r
1 2 r
1 3 r

output:

2

result:

ok single line: '2'

Test #76:

score: 0
Accepted
time: 1ms
memory: 4148kb

input:

3 5
S.U##
##.##
..T..
4
1 1 r
1 2 r
1 3 b
2 3 b

output:

-1

result:

ok single line: '-1'

Test #77:

score: 0
Accepted
time: 1ms
memory: 3864kb

input:

10 10
......S...
..........
##........
.....#....
.....#..##
.........T
U.........
..........
....#.....
....#.....
30
6 9 r
3 7 r
4 8 b
5 8 b
2 6 b
3 3 b
7 5 b
4 1 b
4 3 b
6 7 b
7 6 r
6 9 b
3 7 b
6 6 r
4 4 r
4 5 b
8 5 r
4 7 b
3 4 r
5 7 b
6 8 b
7 6 b
4 4 b
4 2 b
2 5 b
6 5 r
2 7 b
3 4 b
7 5 r
8 4 r

output:

21

result:

ok single line: '21'

Test #78:

score: 0
Accepted
time: 0ms
memory: 3860kb

input:

10 10
..........
..........
..........
..........
..........
.....#....
##....#...
U...#..#..
####......
ST........
25
8 9 b
7 9 r
9 8 r
6 4 r
10 1 r
3 5 b
6 9 b
9 6 b
7 9 b
4 4 r
9 8 b
4 6 r
9 5 r
8 10 b
6 4 b
5 4 r
9 10 b
7 3 r
5 6 r
6 8 b
7 4 b
8 9 r
9 9 r
3 6 b
9 7 b

output:

21

result:

ok single line: '21'