Submission details
Task:Jelly Raid
Sender:Game of Nolife
Submission time:2015-11-25 19:25:41 +0200
Language:C++
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.06 sdetails
#2ACCEPTED0.05 sdetails
#3ACCEPTED0.06 sdetails
#4ACCEPTED0.06 sdetails
#5ACCEPTED0.04 sdetails
#6ACCEPTED0.06 sdetails
#7ACCEPTED0.06 sdetails

Code

#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef long double ld;

const int gmod=120;

char no[gmod+10][66][66];
string ma[66];

int get1(string s){
	if (s.size()==2){
		return s[1]-'0';
	}
	else{
		return (s[1]-'0')*10+(s[2]-'0');
	}
}

int get2(string s){
	if (s.size()==2){
		return s[0]-'0';
	}
	else{
		return (s[0]-'0')*10+(s[1]-'0');
	}
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n,m;
	cin>>n>>m;
	string lol;
	cin>>lol;
	int y1=get1(lol);
	cin>>lol;
	int x1=get2(lol);
	cin>>lol;
	int y2=get1(lol);
	cin>>lol;
	int x2=get2(lol);
	x1--;
	y1--;
	x2--;
	y2--;
	for (int i=0;i<n;i++){
		cin>>ma[i];
	}
	int p;
	cin>>p;
	for (int i=0;i<p;i++){
		int pp;
		cin>>pp;
		vector<pair<int, int> > asd(pp);
		for (int j=0;j<pp;j++){
			string lol1,lol2;
			cin>>lol1>>lol2;
			asd[j]={get1(lol1)-1, get2(lol2)-1};
		}
		int mod=pp+pp-2;
		for (int j=0;j<gmod;j++){
			pair<int, int> tp;
			if (mod==0){
				tp=asd[0];
			}
			else{
				if (j%mod>=pp){
					tp=asd[pp+pp-2-(j%mod)];
				}
				else{
					tp=asd[j%mod];
				}
			}
			//cout<<j<<" "<<tp.F<<" "<<tp.S<<endl;
			for (int y=tp.F;;y++){
				if (y>=n||ma[y][tp.S]=='#') break;
				no[j][y][tp.S]=1;
			}
			for (int y=tp.F;;y--){
				if (y<0||ma[y][tp.S]=='#') break;
				no[j][y][tp.S]=1;
			}
			for (int x=tp.S;;x++){
				if (x>=m||ma[tp.F][x]=='#') break;
				no[j][tp.F][x]=1;
			}
			for (int x=tp.S;;x--){
				if (x<0||ma[tp.F][x]=='#') break;
				no[j][tp.F][x]=1;
			}
		}
	}
	if (x1==x2&&y1==y2){
		cout<<0<<endl;
		return 0;
	}
	if (no[0][y1][x1]) {
		cout<<"IMPOSSIBLE"<<endl;
		return 0;
	}
	queue<pair<int, pair<int, int> > > bfs;
	bfs.push({0, {y1, x1}});
	no[0][y1][x1]=1;
	while (!bfs.empty()){
		auto t=bfs.front();
		bfs.pop();
		//cout<<t.F<<" "<<t.S.F<<" "<<t.S.S<<endl;
		for (int y=-1;y<=1;y++){
			for (int x=-1;x<=1;x++){
				if ((y==0)||(x==0)){
					if (t.S.F+y>=0&&t.S.F+y<n&&t.S.S+x>=0&&t.S.S+x<m){
						if (ma[t.S.F+y][t.S.S+x]=='.'){
							if (t.S.F+y==y2&&t.S.S+x==x2){
								cout<<t.F+1<<endl;
								return 0;
							}
							if (!no[(t.F+1)%gmod][t.S.F+y][t.S.S+x]){
								no[(t.F+1)%gmod][t.S.F+y][t.S.S+x]=1;
								bfs.push({t.F+1, {t.S.F+y, t.S.S+x}});
							}
						}
					}
				}
			}
		}
	}
	cout<<"IMPOSSIBLE"<<endl;
}

Test details

Test 1

Verdict: ACCEPTED

input
60 3
(1 1) (54 3)
...
#..
...
...

correct output
3242

user output
3242

Test 2

Verdict: ACCEPTED

input
60 60
(1 1) (57 59)
.................................

correct output
1683

user output
1683

Test 3

Verdict: ACCEPTED

input
60 60
(1 1) (57 59)
...#...#.......#...#...#...#.....

correct output
2643

user output
2643

Test 4

Verdict: ACCEPTED

input
60 60
(1 1) (57 59)
...#...#...#...#...#...#...#.....

correct output
47043

user output
47043

Test 5

Verdict: ACCEPTED

input
20 60
(10 1) (10 60)
...#...#...#...#...#.............

correct output
70

user output
70

Test 6

Verdict: ACCEPTED

input
60 60
(1 1) (57 59)
...#...#...#...#...#...#...#.....

correct output
27483

user output
27483

Test 7

Verdict: ACCEPTED

input
60 60
(2 2) (58 40)
.................................

correct output
132

user output
132