2018 ICPC焦作 F 【模擬+bfs】

#include <bits/stdc++.h>
using namespace std;
char s[4005][6105];
typedef pair<int,int> PII;
map<PII,int>mp;
int tot=0;
int M,N;
const int maxn = 2e6+50;
const int inf = 0x3f3f3f3f;
vector<int>G[maxn]; 
void addedge(int x,int y)
{
	G[x].push_back(y);
	G[y].push_back(x);
}
int vis[maxn];
int d[maxn];
int bfs(int s,int t)
{
	for(int i=0;i<=tot+1;i++) vis[i] = 0,d[i] = inf;
	d[s] = 1;
	queue<int>que;
	que.push(s);
	vis[s]=1;
	while(!que.empty())
	{
		int t = que.front();
		//cout<<t<<" "<<d[t]<<endl;
		que.pop();
		for(int i=0;i<G[t].size();i++)
		{
			int v = G[t][i];
			if(v == t) return d[v] = min(d[v],d[t]+1);
			if(!vis[v])
			{
				d[v] =min(d[v],d[t]+1);
				vis[v]=1;
				que.push(v);
			}
		}
	}
}
void check(PII t)
{
	int x = t.first;
	int y = t.second;
	int ux = x-4;
	int uy = y;
	PII tmp;
	if(x-4>=1) 
	{
		if(x==9&&y==23)
		//cout<<"..."<<x-2<<" "<<y<<" "<< s[x-2][y]<<endl;
		if(s[x-2][y]!='-') 
	{
		tmp.first = ux;
		tmp.second = uy;
		if(x==9&&y==23)
		//cout<<"8888"<<mp[tmp]<<" "<<mp[t]<<endl;
		addedge(mp[tmp],mp[t]);
	}
	}
	ux = x+4;
	tmp.first = ux; tmp.second= uy; 
	if(ux<=N)
	{
		if(s[x+2][y]==' ')
		{
			addedge(mp[tmp],mp[t]);
		}
	} 
	uy = y-6;
	ux = x-2;
	tmp.first = ux; tmp.second= uy;
	if(uy>=1&&ux>=1)
	{
		if(s[x-1][y-3]==' ') addedge(mp[tmp],mp[t]);
	}
	ux = x+2;
	uy = y-6;
	tmp.first = ux; tmp.second= uy;
	if(x<N&&y>=1)
	{
		if(s[x+1][y-3]==' ') addedge(mp[tmp],mp[t]);
	}
	ux = x-2;
	uy = y+6;
	tmp.first = ux; tmp.second= uy;
	if(x>1&&y<M)
	{
		if(s[x-1][y+3]==' ') addedge(mp[tmp],mp[t]);	
	}
	ux = x+2;
	uy = y+6;
	tmp.first = ux; tmp.second= uy;
	if(x<N&&y<M)
	{
		if(s[x+1][y+3]==' ') addedge(mp[tmp],mp[t]);
	}
	
} 
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,m;
		tot=0;
		scanf("%d%d",&n,&m);
		N = 4*n+3;
		M = 6*m+3;
		PII st,ed;
		getchar();
		mp.clear();
		for(int i=1;i<=n*4+3;i++)
		{
			gets(&s[i][1]);
		//	cout<<i<<endl;
		}
		for(int i=1;i<=n*4+3;i++)
		{
			for(int j=1;j<=m*6+3;j++)
			{
				if(s[i][j]=='S') 
				{
					st.first = i; st.second = j;
				}
				if(s[i][j]=='T')
				{
					ed.first = i; ed.second = j;
				}
			}
		}
		//cout<<st.first<<" "<<st.second<<endl;
		//cout<<ed.first<<" "<<ed.second<<endl;
		PII x;
		x.first = 3; x.second = 5;
		mp[x] = ++tot;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=(m+1)/2;j++)
			{
				PII tmp;
				tmp.first = x.first + (i-1)*4;
				tmp.second = x.second +(j-1)*12; 
				//cout<<tmp.first<<" "<<tmp.second<<" ";
				if(mp.count(tmp)==0)
				{
					mp[tmp] = ++tot;
				}
				//cout<<"id =" <<mp[tmp]<<endl;
			 } 
		}
		//cout<<tot<<endl;
		PII y; y.first =5; y.second = 11;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=(m/2);j++)
			{
				PII tmp; 
				tmp.first = y.first + (i-1)*4;
				tmp.second = y.second + (j-1)*12;
			//	cout<<tmp.first<<" "<<tmp.second<<" ";
				if(mp.count(tmp)==0)
				{
					mp[tmp] = ++tot;
				}
			//	cout<<"id = "<<mp[tmp]<<endl;
				
			}
		}
	//	cout<<tot<<endl;
		for(int i=0;i<=tot;i++)
		{
			G[i].clear();
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				if(j%2)
				{
					PII cent;
					cent.first = (i-1)*4+3;
					cent.second = (j-1)*6+5;
					check(cent);
				}
				else 
				{
					PII cent;
					cent.first = (i-1)*4+5;
					cent.second = (j-2)*6+11;
				//	cout<<cent.first<<" "<<cent.second<<endl;
					check(cent);
				}
			}
		}
	//	cout<<mp[st]<<" "<<mp[ed]<<endl;
		//for(int i=1;i<=tot;i++) cout<<d[i]<<" ";
		//cout<<endl;
		
		bfs(mp[st],mp[ed]);
		if(d[mp[ed]]>=3e6+10) cout<<-1<<endl;
		else 
		cout<<d[mp[ed]]<<endl;
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章