hdu 4634 Swipe Bo

1.每次用循环找出一个遇到‘#’的点（即此时已经停止，要主动去改变运动方向），期间对所经过的点进行处理。

2.将1得到的点分上下左右四个运动方向放进队列。

3.注意判断下一步的可行性。

<pre name="code" class="cpp">#include <stdio.h>
#include <string.h>
#include <queue>
#define up 2
#define down 1
#define left 4
#define right 3
using namespace std;
struct state
{
int x, y, key, step, flag;
friend bool operator < (state n1, state n2)
{
return n1.step > n2.step;
}
};
char map[205][205];
bool vis[205][205][5][(1 << 7) + 1];
int k[205][205];
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int n, m, sumKey;

int judge(int xx, int yy)
{
if(xx >= 0&&xx < n&&yy >= 0&&yy < m&&map[xx][yy] != '#') return 1;
return 0;
}
int judge1(int xx, int yy)
{
if(xx >= 0&&xx < n&&yy >= 0&&yy < m) return 1;
return 0;
}
priority_queue<state>q;
int bfs()
{
int shi, i, full, con, nowkey, nowstep, check, x, y;

state now, next;
full = (1<<sumKey) - 1;
memset(vis, false, sizeof(vis));

while(!q.empty())
{
now = q.top();
q.pop();
//if(map[now.x][now.y] == 'E'&&now.key == full)
//return now.step;

x = now.x;
y = now.y;
con = now.flag;
nowkey = now.key;
nowstep = now.step;

check = 0;
//if(!judge(x, y)) continue;
while(1)
{
//printf("%d %d %d\n", x ,y ,nowstep);
if(map[x][y] == 'K') nowkey = nowkey | k[x][y];
if(map[x][y] == 'E'&&nowkey == full) return nowstep;
//
if(map[x][y] == 'L'){
con = left;
if(vis[x][y][con][nowkey]) break;
vis[x][y][con][nowkey] = true;
}
if(map[x][y] == 'R'){
con = right;
if(vis[x][y][con][nowkey]) break;
vis[x][y][con][nowkey] = true;
}
if(map[x][y] == 'U'){
con = up;
if(vis[x][y][con][nowkey]) break;
vis[x][y][con][nowkey] = true;
}
if(map[x][y] == 'D'){
con = down;
if(vis[x][y][con][nowkey]) break;
vis[x][y][con][nowkey] = true;
}
vis[x][y][con][nowkey] = true; //处理当前步
//
if(judge1(x + dx[con - 1], y + dy[con - 1])&&map[x + dx[con - 1]][y + dy[con - 1]] == '#'){//判断下一步
check = 1;
break;
}
if(judge(x + dx[con - 1], y + dy[con - 1]))//判断下一步
{
x = x + dx[con - 1];
y = y + dy[con - 1];
}
else break;
}

if(check)
{
//vis[x][y][con][nowkey] = true;
for(i = 0;i < 4;i++)
{
if(i != (con - 1))
{
if(judge(x + dx[i], y + dy[i])&&(vis[x][y][i+1][nowkey] == false))//判断当前步是否重复和下一步是否可行
{

next.x = x;
next.y = y;
next.key = nowkey;
next.flag = i + 1;
next.step = nowstep + 1;
vis[x][y][i+1][nowkey] = true;
q.push(next);
}
}
}
}
}
return -1;
}
int main()
{
int ans, i, j, sx, sy;

while(scanf("%d %d", &n, &m) != EOF)
{
getchar();
sumKey = 0;
memset(k, 0, sizeof(k));
for(i = 0;i < n;i++)
{
for(j = 0;j < m;j++)
{
scanf("%c", &map[i][j]);
if(map[i][j] == 'S')
{
sx = i;
sy = j;
}
if(map[i][j] == 'K')
{
k[i][j] = 1 << sumKey;
sumKey++;
}
}
getchar();
}
//printf("%d %d\n", sx, sy);
while(!q.empty()) q.pop();
state tmp;
tmp.x = sx;
tmp.y = sy;
tmp.step = 1;
tmp.key = 0;
map[sx][sy] = '.';
for(i = 0;i < 4;i++)
{
tmp.flag = i + 1;
vis[sx][sy][tmp.flag][0] = true;
q.push(tmp);
}
ans = bfs();
printf("%d\n", ans);
}
return 0;
}

• 本文已收录于以下专栏：

hdu 4634 Swipe Bo（bfs 状态压缩）

• solotzg
• 2014年05月29日 10:27
• 524

hdu Swipe Bo(bfs＋状态压缩)错了多次的题

Swipe Bo Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total...

hdu4634 状压bfs

Description “Swipe Bo” is a puzzle game that requires foresight and skill. The main character of t...

HDU 5755 Gambler Bo(高斯消元解同余方程)

http://acm.hdu.edu.cn/showproblem.php?pid=5755 一眼高斯消元的水题啊，只是数据是900，900^3的高斯消元可能过不去，所以比赛的时候怂了没有写，早知道...

HDU-2016 Multi-University Training Contest 3-Sqrt Bo-大数开方

Sqrt Bo Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Tota...
• MIKASA3
• 2016年07月26日 16:07
• 209

举报原因： 您举报文章：hdu 4634 Swipe Bo 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)