BFS广搜——hdu 4255 A Famous Grid

点击打开链接


                                    A Famous Grid

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 582    Accepted Submission(s): 226


Problem Description
Mr. B has recently discovered the grid named "spiral grid".
Construct the grid like the following figure. (The grid is actually infinite. The figure is only a small part of it.)

Considering traveling in it, you are free to any cell containing a composite number or 1, but traveling to any cell containing a prime number is disallowed. You can travel up, down, left or right, but not diagonally. Write a program to find the length of the shortest path between pairs of nonprime numbers, or report it's impossible.

Input
Each test case is described by a line of input containing two nonprime integer 1 <=x, y<=10,000.
 
Output
For each test case, display its case number followed by the length of the shortest path or "impossible" (without quotes) in one line.
 
Sample Input
  
  
1 4 9 32 10 12
 
Sample Output
  
  
Case 1: 1 Case 2: 7 Case 3: impossible

题目意思:

有这样的一个蛇形数组,输入两个数据,为起点和终点。问起点到终点的步数,且当矩阵里的点

为素数的时候不可通。

1 <=x, y<=10,000. 的范围,注意建矩阵的大小要大于 100* 100 。

一个很明显的 BFS 题目。首先打印出矩阵,对矩阵里的素数进行判断 ok[ ][ ] 数组存储。然后

对起始点进行广搜就行了。


#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
#include<cmath>
using namespace std;
#define M 120

typedef struct data //记录坐标与步数
{
	int x,y,step;
}DATA;

int map[M][M];
int ok[M][M],vis[M][M];
int start,end;

int dx[4]={0,-1, 0,1};
int dy[4]={1, 0,-1,0};

void build() //建图大小120*120 (蛇形数组)
{
	int tot,x,y;
	memset(map,0,sizeof(map));
	tot=map[x=0][y=0]=14400;
	while(tot>1)
	{
		while(y+1<M && !map[x][y+1])  // 右走
			map[x][++y]=--tot;
		while(x+1<M && !map[x+1][y])  // 下走
			map[++x][y]=--tot;
		while(y-1>=0 && !map[x][y-1]) // 左走
			map[x][--y]=--tot;
		while(x-1>=0 && !map[x-1][y]) // 上走
			map[--x][y]=--tot;
	}
}

int is_prime(int n)
{
	int i;
	if(n==1) return 0;
	if(n==2||n==3) return 1;
	for(i=2;i<=sqrt(n);i++)
		if(n%i==0)
			return 0;
	return 1;
}

void init() //判断是否可通,通为0 ,不通为 1
{
	int i,j;
	for(i=0;i<M;i++)
		for(j=0;j<M;j++)
			ok[i][j]=is_prime(map[i][j]);
}

int isThrough(int x,int y)
{
	if(x>=0 && x<M && y>=0 && y<M && !vis[x][y] && !ok[x][y])
		return 1;
	else 
		return 0;
}

int BFS(DATA s)
{
	int i,x,y;
	DATA cur,next;
	queue<DATA>que;
	if(is_prime(start)||is_prime(end)) //起始点为素数直接返回
		return -1;
	if(start==end)
		return 0;
	que.push(s);
	while(!que.empty())
	{
		cur=que.front();
		que.pop();
		for(i=0;i<4;i++) //dx[i],dy[i] 上下左右四个方向
		{
			x=cur.x+dx[i];y=cur.y+dy[i];
			if(isThrough(x,y))
			{
				next.x=x;next.y=y;next.step=cur.step+1; 
				que.push(next);
				vis[x][y]=1;
				if(map[x][y]==end)
					return cur.step+1;
			}
		}
	}
	return -1;
}

int main()
{
	int i,j,count=1,ans,flag;
	DATA begin;
	build();
	init();
	while(cin>>start>>end)
	{
		memset(vis,0,sizeof(vis));
		begin.step=0;
		for(flag=i=0;i<M;i++)
		{
			for(j=0;j<M;j++)
				if(map[i][j]==start)
				{begin.x=i;begin.y=j;flag=1;break;}
			if(flag) break;
		}
		ans=BFS(begin);
		if(ans==-1)
			printf("Case %d: %s\n",count++,"impossible");
		else
			printf("Case %d: %d\n",count++,ans);
	}
	return 0;
}









  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值