HDU 1429 BFS+状态压缩
2013-11-13 16:49
399 查看
#include <iostream> #include <cstdio> #include <cstring> #include <cctype> #include <algorithm> #include <queue> using namespace std; const int N=21; const int KEY=10; char s ; int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}}; int n,m,k; bool hash [1<<KEY];//一定要注意范围,不然就会一直错 struct node { int x,y,step,key; }; int kk(char c) { return c-(isupper(c)?'A':'a'); } int bfs(int x,int y) { int i; queue<node>q; node cur,next; cur.x=x; cur.y=y; cur.step=0; cur.key=0; hash[cur.x][cur.y][cur.key]=1; q.push(cur); while(!q.empty()) { cur=q.front(); q.pop(); if(cur.step>=k)break; for(i=0; i<4; i++) { next.x=cur.x+dir[i][0]; next.y=cur.y+dir[i][1]; next.key=cur.key; next.step=cur.step+1; if(next.x<0||next.x>=n||next.y<0||next.y>=m||s[next.x][next.y]=='*')continue; next.key=cur.key; next.step=cur.step+1; if(isupper(s[next.x][next.y])&&!(next.key&(1<<kk(s[next.x][next.y]))))continue; if(islower(s[next.x][next.y])) next.key=next.key|(1<<kk(s[next.x][next.y])); if(s[next.x][next.y]=='^')return next.step; if(!hash[next.x][next.y][next.key]) { hash[next.x][next.y][next.key]=1; q.push(next); } } } return -1; } int main() { int flag1,flag2; while(scanf("%d%d%d",&n,&m,&k)!=EOF) { k-=1; int flag3=0; for(int i=0; i<n; i++) scanf("%s",&s[i]); for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(s[i][j]=='@') { flag1=i; flag2=j; flag3=1; break; } } if(flag3)break; } int num; memset(hash,0,sizeof(hash)); num=bfs(flag1,flag2); printf("%d\n",num); } return 0; }
相关文章推荐
- HDU 1429 BFS+状态压缩
- hdu(1429) 胜利大逃亡(续) 队列+bfs+状态压缩
- hdu 1429(BFS+状态压缩)
- hdu 1429 胜利大逃亡(续) bfs+状态压缩
- hdu 胜利大逃亡(续) bfs+状态压缩 1429 钥匙与门相对应
- HDU 1429胜利大逃亡(续) (bfs+状态压缩)
- hdu 1429(bfs+状态压缩)
- hdu-1429-胜利大逃亡(续)-bfs+状态压缩
- hdu 1429 胜利大逃亡(续) (bfs+状态压缩)
- HDU 1429 胜利大逃亡(续) (bfs+状态压缩)
- Hdu 1429 胜利大逃亡(续)【bfs+状态压缩】
- hdu~1429(bfs+状态压缩)
- hdu1429-BFS+状态压缩
- hdu 1429 (bfs+状态压缩)
- hdu 1429 胜利大逃亡(续) bfs+状态压缩
- hdu 1429 BFS+状态压缩
- hdu 1429 bfs+状态压缩
- HDU-1429 胜利大逃亡(续) (BFS+状态压缩)
- HDU 1429 (BFS+状态压缩)
- HDU 1429 胜利大逃亡(续) (bfs+状态压缩)