1 //Result:wizmann 1324 Accepted 3112K 3454MS G++ 3200B 2 //What's Wrong: 3 // 1.位压缩的效率,减少无关位 4 // 2.蛇的走动方向没有经过检测 5 // 3.没有自己出数据检验 6 // 4.对于某些数据相当然了 7 #include <cstdio> 8 #include <cstdlib> 9 #include <cstring> 10 #include <algorithm> 11 #include <iostream> 12 #include <queue> 13 #include <bitset> 14 15 using namespace std; 16 17 #define SIZE 21 18 #define INF 1<<25 19 #define STATUS 1<<14 20 #define print(x) cout<<x<<endl 21 22 const int mx[]={0,1,0,-1}; 23 const int my[]={-1,0,1,0}; 24 int n,m,l; 25 26 struct point 27 { 28 int x,y; 29 point(){} 30 point(int i_x,int i_y) 31 { 32 x=i_x;y=i_y; 33 } 34 }; 35 36 struct snake 37 { 38 point head; 39 int status; 40 int step; 41 42 snake() 43 { 44 status=0; 45 step=0; 46 } 47 48 void makeStatus(point *body) 49 { 50 status=0; 51 int ptr=0; 52 for(int i=1;i<l;i++) 53 { 54 int now=i; 55 int pre=i-1; 56 int dir=-1; 57 for(int i=0;i<4;i++) 58 { 59 if(body[pre].x-body[now].x==mx[i] && 60 body[pre].y-body[now].y==my[i]) 61 { 62 dir=i; 63 break; 64 } 65 } 66 //print("DIR:"<<dir); 67 status |= (dir<<ptr); 68 //print(status); 69 ptr+=2; 70 } 71 } 72 }; 73 74 point stone[SIZE*SIZE]; 75 queue<snake> q; 76 bitset<STATUS> visit[SIZE][SIZE]; 77 //char visit[SIZE][SIZE][STATUS]; 78 int stone_num; 79 80 inline bool inMap(int x,int y) 81 { 82 if(x>0 && x<=m && y>0 && y<=n) return true; 83 else return false; 84 } 85 86 int moveSnake(int x,int y,snake& py) 87 { 88 for(int i=0;i<stone_num;i++) 89 { 90 if(x==stone[i].x && y==stone[i].y) return -1; 91 } 92 int status=0; 93 point now=py.head; 94 point head=point(x,y); 95 int ptr=0; 96 for(int i=0;i<l;i++) 97 { 98 int dir; 99 if(now.x==x && now.y==y) return -1; 100 else 101 { 102 //print(now.x<<' '<<now.y); 103 for(int j=0;j<4;j++) 104 { 105 if(head.x-now.x==mx[j] && head.y-now.y==my[j]) 106 { 107 dir=j; 108 break; 109 } 110 } 111 if(i<l-1) 112 { 113 status |= (dir<<ptr); 114 dir=( (3<<ptr) & py.status )>>ptr; 115 //print("DIR:"<<dir); 116 ptr+=2; 117 head=now; 118 now.x-=mx[dir]; 119 now.y-=my[dir]; 120 } 121 } 122 } 123 py.head=point(x,y); 124 py.status=status; 125 py.step++; 126 //puts(""); 127 return py.status; 128 } 129 130 int bfs() 131 { 132 int ans=INF; 133 while(!q.empty()) 134 { 135 snake now=q.front(); 136 q.pop(); 137 int now_x=now.head.x; 138 int now_y=now.head.y; 139 //print(now_x<<' '<<now_y); 140 if(now.step>=ans || visit[now_y][now_x][now.status]) continue; 141 if(now_x==1 && now_y==1) 142 { 143 ans=now.step; 144 continue; 145 } 146 visit[now_y][now_x][now.status]=1; 147 148 point head=now.head; 149 150 for(int i=0;i<4;i++) 151 { 152 int nx=head.x+mx[i]; 153 int ny=head.y+my[i]; 154 if(!inMap(nx,ny)) continue; 155 snake npy=now; 156 int nstatus=moveSnake(nx,ny,npy); 157 //print(nstatus); 158 if(nstatus==-1) continue; 159 else q.push(npy); 160 } 161 } 162 return ans; 163 } 164 165 166 int main() 167 { 168 int cas=1; 169 point body[10]; 170 while(scanf("%d%d%d",&n,&m,&l)!=EOF && n+m+l) 171 { 172 snake py; 173 int a,b; 174 for(int i=0;i<SIZE;i++) 175 { 176 for(int j=0;j<SIZE;j++) 177 { 178 visit[i][j].reset(); 179 } 180 } 181 while(!q.empty()) q.pop(); 182 183 for(int i=0;i<l;i++) 184 { 185 scanf("%d%d",&a,&b); 186 body[i]=point(b,a); 187 } 188 py.head=body[0]; 189 py.makeStatus(body); 190 py.step=0; 191 q.push(py); 192 193 scanf("%d",&stone_num); 194 for(int i=0;i<stone_num;i++) 195 { 196 scanf("%d%d",&a,&b); 197 stone[i]=point(b,a); 198 } 199 int ans=bfs(); 200 if(ans>=INF) ans=-1; 201 202 printf("Case %d: %d\n",cas++,ans); 203 } 204 return 0; 205 }
转载于:https://www.cnblogs.com/Wizmann/archive/2012/04/11/2441632.html