NYOJ 题目592 spiral grid (广搜+素数预处理+大表)

spiral grid

时间限制: 2000 ms  |  内存限制: 65535 KB
难度: 4
描述
Xiaod 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. In addition, traveling from a prime number is disallowed, either. 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.
输入
Each test case is described by a line of input containing two nonprime integer 1 <=x, y<=10,000.
输出
For each test case, display its case number followed by the length of the shortest path or "impossible" (without quotes) in one line.
样例输入
1 4
9 32
10 12
样例输出
Case 1: 1
Case 2: 7
Case 3: impossible

题意:给出一张100*100的蛇形地图,给出两个数据,问从第一个数据走到第二个数据需要多少步,不能走到素数格子,所以终点是素数的直接impossible

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
struct node{
	int x;
	int y;
	int step;
};
queue <struct node> q;
int map[105][105];
int vis[105][105];
int prime_0[10005];//素数是0 
int go[4][2]={1,0,0,1,-1,0,0,-1};
int n=100;
void initial(){//初始化,数组清空,队列清空 
	memset(vis,0,sizeof(vis));
	while(!q.empty()){
		q.pop();
	}
}
void find_prime(){//预处理,把素数先全都找到 
	prime_0[1]=1;
	int i,j;
	for(i=2;i<=10005;i++){
		if(prime_0[i]==0)
		for(j=i+i;j<=10005;j+=i)
		prime_0[j]=1;
	}
}
struct node locate(int aa){//找到一个数字的坐标 
	int i,j;
	struct node bb;
	bb.step=0;
	for(i=1;i<=100;i++)
	 for(j=1;j<=100;j++){
 		if(map[i][j]==aa){
		 	bb.x=i;
		 	bb.y=j;
		 	break;
		 }
 	}
	return bb;
}
void build_map(int maxn){//地图打表 
	int i,j;
	for(i=1;i<=n/2;i++){
		for(j=1;j<=n-2*i+2;j++)
			map[i][j+i-1]=maxn--;
		for(j=1;j<=n-2*i;j++)
			map[i+j][n-i+1]=maxn--;
		for(j=n-i+1;j>=i;j--)
		    map[n-i+1][j]=maxn--;
		for(j=n-i;j>=i+1;j--)
		    map[j][i]=maxn--;
	}
}

int bfs(int x1,int y1,int en){//广度搜索 
	int i;
	
	struct node e={x1,y1,0};
	q.push(e);
	vis[x1][y1]=1;
	while(!q.empty()){
		e=q.front();
		if(map[e.x][e.y]==en)break;
		q.pop();
		for(i=0;i<4;i++){
			int xx=e.x+go[i][0];
			int yy=e.y+go[i][1];
			if(xx>=1&&xx<=100&&yy>=1&&yy<=100&&vis[xx][yy]==0&&prime_0[map[xx][yy]]){
				struct node ee={xx,yy,e.step+1};
				q.push(ee);
				vis[xx][yy]=1;
			}
		}
	}
	if(q.empty())return -1;
	else return e.step;
}
int main(){
	int case_num=1;
	int i,j,num,st,en;
	build_map(n*n);
    find_prime();
	while(scanf("%d%d",&st,&en)!=EOF){
		if(!prime_0[en]){
			printf("Case %d: impossible\n",case_num++);
			continue;
		}//终点是素数,直接impossible 
	    struct node start=locate(st);//找到起点坐标 
	    initial();//初始化 
	    int ans=bfs(start.x,start.y,en);
	    if(ans==-1)printf("Case %d: impossible\n",case_num++);
	    else printf("Case %d: %d\n",case_num++,ans);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值