QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#77198#4856. Puzzle: Patrick's ParaboxJinTianHaoWA 33ms3980kbC++173.7kb2023-02-13 11:24:092023-02-13 11:24:12

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-13 11:24:12]
  • 评测
  • 测评结果:WA
  • 用时:33ms
  • 内存:3980kb
  • [2023-02-13 11:24:09]
  • 提交

answer

#include <bits/stdc++.h>
#define inf 1000000007
#define mod 1000000007
// #define int long long
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
using namespace std;
template <typename T> void read(T &x){
	x=0;char ch=getchar();int fh=1;
	while (ch<'0'||ch>'9'){if (ch=='-')fh=-1;ch=getchar();}
	while (ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	x*=fh;
}
template <typename T> void write(T x) {
	if (x<0) x=-x,putchar('-');
	if (x>9) write(x/10);
	putchar(x%10+'0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
const int dx[]={-1,1,0,0},dy[]={0,0,-1,1};
int wx[4],wy[4];
int n,m,t;
int spx,spy,sbx,sby,tpx,tpy,tbx,tby;
char mp[500005];
namespace bp1
{
	int dist[5005][5005];
	int get(int x,int y)
	{
		return (x-1)*m+y;
	}
	void solve()
	{
		wx[0]=n;wy[0]=(m+1)/2;
		wx[1]=1;wy[1]=(m+1)/2;
		wx[2]=(n+1)/2;wy[2]=m;
		wx[3]=(n+1)/2;wy[3]=1;
		for(int i=1;i<=n*m;++i)
			for(int j=1;j<=n*m;++j)
				dist[i][j]=inf;
		dist[get(spx,spy)][get(sbx,sby)]=0;
		deque<pair<pair<int,int>,pair<int,int>>> q;
		q.push_back({{spx,spy},{sbx,sby}});
		while(!q.empty())
		{
			auto p=q.front();q.pop_front();
			int px=p.first.first,py=p.first.second;
			int bx=p.second.first,by=p.second.second;
			for(int d=0;d<4;++d)
			{
				int pxx=px+dx[d],pyy=py+dy[d];
				int bxx=bx+dx[d],byy=by+dy[d];
				if(pxx==bx&&pyy==by&&bxx>=1&&bxx<=n&&byy>=1&&byy<=m&&mp[get(bxx,byy)]=='.'&&dist[get(pxx,pyy)][get(bxx,byy)]>dist[get(px,py)][get(bx,by)]+1)
				{
					dist[get(pxx,pyy)][get(bxx,byy)]=dist[get(px,py)][get(bx,by)]+1;
					q.push_back({{pxx,pyy},{bxx,byy}});
				}
				else if(pxx>=1&&pxx<=n&&pyy>=1&&pyy<=m&&mp[get(pxx,pyy)]=='#')
				{
					continue;
				}
				else if(pxx>=1&&pxx<=n&&pyy>=1&&pyy<=m&&mp[get(pxx,pyy)]=='.'&&(pxx!=bx||pyy!=by)&&dist[get(pxx,pyy)][get(bx,by)]>dist[get(px,py)][get(bx,by)])
				{
					dist[get(pxx,pyy)][get(bx,by)]=dist[get(px,py)][get(bx,by)];
					q.push_front({{pxx,pyy},{bx,by}});
				}
				else if(pxx==bx&&pyy==by&&(bxx<1||bxx>n||byy<1||byy>m))
				{
					continue;
				}
				else if(pxx==bx&&pyy==by&&bxx>=1&&bxx<=n&&byy>=1&&byy<=m&&mp[get(bxx,byy)]=='#'&&mp[get(wx[d],wy[d])]=='#')
				{
					continue;
				}
				else if(pxx==bx&&pyy==by&&bxx>=1&&bxx<=n&&byy>=1&&byy<=m&&mp[get(bxx,byy)]=='#'&&mp[get(wx[d],wy[d])]=='.'&&dist[get(wx[d],wy[d])][get(bx,by)]>dist[get(px,py)][get(bx,by)])
				{
					dist[get(wx[d],wy[d])][get(bx,by)]=dist[get(px,py)][get(bx,by)];
					q.push_front({{wx[d],wy[d]},{bx,by}});
				}
				else if((pxx<1||pxx>n||pyy<1||pyy>m)&&bxx>=1&&bxx<=n&&byy>=1&&byy<=m&&mp[get(bxx,byy)]=='#')
				{
					continue;
				}
				else if((pxx<1||pxx>n||pyy<1||pyy>m)&&(bxx<1||bxx>n||byy<1||byy>m))
				{
					continue;
				}
				else if((pxx<1||pxx>n||pyy<1||pyy>m)&&bxx>=1&&bxx<=n&&byy>=1&&byy<=m&&mp[get(bxx,byy)]=='.'&&dist[get(bxx,byy)][get(bx,by)]>dist[get(px,py)][get(bx,by)])
				{
					dist[get(bxx,byy)][get(bx,by)]=dist[get(px,py)][get(bx,by)];
					q.push_front({{bxx,byy},{bx,by}});
				}
			}
		}
		writeln(dist[get(tpx,tpy)][get(tbx,tby)]==inf?-1:dist[get(tpx,tpy)][get(tbx,tby)]);
	}
}
signed main()
{
	// freopen("parabox.in","r",stdin);
	// freopen("parabox.out","w",stdout);
	int tc;read(tc);
	while(tc--)
	{
		read(n);read(m);t=0;
		for(int i=1;i<=n;++i)
		{
			scanf("%s",mp+t+1);
			for(int j=1;j<=m;++j)
				if(mp[t+j]=='p')
					spx=i,spy=j,mp[t+j]='.';
				else if(mp[t+j]=='b')
					sbx=i,sby=j,mp[t+j]='.';
				else if(mp[t+j]=='=')
					tpx=i,tpy=j,mp[t+j]='.';
				else if(mp[t+j]=='-')
					tbx=i,tby=j,mp[t+j]='.';
			t+=m;
		}
		if(n*m<=5000) bp1::solve();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3980kb

input:

3
9 9
#########
#####..-#
#..=##.##
#.p.##.##
....##.##
#...b..##
#...##.##
#....####
####.####
9 9
#########
#.......#
#.#####.#
#.#=....#
..#....-#
###.p.#.#
#.....#b#
#.....#.#
####.####
9 9
####.####
#....####
#.####.##
#.......#
#.......#
###.#####
#=.b#..##
#-..p..##
#########

output:

7
4
19

result:

ok 3 number(s): "7 4 19"

Test #2:

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

input:

1
2 2
pb
-=

output:

-1

result:

ok 1 number(s): "-1"

Test #3:

score: 0
Accepted
time: 33ms
memory: 3424kb

input:

100000
2 2
-p
=b
2 2
b-
=p
2 2
-b
p=
2 2
-p
b=
2 2
p=
-b
2 2
b=
-p
2 2
=b
-p
2 2
b=
-p
2 2
-p
b=
2 2
b=
-p
2 2
p=
b-
2 2
pb
=-
2 2
p-
=b
2 2
-=
pb
2 2
=p
-b
2 2
p=
-b
2 2
-b
=p
2 2
=p
-b
2 2
=b
-p
2 2
=-
bp
2 2
-=
bp
2 2
bp
=-
2 2
=-
bp
2 2
=b
-p
2 2
bp
-=
2 2
-b
p=
2 2
=p
b-
2 2
b-
p=
2 2
pb
=-
2 2...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

ok 100000 numbers

Test #4:

score: -100
Wrong Answer
time: 1ms
memory: 3596kb

input:

1
500 200
#....#.#.#.##.#..#.#..#......#...#..#...#.#...........#.#.#..#...###..##..#.#.#.....#........#..####..#..#..#####...............#.#......#.##.#..##.........##.###.#....###..#...#.#...#...##..##.#...#.
##..##.....#.##..#.#.###.#....#...#..#.#....##..............##.....#...#....#..##.##...##...

output:


result:

wrong answer Answer contains longer sequence [length = 1], but output contains 0 elements