Nylg592 bfs+蛇形填数

9 篇文章 0 订阅
感觉测试数据中y可能是素数,x不会是素数;如果从y开始搜索就会出错,要从x开始搜索

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

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;
public class Nylg1072 {
	private static final int N = 100;
	private static int[][] map = new int[N+2][N+2];
	private static boolean[] prime = new boolean[N*N+1];
	private static int[][] s = {{-1, 0, 1, 0}, {0, 1, 0, -1}};
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int cases = 0;
		int x, y;
		initPrime();
        //showPrime();
		init();
		//show(map);
		while(sc.hasNextInt()) {
			cases ++;
			x = sc.nextInt();
			y = sc.nextInt();
			System.out.print("Case " + cases + ": ");
			if(x == y) {
				System.out.println("0");
				continue;
			}
			System.out.println(bfs(x, y));
		}
	}
	
	private static String bfs(int x, int y) {
		String result = "impossible";
		Queue<Node> queue = new ArrayDeque<Node>();
		int tempMap[][] = cloneMap();
		int a, b, step;
		Node temp = search(x);
		tempMap[temp.i][temp.j] = 0;
		queue.add(temp);
		while(!queue.isEmpty()) {
			temp = queue.poll();
			for(int i=0; i<4; i++) {
				a = temp.i+s[0][i];
				b = temp.j+s[1][i];
				step = temp.step + 1;
				if(tempMap[a][b]!=0 && prime[tempMap[a][b]]) {
					if(tempMap[a][b] == y) return step+"";
					queue.add(new Nylg1072().new Node(a, b, step));
					tempMap[a][b] = 0;
				}
			}
		}
		return result;
	}

	private static int[][] cloneMap() {
		int[][] tempMap = new int[N+2][N+2];
		for(int i=1; i<=N; i++) {
			for(int j=1; j<=N; j++) {
				tempMap[i][j] = map[i][j];
			}
		}
		return tempMap;
	}

	private static Node search(int x) {
		for(int i=1; i<=N; i++) {
			for(int j=1; j<=N; j++) {
				if(map[i][j] == x) return new Nylg1072().new Node(i, j, 0);
			}
		}
		return null;
	}


	private static void initPrime() {
		int k = N*N/2;
		int m = N*N+1;
		prime[0] = prime[1] = true;
		for(int i=2; i<=k; i++) {
			int j=i+i;
			while(j<m) {
				prime[j] = true;
				j += i;
			}
		}
	}

	private static void init() {
		int count = N*N;
		int r = 1; 
		int c = N;
		while(count > 0) {
			for(int i=1; i<=N; i++) {//行向右走
				if(map[r][i]==0) {
					map[r][i] = count--;
				}
			}
			for(int i=r; i<=N; i++) {//列向下走
				if(map[i][c]==0) {
					map[i][c] = count--;
				}
			}
			for(int i=N; i>=1; i--) {//行向左走
				if(map[c][i]==0) {
					map[c][i] = count--;
				}
			}
			for(int i=N; i>=1; i--) {//列向上走
				if(map[i][r]==0) {
					map[i][r] = count--;
				}
			}
			r++;
			c--;
		}
	}
	
	class Node {
		int i;
		int j;
		int step;
		
		public Node(int i, int j, int step) {
			this.i = i;
			this.j = j;
			this.step = step;
		}
	}

//	private static void showPrime() {
//		for(int i=0; i<=N*N; i++) {
//			System.out.print(i+""+prime[i] + " ");
//		}
//		System.out.println();
//	}
//
//	private static void show(int[][] map1) {
//		for(int i=0; i<map1.length; i++) {
//			for(int j=0; j<map1[0].length; j++) {
//				System.out.printf("%6d", map1[i][j]);
//			}
//			System.out.println();
//		}
//	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值