建边跑01BFS做法求调,玄关
查看原帖
建边跑01BFS做法求调,玄关
820574
time_keeper楼主2024/7/15 19:35

36pts36pts

WA ON #1...一大堆

#include<bits/stdc++.h>
using namespace std;
const int N = 505;
vector<pair<int,int>> e[N * N << 1];
int n,m;
pair<int,int> make_pair(int a,int b){
	pair<int,int> t;
	t.first = a,t.second = b;
	return t;
}
void f(int a,int b,char c){//建图 
	int ls = (a - 1) * (m + 1) + b;
	int rs = (a - 1) * (m + 1) + b + 1;
	int lx = a * (m + 1) + b;
	int rx = a * (m + 1) + b + 1;
	if(c == '/'){
		e[rs].push_back(make_pair(lx,0));
		e[lx].push_back(make_pair(rs,0));
		e[ls].push_back(make_pair(rx,1));
		e[rx].push_back(make_pair(ls,1));
	}else{
		e[ls].push_back(make_pair(rx,0));
		e[rx].push_back(make_pair(ls,0));
		e[rs].push_back(make_pair(lx,1));
		e[lx].push_back(make_pair(rs,1));
	}
}
bool vis[N * N << 1];
int main(){
	cin >> n >> m;
	string s;
	for(int i = 1;i <= n;i++){
		cin >> s;
		int tot = 0;
		for(char j : s){
			f(i,++tot,j);
		}
	}
	deque<pair<int,int>>q;
	q.push_back(make_pair(1,0));
	vis[1] = true;
	int ans = 1e9;
	while(!q.empty()){
		pair<int,int>t = q.front();
		q.pop_front();
		int u = t.first;
		int w = t.second;
		for(auto i : e[u]){
			int v = i.first;
			int ww = i.second;
			if(vis[v]) continue;
			vis[v] = true;
//			cout << v << " " << w << " " << ww <<endl; 
			if(w == 1)q.push_back(make_pair(v,w + ww));
			else q.push_front(make_pair(v,w + ww));
			if(v == (n + 1) * (m + 1) )ans = min(ans,w + ww);
		} 
	}
//	for(int i = 1;i <= (n + 1) * (m + 1);i++){
//		cout << "点" << i << "连边:"<< endl;
//		for(auto j : e[i]){
//			cout << "  " << j.first << " " << j.second << endl;
//		}
//	}
	cout << ans;
}
2024/7/15 19:35
加载中...