Submission #860544

#TimeUsernameProblemLanguageResultExecution timeMemory
860544NotLinuxMaze (JOI23_ho_t3)C++17
86 / 100
2063 ms1240444 KiB
#include <bits/stdc++.h>
using namespace std;
void solve(){
	int r,c,n;
	cin >> r >> c >> n;	
	pair < int , int > s,g;
	cin >> s.first >> s.second >> g.first >> g.second;
	s.first--;s.second--;g.first--;g.second--;
	int grid[r][c];
	set < int > hor[r] , ver[c];
	for(int i = 0;i<r;i++){//1 = # , 0 = .
		string str;cin >> str;
		for(int j = 0;j<c;j++){
			grid[i][j] = str[j] == '#';
			if(i != s.first or j != s.second){
				ver[j].insert(i);
				hor[i].insert(j);
			}
		}
	}
	queue < pair < int , int > > bfs[r+c+1];
	bfs[0].push(s);
	vector < vector < int > > vis(r , vector < int > (c,0));

	function < void (int , int , int) > psh = [&](int dist , int x , int y){
		if(x < 0 or x >= r or y < 0 or y >= c)return;
		//cout << "spread : " << dist << " " << x << " " << y << endl;
		if(vis[x][y] == 0){
			//cout << "succ" << endl;
			bfs[dist].push({x,y});
			hor[x].extract(y);
			ver[y].extract(x);
		}
	};

	for(int curdist = 0;curdist < (r+c+1);curdist++){
		while(bfs[curdist].size()){
			pair < int , int > p = bfs[curdist].front();
			bfs[curdist].pop();
			if(vis[p.first][p.second])continue;
			//cout << "bfs : " << p.first << " " << p.second << " " << curdist << endl;
			vis[p.first][p.second] = 1;
			if(p == g){
				cout << curdist << '\n';
				return;
			}
			//spread
			if((p.first+n) < r){//	({n,-n} , {n,n})
				auto lb = hor[p.first + n].upper_bound(p.second - n);
				auto ub = hor[p.first + n].lower_bound(p.second + n);
				if(lb != hor[p.first + n].end() and ub != hor[p.first + n].begin()){
					vector < int > willdel;
					for(auto it = lb;it != ub;++it){
						willdel.push_back(*it);
					}
					for(auto itr : willdel){
						psh(curdist+1,p.first+n,itr);
					}
				}
			}
			if((p.first - n) >= 0){//	({-n,-n} , {-n,n})
				auto lb = hor[p.first - n].upper_bound(p.second - n);
				auto ub = hor[p.first - n].lower_bound(p.second + n);
				if(lb != hor[p.first - n].end() and ub != hor[p.first - n].begin()){
					vector < int > willdel;
					for(auto it = lb;it != ub;++it){
						willdel.push_back(*it);
					}
					for(auto itr : willdel){
						psh(curdist+1,p.first-n,itr);
					}
				}
			}
			if((p.second + n) < c){//	({-n,n} , {n,n})
				auto lb = ver[p.second + n].upper_bound(p.first - n);
				auto ub = ver[p.second + n].lower_bound(p.first + n);
				if(lb != ver[p.second + n].end() and ub != ver[p.second + n].begin()){
					vector < int > willdel;
					for(auto it = lb;it != ub;++it){
						willdel.push_back(*it);
					}
					for(auto itr : willdel){
						psh(curdist+1,itr,p.second+n);
					}
				}
			}
			if((p.second - n) >= 0){//	({-n,-n} , {n,-n})
				auto lb = ver[p.second - n].upper_bound(p.first - n);
				auto ub = ver[p.second - n].lower_bound(p.first + n);
				if(lb != ver[p.second - n].end() and ub != ver[p.second - n].begin()){
					vector < int > willdel;
					for(auto it = lb;it != ub;++it){
						willdel.push_back(*it);
					}
					for(auto itr : willdel){
						psh(curdist+1,itr,p.second-n);
					}
				}
			}
			if(p.first != 0 and grid[p.first-1][p.second] == 0)psh(curdist,p.first-1,p.second);
			if(p.second != 0 and grid[p.first][p.second-1] == 0)psh(curdist,p.first,p.second-1);
			if(p.first != (r-1) and grid[p.first+1][p.second] == 0)psh(curdist,p.first+1,p.second);
			if(p.second != (c-1) and grid[p.first][p.second+1] == 0)psh(curdist,p.first,p.second+1);
			if((abs(g.first - p.first) <= n and abs(g.second - p.second) <= (n-1)) or (abs(g.first - p.first) <= (n-1) and abs(g.second - p.second) <= n)){
				psh(curdist+1,g.first,g.second);
			}
		}
	}
	cout << -1 << '\n';
}
signed main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int testcase = 1;//cin >> testcase;
	while(testcase--)solve();
	//cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...