HDU1429 胜利大逃亡(续) BFS +简单状压
来源:互联网 发布:全民奇迹源码 编辑:程序博客网 时间:2024/05/01 12:08
把手中持有的钥匙状态状压一下即可,然后vis访问标记的时候,开个三维,多一维即为当前持有钥匙状态,这样就能祛除重复标记困难走点的问题,跟网络赛那题很像,网络赛的更难点,这个简单点
int n,m,t;int sx,sy,ex,ey;char mp[20 + 55][20 + 55];bool vis[20 + 5][20 + 5][(1<<10) + 5];int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};struct Node {int now;//现在钥匙状态int x,y;int step;friend bool operator<(Node aa,Node bb) {return aa.step > bb.step;}};void init() {memset(vis,false,sizeof(vis));}bool input() { while(cin>>n>>m>>t) {for(int i=0;i<n;i++) {scanf("%s",mp[i]);for(int j=0;j<m;j++) {if(mp[i][j] == '@')sx = i,sy = j;if(mp[i][j] == '^')ex = i,ey = j;}} return false; } return true;} int ans;void bfs(int x,int y) {queue<Node> q;Node ss,ee;ss.x = x,ss.y = y,ss.step = 0,ss.now = 0;q.push(ss);vis[ss.x][ss.y][0] = true;while(!q.empty()) {ss = q.front();q.pop();if(ss.step >= t)continue;if(ss.x == ex && ss.y == ey) {ans = min(ans,ss.step);break;}for(int i=0;i<4;i++) {int dx = ss.x + dir[i][0];int dy = ss.y + dir[i][1];if(dx < 0 || dx >= n || dy < 0 || dy >= m)continue;if(mp[dx][dy] == '*')continue;if(vis[dx][dy][ss.now])continue;ee.now = ss.now;if(mp[dx][dy] >= 'A' && mp[dx][dy] <= 'J') {/*遇到门*/int tmp = mp[dx][dy] - 'A';if((ss.now&(1<<tmp)) == 0)continue;}if(mp[dx][dy] >= 'a' && mp[dx][dy] <= 'z') {/*遇到钥匙*/int tmp = mp[dx][dy] - 'a';ee.now |= (1<<tmp);}ee.x = dx,ee.y = dy,ee.step = ss.step + 1;vis[dx][dy][ss.now] = true;q.push(ee);}}}void cal() {ans = inf;bfs(sx,sy);if(ans == inf || ans >= t)puts("-1");else cout<<ans<<endl;}void output() {}int main() { while(true) { init(); if(input())return 0; cal(); output(); } return 0;}
0 0
- HDU1429 胜利大逃亡(续) BFS +简单状压
- HDU1429 胜利大逃亡(续) 【状压搜索BFS】
- hdu1429 胜利大逃亡(续) (BFS+简单状态压缩)
- HDU1429 胜利大逃亡(续)(BFS)
- hdu1429 胜利大逃亡(续)--BFS
- HDU1429 胜利大逃亡(续)
- hdu1429 胜利大逃亡(续)
- HDU1429-胜利大逃亡(续)
- HDU1429-胜利大逃亡(续)
- Hdu1429 胜利大逃亡(续)
- HDU1429胜利大逃亡续(状态压缩+BFS)
- hdu1429胜利大逃亡(续) (状态压缩+BFS)
- hdu1429 胜利大逃亡(续)bfs状态压缩
- hdu1429胜利大逃亡(续)(状态压缩+bfs)
- HDU1429胜利大逃亡(续)BFS+状态压缩
- hdu1429 胜利大逃亡(续) (BFS+状态压缩)
- HDU1429 胜利大逃亡(续)(bfs+状态压缩)
- HDU1429 胜利大逃亡(续)(BFS+状态压缩)
- mysql软件安装困难请看
- 【Ubuntu】Language Support 中语言安装不上
- 关于运行时的方法使用:objc_setAssociatedObject
- 复杂数据类型
- HDU 2045 不容易系列之(3)—— LELE的RPG难题
- HDU1429 胜利大逃亡(续) BFS +简单状压
- PCLint使用介绍
- gdi+ 快速绘制透明窗口
- Linux内核的示意图 [摘抄]
- linux后台开发必经之书
- 希尔排序 小讲
- zero与usb-skeleton结合测试
- python类的介绍1
- 手机号码和电话号码的正则表达式