标签:des style blog http color java os io
1 10 *........X 1 3 *#X 3 20 #################### #XY.gBr.*.Rb.G.GG.y# #################### 0 0
Escape possible in 9 steps. The poor student is trapped! Escape possible in 45 steps.
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<queue> using namespace std; const int maxn=20+10; char map[maxn][maxn]; int n,m,t; bool vis[maxn][maxn][(1<<10)+10]; int dx[]={-1,1,0,0}; int dy[]={0,0,-1,1}; struct Point { int x,y,step; int key; }; queue<Point>Q; Point st; bool check(int x,int y) { if(x>=1&&x<=n&&y>=1&&y<=m&&map[x][y]!='*') return true; return false; } int bfs() { while(!Q.empty()) Q.pop(); memset(vis,false,sizeof(vis)); vis[st.x][st.y][st.key]=true; st.key=st.step=0; Q.push(st); Point cur,nex; while(!Q.empty()) { cur=Q.front(); Q.pop(); if(map[cur.x][cur.y]=='^') return cur.step; for(int i=0;i<4;i++) { nex.x=cur.x+dx[i]; nex.y=cur.y+dy[i]; nex.key=cur.key; if(check(nex.x,nex.y)) { nex.step=cur.step+1; if(nex.step>=t) continue; else if(map[nex.x][nex.y]>='A'&&map[nex.x][nex.y]<='Z') { int temp=map[nex.x][nex.y]-'A'; int nk=cur.key&1<<temp; if(nk&&!vis[nex.x][nex.y][nex.key]) { vis[nex.x][nex.y][nex.key]=true; Q.push(nex); } } else if(map[nex.x][nex.y]>='a'&&map[nex.x][nex.y]<='z') { int temp=map[nex.x][nex.y]-'a'; nex.key=cur.key|1<<temp; if(!vis[nex.x][nex.y][nex.key]) { vis[nex.x][nex.y][nex.key]=true; Q.push(nex); } } else { if(!vis[nex.x][nex.y][nex.key]) { vis[nex.x][nex.y][nex.key]=true; Q.push(nex); } } } } } return -1; } inline void read_graph() { char str[maxn]; for(int i=1;i<=n;i++) { scanf("%s",str+1); for(int j=1;j<=m;j++) { if(str[j]=='@') { st.x=i; st.y=j; map[i][j]=str[j]; } else map[i][j]=str[j]; } } } int main() { while(~scanf("%d%d%d",&n,&m,&t)) { read_graph(); int ans=bfs(); printf("%d\n",ans); } return 0; }
hdu1885Key Task(状态压缩+bfs),布布扣,bubuko.com
标签:des style blog http color java os io
原文地址:http://blog.csdn.net/u014303647/article/details/38659103