nyoj592spiral grid
来源:互联网 发布:稳定网络的软件 编辑:程序博客网 时间:2024/05/02 02:08
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 49 3210 12
- 样例输出
Case 1: 1Case 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
- nyoj592spiral grid
- NYOJ592spiral grid
- grid
- grid
- Grid
- Grid
- grid
- Grid
- grid
- Grid
- ext-grid常见问题 : Grid
- Grid--bigdog
- GRID新手入门
- freedom--grid
- Grid样式
- Grid 概述
- layout-grid
- Grid Computing
- 问题
- UIColor
- leetcode-Rotate Image
- JavaBean 浅谈
- CentOS6.3 系统中su,su -,sudo三命令讲解
- nyoj592spiral grid
- Html+js实现表格可编辑,并能动态添加删除行
- python 字符串
- Google发布Android Studio 1.0
- unity之列的基本操作和约束
- iOS7: 漫谈基础集合类(NSArray, NSSet, NSOrderedSet ,NSHashTable和 NSDictionary)
- JDBC入门知识
- Java基本功练习八(多维数组[二维、三维、模拟评卷系统、九宫格验证])
- nyoj239月老的难题