BFS+状态压缩 hdu-1429 胜利大逃亡(续)
2013-08-02 16:57
316 查看
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1429
题目意思:
给一个矩阵,告诉起点和终点,有些位置是墙不能走,有些位置是门,要拿到对应钥匙才能走,有些位置是钥匙。每走一步花一分钟。
问从起点到终点能否在t分钟内走到,如果能输出最短的到达时间,如果不能则输出-1。
解题思路:
状态压缩+BFS
每一把钥匙对应一位,拿到了为1,没有拿到则为0.
直接BFS就行了。
代码:
http://acm.hdu.edu.cn/showproblem.php?pid=1429
题目意思:
给一个矩阵,告诉起点和终点,有些位置是墙不能走,有些位置是门,要拿到对应钥匙才能走,有些位置是钥匙。每走一步花一分钟。
问从起点到终点能否在t分钟内走到,如果能输出最短的到达时间,如果不能则输出-1。
解题思路:
状态压缩+BFS
每一把钥匙对应一位,拿到了为1,没有拿到则为0.
直接BFS就行了。
代码:
#include<iostream> #include<cmath> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<algorithm> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #define eps 1e-6 #define INF 0x1f1f1f1f #define PI acos(-1.0) #define ll __int64 #define lson l,m,(rt<<1) #define rson m+1,r,(rt<<1)|1 //#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; /* freopen("data.in","r",stdin); freopen("data.out","w",stdout); */ bool vis[1<<11][25][25]; char save[25][25]; int n,m,t,dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; struct Point { int x,y,ss; int r; }; bool iscan(int x,int y) { if(x<1||x>n||y<1||y>m||save[x][y]=='*') return false; return true; } Point s; void bfs() { memset(vis,false,sizeof(vis)); queue<Point>myq; s.ss=0; s.r=0; myq.push(s); vis[0][s.x][s.y]=true; //一把钥匙都没拿到 while(!myq.empty()) { Point tmp=myq.front(); myq.pop(); int xx,yy; for(int i=0;i<4;i++) { xx=tmp.x+dir[i][0],yy=tmp.y+dir[i][1]; if(!iscan(xx,yy)) continue; if(save[xx][yy]=='^') //到达出口 { if(tmp.ss+1<t) printf("%d\n",tmp.ss+1); else printf("-1\n"); //最近的都不行,后面的肯定不行 return ; } int tt=tmp.r; if(save[xx][yy]<='j'&&save[xx][yy]>='a') //又拿到一把钥匙 { int ta=1<<(save[xx][yy]-'a'+1); tt=tt|ta; } else if(save[xx][yy]<='J'&&save[xx][yy]>='A') { if(!(tmp.r&(1<<(save[xx][yy]-'A'+1)))) //没有打开此门的钥匙 continue; } if(vis[tt][xx][yy]) //该状态之前已经访问过 continue; vis[tt][xx][yy]=true; Point pp; pp.x=xx,pp.y=yy,pp.r=tt,pp.ss=tmp.ss+1; myq.push(pp); } } printf("-1\n"); //不可能到达出口 return ; } int main() { while(~scanf("%d%d%d",&n,&m,&t)) { for(int i=1;i<=n;i++) { scanf("%s",save[i]+1); for(int j=1;j<=m;j++) { if(save[i][j]=='@') s.x=i,s.y=j; } } bfs(); } return 0; }
相关文章推荐
- HDU 1429胜利大逃亡(续) (bfs+状态压缩)
- HDU 1429 胜利大逃亡(续) (bfs+状态压缩)
- hdu-1429-胜利大逃亡(续)-bfs+状态压缩
- hdu 1429 胜利大逃亡(续) bfs+状态压缩
- HDU 1429 胜利大逃亡(续) BFS+状态压缩
- HDU 1429 胜利大逃亡(续) (bfs+状态压缩)
- hdu 1429 胜利大逃亡(续) bfs+状态压缩
- Hdu 1429 胜利大逃亡(续)【bfs+状态压缩】
- hdu 1429 胜利大逃亡(续) bfs+状态压缩
- hdu(1429) 胜利大逃亡(续) 队列+bfs+状态压缩
- HDU-1429 胜利大逃亡(续) (BFS+状态压缩)
- hdu 胜利大逃亡(续) bfs+状态压缩 1429 钥匙与门相对应
- hdu 1429 胜利大逃亡(续) (bfs+状态压缩)
- hdoj 1429 胜利大逃亡(续) 【BFS+状态压缩】
- HDU_1429——胜利大逃亡续,十位二进制状态压缩,状态判重
- hdu 1429 BFS+状态压缩
- HDU 1429 胜利大逃亡(续)(状态压缩表示,BFS)
- HDU 1429--胜利大逃亡(续)【BFS && 状态压缩】
- HDU 1429 BFS+状态压缩
- hdu 1429 胜利大逃亡(续)(bfs+状态压缩)