nyoj592spiral 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

蛇形填数+广搜,先把表格用蛇形填数打出来,然后再用广搜搜最少步数,很简单,就是存的时候要用筛选法存不然会超时的

题意:这个表格中只能走非质数的格子,求最少步数,,,

 
#include <iostream>
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstring>

using namespace std;

struct node
{
	int x, y, step;
};
int first, last, sx, sy, ex, ey;
int maze[105][105], vis[105][105], mark[10000];
int di[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int bfs()
{
	queue <node> q;
	node no = {sx, sy, 0};
	vis[sx][sy] = 1;//printf("%d\t%d\n", sx, sy);
	int i, current_x, current_y;
	q.push(no);
	while(!q.empty())
	{
		no = q.front();
		if(no.x == ex && no.y == ey)
		{
			return no.step;
		}//printf("%d\t%d\n", no.x, no.y);
		q.pop();
		for(i = 0; i < 4; i++)
		{
			current_x = no.x + di[i][0];
			current_y = no.y + di[i][1];
			if(current_x >= 1 && current_x <= 100 && current_y >= 1 && current_y <= 100)
			{
				if(vis[current_x][current_y] == 0 && mark[maze[current_x][current_y]] == 1)
				{
					node de = {current_x, current_y, no.step + 1};
					q.push(de);
					vis[current_x][current_y] = 1;
				}
			}
		}
	}
	return -1;
}
int main()
{
	int row, col;
	row = 1;
	col = 1;
	int num = 10000;
	while(num > 0)//初始化存表格
	{
		while(row + col <= 101 && maze[row][col] == 0 && num > 0)
		{
			maze[row][col] = num;
			num--;
			col++;
		}
		col--;
		row++;
		while(row <= col && maze[row][col] == 0 && num > 0)
		{
			maze[row][col] = num;
			num--;
			row++;
		}
		row--;
		col--;
		while(row + col >= 101 && maze[row][col] == 0 && num > 0)
		{
			maze[row][col] = num;
			num--;
			col--;
		}
		row--;
		col++;
		while(col <= row && maze[row][col] == 0 && num > 0)
		{
			maze[row][col] = num;
			num--;
			row--;
		}
		row++;
		col++;
	}
	int i, j;
	/*for(i = 2; i <= 10000; i++)
	{
		flag = 1;
		for(j = 2; j <= sqrt(i); j++)
		{
			if(i % j == 0)
			{
				flag = 0;
				break ;
			}
		}
		if(flag == 1)
		{
			mark[i] = 1;
		}
	}*/
	mark[1] = 1;
	for(i = 2; i <= 5000; i++)//筛选法求素数
    {
        if(mark[i] == 1)
        {
            continue;
        }
        for(j = i * 2; j < 10000; j += i)
        {
            mark[j] = 1;
        }
    }
	int cnt = 1, flage, flags;
	while(~scanf("%d%d", &first, &last))
	{
		printf("Case %d: ", cnt);
		flage = 0;
		flags = 0;
		if(first == 0)
        {
            printf("impossible\n");
        }
        else
        {
		for(row = 1; row <= 100; row++)
		{
			for(col = 1; col <= 100; col++)
			{
				if(maze[row][col] == first)
				{
					sx = row;
					sy = col;
					flags = 1;
				}
				if(maze[row][col] == last)
				{
					ex = row;
					ey = col;
					flage = 1;
				}
				if(flags == 1 && flage == 1)
				{
					break ;
				}
			}
		}//printf("%d\t%d\n%d\t%d\n", sx, sy, ex, ey);
		int result = bfs();//printf("%d\n", result);
		if(result == -1)
		{
			printf("impossible\n");
		}
		else
		{
			printf("%d\n", result);
		}
        }
		cnt++;
		memset(vis, 0, sizeof(vis));
	}
	return 0;
}
        


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值