1 /* 2 宽搜+记忆化搜索 3 */ 4 #include <iostream> 5 #include <cstdio> 6 #include <string> 7 #include <queue> 8 #include <string> 9 #define leftedge 1 10 #define rightedge 5 11 #define topedge 1 12 #define downedge 6 13 using namespace std; 14 struct status{ 15 int map[7][6]; 16 int step; 17 }; 18 bool success; 19 int resx,resy; 20 int steps[1000]; 21 int limit; 22 void output(status s){ 23 int i,j; 24 printf(" --------------- "); 25 for(i=topedge;i<=downedge;++i){ 26 for(j=leftedge;j<=rightedge;++j) 27 printf("%d ",s.map[i][j]); 28 printf(" "); 29 } 30 printf("--------------- "); 31 return; 32 } 33 34 bool check(status s){ 35 int i,j; 36 for(i=topedge;i<=downedge;++i) 37 for(j=leftedge;j<=rightedge;++j){ 38 if(s.map[i][j]!=0) return 0; 39 } 40 return 1; 41 } 42 struct node{ 43 int x,y; 44 }; 45 status update(int x,int y,status s){ //已经爆了的气球 46 queue<node>order; 47 while(order.size()>0) order.pop(); 48 int i,j,k,ii,jj,tx,ty; 49 node t,t1,t2; 50 t.x=x; t.y=y; 51 order.push(t); 52 while(order.size()>0){ 53 t1=order.front(); 54 tx=t1.x; ty=t1.y; 55 order.pop(); 56 s.map[tx][ty]=0; 57 for(i=tx-1;i>=topedge;--i){ 58 if(s.map[i][ty]==0) continue; 59 else { 60 s.map[i][ty]++; t2.x=i; t2.y=ty; 61 if(s.map[i][ty]==5) {s.map[i][ty]=0;order.push(t2);} 62 break; 63 } 64 } 65 for(i=tx+1;i<=downedge;++i){ 66 if(s.map[i][ty]==0) continue; 67 else { 68 s.map[i][ty]++; t2.x=i; t2.y=ty; 69 if(s.map[i][ty]==5) {s.map[i][ty]=0;order.push(t2);} 70 break; 71 } 72 } 73 for(i=ty-1;i>=leftedge;--i){ 74 if(s.map[tx][i]==0) continue; 75 else { 76 s.map[tx][i]++; t2.x=tx; t2.y=i; 77 if(s.map[tx][i]==5) {s.map[tx][i]=0;order.push(t2);} 78 break; 79 } 80 } 81 for(i=ty+1;i<=rightedge;++i){ 82 if(s.map[tx][i]==0) continue; 83 else { 84 s.map[tx][i]++; t2.x=tx; t2.y=i; 85 if(s.map[tx][i]==5) {s.map[tx][i]=0;order.push(t2);} 86 break; 87 } 88 } 89 } 90 if(check(s)==1) {resx=x;resy=y; success=1;output(s);} 91 //output(s); 92 return s; 93 } 94 void change(int step,status s){ 95 int i,j,ii,jj; 96 status temp; 97 if(success==1||step>limit||check(s)==1) 98 return; 99 for(i=topedge;i<=downedge;++i) 100 for(j=leftedge;j<=rightedge;++j){ 101 temp=s; 102 if(temp.map[i][j]==0) continue; 103 temp.map[i][j]++; 104 105 temp.step=i*10+j; 106 steps[step*100+temp.step]=s.step; 107 108 109 110 if(temp.map[i][j]==5) temp=update(i,j,temp); 111 if(success==1){ 112 return; 113 } 114 change(step+1,temp); 115 if(success==1){ 116 printf("ok "); 117 118 119 120 121 return ; 122 } 123 124 125 126 } 127 return; 128 } 129 int main(){ 130 int i,j,tx,ty; 131 status s; 132 int res[10]; 133 while(true){ 134 success=0; 135 printf("请输入限制步数 "); 136 scanf("%d",&limit); 137 for(i=topedge;i<=downedge;++i) 138 for(j=leftedge;j<=rightedge;++j) 139 scanf("%d",&s.map[i][j]); 140 s.step=0; 141 142 change(1,s); 143 printf("success=%d ",success); 144 145 146 res[limit]=limit*100+resx*10+resy; 147 tx=resx; ty=resy; 148 149 for(i=limit-1;i>=1;--i){ 150 res[i]= steps[ (i+1)*100+ tx*10+ty ]; 151 tx=res[i]/10; ty=res[i]%10; 152 } 153 for(i=1;i<=limit;i++) 154 { 155 res[i]=res[i]%100; 156 printf("%d %d ",res[i]/10,res[i]%10); 157 } 158 } 159 return 0; 160 }