HDU1429胜利大逃亡(续)&&HDU 1885 Key Task BFS+状态压缩+水
2014-07-15 15:09
435 查看
HDU1429
只有10把钥匙 1<<10足够了
标记用三维数组
用Linux好不习惯 继续克服~
HDU1885
这个就4把钥匙
只有10把钥匙 1<<10足够了
标记用三维数组
用Linux好不习惯 继续克服~
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream> #include <algorithm> using namespace std; #define MAXN 11111 #include<queue> const int INF = 999999; char mp[33][33]; bool vis[33][33][1026]; int n,m; struct node { int x,y,step; int key; }; queue<node>q; int xx[4]= {1,0,-1,0}; int yy[4]= {0,1,0,-1}; int bfs(int x,int y) { while(!q.empty()) q.pop(); memset(vis,false,sizeof(vis)); node front,rear; front.x=x,front.y=y; front.key=0; front.step=0; vis[x][y][0]=true; q.push(front); while(!q.empty()) { front=q.front(); q.pop(); for(int i=0; i<4; i++) { int dx=front.x+xx[i],dy=front.y+yy[i]; if(dx>=0&&dy>=0&&dx<n&&dy<m&&mp[dx][dy]!='*'&&!vis[dx][dy][front.key]) { if(mp[dx][dy]=='^')//到终点 { return front.step+1; } else if(mp[dx][dy]>='a'&&mp[dx][dy]<='z')//拿到钥匙 { vis[dx][dy][front.key]=true;//标记 rear.key =(front.key )| ( 1<<(mp[dx][dy]-'a'));//添加钥匙 rear.x=dx,rear.y=dy,rear.step=front.step+1; q.push(rear); } else if(mp[dx][dy]>='A'&&mp[dx][dy]<='Z')//遇到门 { if(front.key&(1<<(mp[dx][dy]-'A')))//如果有对应的钥匙 { rear.x=dx,rear.y=dy,rear.key=front.key,rear.step=front.step+1; vis[dx][dy][rear.key]=true; q.push(rear); } } else { rear.x=dx,rear.y=dy,rear.key=front.key,rear.step=front.step+1; vis[dx][dy][rear.key]=true; q.push(rear); } } } } return 0; } int main() { int t; // freopen("in.txt","r",stdin); while(scanf("%d%d%d",&n,&m,&t)!=EOF) { node s; for(int i=0; i<n; i++) scanf("%s",mp[i]); for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(mp[i][j]=='@') s.x=i,s.y=j,s.step=0; } } int ans=bfs(s.x,s.y); if(ans==0||ans>=t) printf("-1\n"); else printf("%d\n",ans); } return 0; }
HDU1885
这个就4把钥匙
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream> #include <algorithm> using namespace std; #define MAXN 11111 #include<queue> const int INF = 999999; char mp[133][133]; bool vis[133][133][33]; int n,m; int ke[28]; struct node { int x,y,step; int key; }; queue<node>q; int xx[4]= {1,0,-1,0}; int yy[4]= {0,1,0,-1}; int bfs(int x,int y) { while(!q.empty()) q.pop(); memset(vis,false,sizeof(vis)); node front,rear; front.x=x,front.y=y; front.key=0; front.step=0; vis[x][y][0]=true; q.push(front); while(!q.empty()) { front=q.front(); q.pop(); for(int i=0; i<4; i++) { int dx=front.x+xx[i],dy=front.y+yy[i]; if(dx>=0&&dy>=0&&dx<n&&dy<m&&mp[dx][dy]!='#'&&!vis[dx][dy][front.key]) { if(mp[dx][dy]=='X')//到终点 { return front.step+1; } else if(mp[dx][dy]>='a'&&mp[dx][dy]<='z')//拿钥匙 { vis[dx][dy][front.key]=true; rear.key =(front.key )| ( 1<<(ke[mp[dx][dy]-'a'])); rear.x=dx,rear.y=dy,rear.step=front.step+1; q.push(rear); } else if(mp[dx][dy]>='A'&&mp[dx][dy]<='Z')//开门 { if(front.key&(1<<(ke[mp[dx][dy]-'A']))) { rear.x=dx,rear.y=dy,rear.key=front.key,rear.step=front.step+1; vis[dx][dy][rear.key]=true; q.push(rear); } } else { rear.x=dx,rear.y=dy,rear.key=front.key,rear.step=front.step+1; vis[dx][dy][rear.key]=true; q.push(rear); } } } } return 0; } int main() { //freopen("in.txt","r",stdin); ke['a'-'a']=0,ke['b'-'a']=1,ke['r'-'a']=2,ke['g'-'a']=3; while(scanf("%d%d",&n,&m),n+m) { node s; for(int i=0; i<n; i++) scanf("%s",mp[i]); for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(mp[i][j]=='*') s.x=i,s.y=j,s.step=0; } } int ans=bfs(s.x,s.y); if(ans==0) printf("The poor student is trapped!\n"); else printf("Escape possible in %d steps.\n",ans); } return 0; }
相关文章推荐
- HDU1429胜利大逃亡(续)&&HDU 1885 Key Task BFS+状态压缩+水
- hdu 1885 Key Task bfs + 状态压缩
- HDU 1885 Key Task (状态压缩+BFS)
- HDU 1429--胜利大逃亡(续)【BFS && 状态压缩】
- HDU 5094 --Maze【BFS && 状态压缩】
- hdu 4771 状态压缩+bfs Stealing Harry Potter's Precious
- HDU 5094 --Maze【BFS && 状态压缩】
- hdu 1885 Key Task bfs
- HDU 1885 Key Task(状态压缩+BFS)
- Hdu-1885-Key Task [状态压缩][bfs]
- hdu 1885 Key Task(状态压缩+bfs)
- hdu 1885(状态压缩+bfs)
- HDU 1429--胜利大逃亡(续)【BFS && 状态压缩】
- hdu 1885(状态压缩+bfs)
- BFS+状态压缩 hdu-1885-Key Task
- HDU 4182 Judges' response(01背包+TSP状态压缩DP)
- 数位dp+LIS+状态压缩-hdu-4352-XHXJ's LIS
- HDU Key Task Key Task(三维bfs)
- hdu 3681 Prison Break (状态压缩dp/dfs + bfs)
- HDU 3681 Prison Break(状态压缩dp + BFS)