2018-2019ACM焦作区域赛F - Honeycomb Gym -102028F(BFS)

20 篇文章 0 订阅
8 篇文章 0 订阅

题干:

time limit per test:4.0 s
memory limit per test:1024 MB
cells built by honey bees, which can be described as a regular tiling of the Euclidean plane, in which three hexagons meet at each internal vertex. The internal angle of a hexagon is 120 degrees, so three hexagons at a point make a full 360360 degrees. The following figure shows a complete honeycomb with 3 rows and 4 columns.
在这里插入图片描述
Here we guarantee that the first cell in the second column always locates in the bottom right side of the first cell in the first column, as shown above. A general honeycomb may, on the basis of a complete honeycomb, lose some walls between adjacent cells, but the honeycomb is still in a closed form. A possible case looks like the figure below.
在这里插入图片描述
Hamilton is a brave bee living in a general honeycomb. Now he wants to move from a starting point to a specified destination. The image below gives a feasible path in a 3×4 honeycomb from the 1-st cell in the 22nd column to the 1-st cell in the 4-th column.
在这里插入图片描述
Please help him find the minimum number of cells that a feasible path has to pass through (including the starting point and the destination) from the specified starting point to the destination.

Input
The input contains several test cases, and the first line contains a positive integer TT indicating the number of test cases which is up to 1 0 4 10^4 104.

For each test case, the first line contains two integers rr and cc indicating the number of rows and the number of columns of the honeycomb, where 2≤r,c≤ 1 0 3 10^3 103.

The following (4r+3)(4r+3) lines describe the whole given honeycomb, where each line contains at most (6c+3)(6c+3) characters. Odd lines contain grid vertices represented as plus signs ("+") and zero or more horizontal edges, while even lines contain two or more diagonal edges. Specifically, a cell is described as 66 vertices and at most 66 edges. Its upper boundary or lower boundary is represented as three consecutive minus signs ("-"). Each one of its diagonal edges, if exists, is a single forward slash ("/") or a single backslash ("") character. All edge characters will be placed exactly between the corresponding vertices. At the center of the starting cell (resp. the destination), a capital “S” (resp. a capital “T”) as a special character is used to indicate the special cell. All other characters will be space characters. Note that if any input line could contain trailing whitespace, that whitespace will be omitted.

We guarantee that all outermost wall exist so that the given honeycomb is closed, and exactly one “S” and one “T” appear in the given honeycomb. Besides, the sum of r⋅cr⋅c in all test cases is up to 2× 1 0 6 10^6 106

Output
For each test case, output a line containing the minimum number of cells that Hamilton has to visit moving from the starting cell (“S”) to the destination (“T”), including the starting cell and the destination. If no feasible path exists, output -1 instead.

Example

intput:
1
3 4
  +---+       +---+
 /     \     /     \
+       +---+       +---+
 \           \     /     \
  +   +   S   +---+   T   +
 /     \     /           /
+       +---+       +   +
 \           \     /     \
  +---+       +---+       +
 /                       /
+       +---+       +   +
 \                 /     \
  +---+       +---+       +
       \     /     \     /
        +---+       +---+
output:
7

思路:

总而言之就是走蜂巢,从S蜂巢走到T蜂巢,求经过的最少蜂巢数。
本题的难点在于如何对蜂巢建图,但是我们发现每个蜂巢的中心点是可以求出来的,并且中心点与中心点之间的间隔是一定的(比如上下两个蜂巢只在纵坐标上差4)。
这样我们规定每个方向走的距离,又因为S、T都在蜂巢的正中间,所以我们每一次走一半,看看这里有没有边,如果没有则走完另一半。
所以直接从S跑BFS就完事了。
然后注意下边界,观察可得字符串有4n+3行,6n+3列,数组记得开大点就行。。。

#include <bits/stdc++.h>
using namespace std;
char ch[11000][11000];
int vis[11000][11000];
int sx,ex,sy,ey,n,m;
struct stu{
	int x,y,num;
};
int d[6][2]={{1,3},{-1,3},{1,-3},{-1,-3},{2,0},{-2,0}};
bool check(int x,int y){
	if(x<0||y<0||x>=n||y>=m)
		return false;
	else
		return true;
}
int bfs()
{
	stu t,temp;
	t.x=sx;t.y=sy;t.num=1;
	queue<stu> q;
	q.push(t);
	vis[sx][sy]=0;
	while(!q.empty()){
		t=q.front();
		q.pop();
		if(t.x==ex&&t.y==ey)
			return t.num;
		for(int i=0;i<6;i++){
			int tx=t.x+d[i][0],ty=t.y+d[i][1];
			if(check(tx,ty)&&ch[tx][ty]==' '){
				int ttx=tx+d[i][0],tty=ty+d[i][1];
				if(check(ttx,tty)&&t.num+1<vis[ttx][tty]){
					temp.x=ttx;temp.y=tty;temp.num=t.num+1;
					q.push(temp);
					vis[ttx][tty]=temp.num;
					//printf("%d %d\n",temp.x,temp.y);
				}
			}
		}
	}
	return -1;
}
int main()
{
	//for(int i=0;i<6;i++)
		//printf("%d %d\n",d[i][0],d[i][1]);
    int t,t1;
    scanf("%d",&t1);
    while(t1--){
    	scanf("%d%d",&n,&m);
    	n=4*n+3;
    	m=6*m+3;
    	getchar();
    	for(int i=0;i<n;i++){
    		gets(ch[i]);
    		for(int j=0;ch[i][j]!='\0';j++){
    			vis[i][j]=0x3f3f3f3f;
    			if(ch[i][j]=='S'){
    				sx=i;
    				sy=j;
    			}
    			else if(ch[i][j]=='T'){
    				ex=i;
    				ey=j;
    			}
    		}
    	}
    	//printf("%d %d %d %d\n",sx,sy,ex,ey);
    	printf("%d\n",bfs());
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值