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 唉,好久没写搜索了,竟然写了两个晚上,终于AC了;
错误原因:当被找的是素数是,则不能找到,素数孔,能出不能进,也就是说,输入100 3 输出impossible,而输入3 100,则不是如此;
代码如下:1 #include<algorithm> 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<queue> 6 using namespace std; 7 int a[10010]; 8 int dir[4][2]={0,1,0,-1,1,0,-1,0}; 9 const int maxn=105; 10 int vst[maxn][maxn];//访问标记 11 int map[105][105],map1[105][105]; 12 struct state 13 { 14 int x,y;//坐标位置; 15 int step;//搜索统计 16 }; 17 state mm[maxn]; 18 bool check(state s,int bb)//判断该点是否满足条件 19 { 20 //cout<<"**"<<endl; 21 if( (map1[s.x][s.y]==bb)||(!vst[s.x][s.y] && map[s.x][s.y]!=0 && s.x>=0 && s.x<100 && s.y>=0 && s.y<100) ) 22 return 1; 23 else return 0; 24 } 25 int bfs(int aa,int bb) 26 {int i,j; 27 memset(vst,0,sizeof(vst)); 28 for(i=0;i<=100;i++) 29 for(j=0;j<=100;j++) 30 if(map1[i][j]==aa) 31 { 32 goto end; 33 } 34 end : 35 // cout<<i<<j<<endl; 36 queue<state>q; 37 state now,next,st; 38 st.x=i;st.y=j; 39 st.step=0; 40 q.push(st); 41 vst[st.x][st.y]=1; 42 while(!q.empty()) 43 { 44 now=q.front(); 45 q.pop(); 46 if(map1[now.x][now.y]==bb) 47 { 48 return now.step; 49 } 50 for(i=0;i<4;i++) 51 { 52 next.x=now.x+dir[i][0]; 53 next.y=now.y+dir[i][1]; 54 next.step=now.step+1; 55 if(check(next,bb))//满足条件; 56 { 57 //cout<<next.x<<"***"<<next.y<<endl; 58 q.push(next); 59 vst[next.x][next.y]=1; 60 } 61 } 62 } 63 return 0; 64 } 65 void fun( )//判断是否是素数 66 { 67 int i,j,k; 68 memset(a,0,sizeof(a)); 69 a[1]=1; 70 for(i=2;i<=10010;i++) 71 for(j=2;i*j<=10010;j++) 72 a[i*j]=1; 73 } 74 void fuu()//蛇形填数 75 { 76 int tot,x=0,y=0,n=100; 77 memset(map,0,sizeof(map)); 78 memset(map1,0,sizeof(map1)); 79 tot=map1[0][0]=10000; 80 map[0][0]=1; 81 while(tot>1) 82 { 83 while(y+1<n && !map1[x][y+1]) 84 { 85 --tot; 86 if(a[tot]!=0) 87 { 88 map[x][y+1]=1;//如果此位置不是素数则能走,能走的为1,否则为零; 89 } 90 map1[x][++y]=tot;//初始化二位数组,填数 91 } 92 while( x+1<n && !map1[x+1][y]) 93 { 94 --tot; 95 if(a[tot]!=0) 96 {map[x+1][y]=1;} 97 map1[++x][y]=tot; 98 } 99 while(y-1>=0 && !map1[x][y-1]) 100 { --tot; 101 if(a[tot]!=0) 102 {map[x][y-1]=1;} 103 map1[x][--y]=tot; 104 } 105 while(x>0 && !map1[x-1][y]) 106 { 107 --tot; 108 if(a[tot]!=0) 109 {map[x-1][y]=1;} 110 map1[--x][y]=tot; 111 } 112 } 113 } 114 int main() 115 { 116 int m,n,nn=1,k; 117 fun();fuu(); 118 while(cin>>m>>n) 119 { 120 if(m==n) 121 { 122 printf("Case %d: 0 ",nn++);continue;//相同输入零 123 } 124 else if(a[n]==0)//如果第二个是素数则输出impossible 125 {printf("Case %d: impossible ",nn++);continue;} 126 k=bfs(m,n); 127 if(k==0) 128 printf("Case %d: impossible ",nn++); 129 else printf("Case %d: %d ",nn++,k); 130 } 131 return 0; 132 }