POJ2965

和1753基本一样

Problem: 2965   User: wjinkun
Memory: 740K   Time: 79MS
Language: GCC   Result: Accepted
 1 #include <stdio.h>
 2 unsigned short queue[65536][2];   //[0]记录状态,[1]记录下一次起始位置
 3 unsigned short step[65536];       //翻转过程
 4 int            s[16];
 5 int main()
 6 {
 7     int rear=1,top=0,i,j,temp;
 8     for(i=0; i<4; i++)               //b1w0
 9         {
10             for(j=0; j<4; j++)
11                 if(getchar()=='+')
12                     queue[0][0]|=1<<(i*4+j);
13 
14             getchar();
15         }
16 
17 
18     while(1)
19         {
20             if(queue[top][0]==0)
21                 {
22                     for(i=0,j=0;i<16;i++)
23                         if((step[0]>>i)&1)
24                             s[j++]=i;
25                     printf("%d\n",j);
26                     for(i=0;i<j;i++)
27                         printf("%d %d\n",s[i]/4+1,s[i]%4+1);
28                     return 0;
29                 }
30             else
31                 {
32                     for(i=queue[top][1]; i<16; i++)
33                         {
34                             temp=queue[top][0];
35                             temp^=1<<i;
36                             for(j=0;j<4;j++)
37                                 temp^=1<<(i/4*4+j);
38                             for(j=0;j<4;j++)
39                                 temp^=1<<(j*4+i%4);
40 
41                             queue[rear][0]=temp;
42                             queue[rear++][1]=i+1;
43                             step[temp]=step[queue[top][0]]+(1<<i);
44                         }
45 
46                     top++;
47                 }
48         }
49     return 0;
50 }

 

posted on 2012-08-28 14:07  wjinkun  阅读(81)  评论(0编辑  收藏  举报

导航