HDU 4771 2013亚洲区域赛杭州站 B题 状态压缩+bfs
2014-07-30 09:57
330 查看
http://acm.hdu.edu.cn/showproblem.php?pid=4771
Problem Description
Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his precious with him. As you know, uncle Vernon never allows
such magic things in his house. So Harry has to deposit his precious in the Gringotts Wizarding Bank which is owned by some goblins. The bank can be considered as a N × M grid consisting of N × M rooms. Each room has a coordinate. The coordinates of the upper-left
room is (1,1) , the down-right room is (N,M) and the room below the upper-left room is (2,1)..... A 3×4 bank grid is shown below:
![](http://acm.hdu.edu.cn/data/images/C486-1002-1.jpg)
Some rooms are indestructible and some rooms are vulnerable. Goblins always care more about their own safety than their customers' properties, so they live in the indestructible rooms and put customers' properties in vulnerable rooms. Harry Potter's precious
are also put in some vulnerable rooms. Dudely wants to steal Harry's things this holiday. He gets the most advanced drilling machine from his father, uncle Vernon, and drills into the bank. But he can only pass though the vulnerable rooms. He can't access
the indestructible rooms. He starts from a certain vulnerable room, and then moves in four directions: north, east, south and west. Dudely knows where Harry's precious are. He wants to collect all Harry's precious by as less steps as possible. Moving from
one room to another adjacent room is called a 'step'. Dudely doesn't want to get out of the bank before he collects all Harry's things. Dudely is stupid.He pay you $1,000,000 to figure out at least how many steps he must take to get all Harry's precious.
Input
There are several test cases.
In each test cases:
The first line are two integers N and M, meaning that the bank is a N × M grid(0<N,M <= 100).
Then a N×M matrix follows. Each element is a letter standing for a room. '#' means a indestructible room, '.' means a vulnerable room, and the only '@' means the vulnerable room from which Dudely starts to move.
The next line is an integer K ( 0 < K <= 4), indicating there are K Harry Potter's precious in the bank.
In next K lines, each line describes the position of a Harry Potter's precious by two integers X and Y, meaning that there is a precious in room (X,Y).
The input ends with N = 0 and M = 0
Output
For each test case, print the minimum number of steps Dudely must take. If Dudely can't get all Harry's things, print -1.
Sample Input
2 3
##@
#.#
1
2 2
4 4
#@##
....
####
....
2
2 1
2 4
0 0
Sample Output
-1
5
题目大意:
一个人从一个n*m的网格中@点出发,目的是访问k个已知的点,要求所用时间最少。
解题思路:
状态压缩,用一个二进制数的每一位表示给出的点是否全部被标记,标记该二进制位对应为1否则为0,若已知点对应的k个二进制位全部标记完毕,那么bfs结束,此时的时间为最短时间。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
int n,m;
int a[110][110];
int sx,sy;
int k;
int dp[110][110][32];
int px[10],py[10];
struct node
{
int x,y;
int s;
node(int _x=0,int _y=0,int _s=0)
{
x=_x;
y=_y;
s=_s;
}
};
int move[][2]={{0,1},{0,-1},{1,0},{-1,0}};
int bfs()
{
queue <node> q;
int s=0;
q.push(node(sx,sy,s));
memset(dp,-1,sizeof(dp));
dp[sx][sy][s]=0;
while(!q.empty())
{
node tmp=q.front();
q.pop();
if(tmp.s==(1<<k)-1)
return dp[tmp.x][tmp.y][tmp.s];
for(int i=0;i<4;i++)
{
int nx=tmp.x+move[i][0];
int ny=tmp.y+move[i][1];
int s=tmp.s;
if(nx<0||nx>=n||ny<0||ny>=m)
continue;
if(a[nx][ny]==-2)
continue;
for(int j=0;j<k;j++)
if(nx==px[j]&&ny==py[j])
{
s|=(1<<j);
}
if(dp[nx][ny][s]!=-1)
continue;
dp[nx][ny][s]=dp[tmp.x][tmp.y][tmp.s]+1;
q.push(node(nx,ny,s));
}
}
return -1;
}
int main()
{
while(~scanf("%d%d%*c",&n,&m))
{
if(n==0&&m==0)
break;
memset(a,-1,sizeof(a));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
char x;
scanf("%c",&x);
if(x=='@')
sx=i,sy=j;
if(x=='#')
a[i][j]=-2;
}
getchar();
}
scanf("%d",&k);
int x,y;
for(int i=0;i<k;i++)
{
scanf("%d%d",&x,&y);
x--;
y--;
px[i]=x;
py[i]=y;
}
printf("%d\n",bfs());
}
return 0;
}
Problem Description
Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his precious with him. As you know, uncle Vernon never allows
such magic things in his house. So Harry has to deposit his precious in the Gringotts Wizarding Bank which is owned by some goblins. The bank can be considered as a N × M grid consisting of N × M rooms. Each room has a coordinate. The coordinates of the upper-left
room is (1,1) , the down-right room is (N,M) and the room below the upper-left room is (2,1)..... A 3×4 bank grid is shown below:
![](http://acm.hdu.edu.cn/data/images/C486-1002-1.jpg)
Some rooms are indestructible and some rooms are vulnerable. Goblins always care more about their own safety than their customers' properties, so they live in the indestructible rooms and put customers' properties in vulnerable rooms. Harry Potter's precious
are also put in some vulnerable rooms. Dudely wants to steal Harry's things this holiday. He gets the most advanced drilling machine from his father, uncle Vernon, and drills into the bank. But he can only pass though the vulnerable rooms. He can't access
the indestructible rooms. He starts from a certain vulnerable room, and then moves in four directions: north, east, south and west. Dudely knows where Harry's precious are. He wants to collect all Harry's precious by as less steps as possible. Moving from
one room to another adjacent room is called a 'step'. Dudely doesn't want to get out of the bank before he collects all Harry's things. Dudely is stupid.He pay you $1,000,000 to figure out at least how many steps he must take to get all Harry's precious.
Input
There are several test cases.
In each test cases:
The first line are two integers N and M, meaning that the bank is a N × M grid(0<N,M <= 100).
Then a N×M matrix follows. Each element is a letter standing for a room. '#' means a indestructible room, '.' means a vulnerable room, and the only '@' means the vulnerable room from which Dudely starts to move.
The next line is an integer K ( 0 < K <= 4), indicating there are K Harry Potter's precious in the bank.
In next K lines, each line describes the position of a Harry Potter's precious by two integers X and Y, meaning that there is a precious in room (X,Y).
The input ends with N = 0 and M = 0
Output
For each test case, print the minimum number of steps Dudely must take. If Dudely can't get all Harry's things, print -1.
Sample Input
2 3
##@
#.#
1
2 2
4 4
#@##
....
####
....
2
2 1
2 4
0 0
Sample Output
-1
5
题目大意:
一个人从一个n*m的网格中@点出发,目的是访问k个已知的点,要求所用时间最少。
解题思路:
状态压缩,用一个二进制数的每一位表示给出的点是否全部被标记,标记该二进制位对应为1否则为0,若已知点对应的k个二进制位全部标记完毕,那么bfs结束,此时的时间为最短时间。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
int n,m;
int a[110][110];
int sx,sy;
int k;
int dp[110][110][32];
int px[10],py[10];
struct node
{
int x,y;
int s;
node(int _x=0,int _y=0,int _s=0)
{
x=_x;
y=_y;
s=_s;
}
};
int move[][2]={{0,1},{0,-1},{1,0},{-1,0}};
int bfs()
{
queue <node> q;
int s=0;
q.push(node(sx,sy,s));
memset(dp,-1,sizeof(dp));
dp[sx][sy][s]=0;
while(!q.empty())
{
node tmp=q.front();
q.pop();
if(tmp.s==(1<<k)-1)
return dp[tmp.x][tmp.y][tmp.s];
for(int i=0;i<4;i++)
{
int nx=tmp.x+move[i][0];
int ny=tmp.y+move[i][1];
int s=tmp.s;
if(nx<0||nx>=n||ny<0||ny>=m)
continue;
if(a[nx][ny]==-2)
continue;
for(int j=0;j<k;j++)
if(nx==px[j]&&ny==py[j])
{
s|=(1<<j);
}
if(dp[nx][ny][s]!=-1)
continue;
dp[nx][ny][s]=dp[tmp.x][tmp.y][tmp.s]+1;
q.push(node(nx,ny,s));
}
}
return -1;
}
int main()
{
while(~scanf("%d%d%*c",&n,&m))
{
if(n==0&&m==0)
break;
memset(a,-1,sizeof(a));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
char x;
scanf("%c",&x);
if(x=='@')
sx=i,sy=j;
if(x=='#')
a[i][j]=-2;
}
getchar();
}
scanf("%d",&k);
int x,y;
for(int i=0;i<k;i++)
{
scanf("%d%d",&x,&y);
x--;
y--;
px[i]=x;
py[i]=y;
}
printf("%d\n",bfs());
}
return 0;
}
相关文章推荐
- hdu4771_2013亚洲区域赛杭州站(状态压缩bfs)
- HDU 4771 Stealing Harry Potter's Precious (2013杭州赛区1002题,bfs,状态压缩)
- 枚举+状态压缩——2013 杭州区域赛A ( UVA 6454 )
- hdu 4770 13 杭州 现场 A - Lights Against Dudely 暴力 bfs 状态压缩DP 难度:1
- HDU 4770 Lights Against Dudely -- 2013 杭州赛区现场赛-A(状态压缩)
- 2013亚洲区域赛杭州站 B题 Zhuge Liang's Password
- 2013杭州站A题||hdu4770 状态 压缩+暴力枚举
- HDU 4778 Gems Fight! (2013杭州赛区1009题,状态压缩,博弈)
- HDU3681Prison Break(状态压缩+BFS)
- hiho 1233 Boxes(状态压缩+BFS)
- hdu 5025 Saving Tang Monk[状态压缩bfs]( 2014 ACM/ICPC Asia Regional Guangzhou Online)
- HDU 4463 Outlets 2012年亚洲区域赛杭州赛区现场赛K题
- hdu 4462 - Scaring the Birds(状态压缩+bfs)
- hihocoder #1233 : Boxes 北京赛区(2015)网络赛(状态压缩+bfs)
- hdu2209 翻纸牌游戏--BFS & 位运算 & 状态压缩(待解决)
- hdu1254推箱子 (BFS+BFS+状态压缩)
- HDU5094->BFS&&状态压缩
- 【bzoj 1195】最短母串(AC自动机+状态压缩+bfs)
- hdu5025(bfs,状态压缩)
- hdu 4778 13杭州I题 状态压缩dp+博弈