HDU 1429 状压bfs 胜利大逃亡
2016-09-01 18:17
260 查看
题目链接:HDU 1429
题解:这题 是状压bfs入门题,但是实在我知道是状压bfs之后了。。。这种最短路一般是bfs,第一眼看到有状态问题,就想着bfs,无果 然后联想到状压dp, 一点点找状态,最后 好不容易做出答案 提交 MLE...懵逼 开数组标记状态的时候 用的1 << 10 + 5 ..哎。。
状压bfs,这题找到钥匙 ,要判断 遇到锁要看有没有钥匙
step[i][j][k]表示在i, j点状态为k的最优解
AC code:
题解:这题 是状压bfs入门题,但是实在我知道是状压bfs之后了。。。这种最短路一般是bfs,第一眼看到有状态问题,就想着bfs,无果 然后联想到状压dp, 一点点找状态,最后 好不容易做出答案 提交 MLE...懵逼 开数组标记状态的时候 用的1 << 10 + 5 ..哎。。
状压bfs,这题找到钥匙 ,要判断 遇到锁要看有没有钥匙
step[i][j][k]表示在i, j点状态为k的最优解
AC code:
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<cctype> #include<queue> using namespace std; #define debug 0 #define inf 0x3f3f3f3f const int maxn = 20 + 5; char str[maxn][maxn]; int steps[maxn][maxn][(1 << 10) + 5], n, m, tl, stx, sty; int dir[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};//数组标记方向 struct R { int x, y; int status; int step; } s, t; bool judge(int x, int y) { if(x < 1 || x > n || y < 1 || y > m) return false; return true; } int tran(char ch) { int ans = 1; char a = isupper(ch)? 'A' : 'a'; int n = ch - a; while(n--) ans *= 2; return ans; } int bfs() { s.step = 0; s.status = 0; s.x = stx; s.y = sty; steps[s.x][s.y][s.status] = 0; //初始化 起点状态步数优先解为0 queue<R> q; q.push(s); while(!q.empty()) { t = q.front(); q.pop(); if(str[t.x][t.y] == '^') { return t.step;//规定时间内找到出口 } for(int i = 0; i < 4; i++) { s.x = t.x + dir[i][0], s.y = t.y + dir[i][1]; s.status = t.status; s.step = t.step + 1;//下一步的状态 //printf("status:%d\n",s.status); if(s.step >= tl) //剪枝 continue; if(judge(s.x, s.y))//边界 { if(s.step >= steps[s.x][s.y][s.status]) continue;//找的是最优解 steps[s.x][s.y][s.status] = s.step; char ch = str[s.x][s.y]; if(ch == '*')//不可走 continue; else if(islower(ch)) { int temp = tran(ch); if((s.status & temp) == 0)//位运算优先级 刚开始找bug在这里浪费好多时间 没加() { s.status += temp;//第一次找到该钥匙更新状态 //printf("yes\n"); } q.push(s); } else if(isupper(ch)) { int temp = tran(ch); if((s.status & temp) == 0)//找到带锁门,如果有钥匙可行 continue; q.push(s); } else { q.push(s); } } } } return -1; } int main() { #if debug freopen("in.txt", "r", stdin); #endif // debug char a; while(~scanf("%d%d%d", &n, &m, &tl)) { memset(steps, inf, sizeof(steps)); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { while((a = getchar()) == ' ' || a == '\n'); str[i][j] = a; if(a == '@') { stx = i; sty = j; } } //printf("%d %d\n", stx, sty); int ans = bfs(); printf("%d\n", ans); } return 0; }
相关文章推荐
- HDU1429 胜利大逃亡(续) 【状压搜索BFS】
- HDU 1429 胜利大逃亡(续)(状压bfs)
- hdu 1429 胜利大逃亡(续) (bfs+状态压缩)
- HDU 1429 胜利大逃亡(续)(状态压缩表示,BFS)
- hdu1429 胜利大逃亡(续) (BFS+状态压缩)
- hdu 1429 胜利大逃亡(续) 二进制bfs
- hdu 1429 胜利大逃亡(续)(BFS+位压缩+走回头路)
- [BFS] HDU 1429 胜利大逃亡(续)
- 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 && 状态压缩】
- B - 胜利大逃亡(续) HDU - 1429 bfs