bfs

Escape
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 301 Accepted Submission(s): 110

Problem Description
You find yourself trapped in a large rectangular room, made up of large square tiles; some are accessible, others are blocked by obstacles or walls. With a single step, you can move from one tile to another tile if it is horizontally or vertically adjacent (i.e. you cannot move diagonally).
To shake off any people following you, you do not want to move in a straight line. In fact, you want to take a turn at every opportunity, never moving in any single direction longer than strictly necessary. This means that if, for example, you enter a tile from the south, you will turn either left or right, leaving to the west or the east. Only if both directions are blocked, will you move on straight ahead. You never turn around and go back!
Given a map of the room and your starting location, figure out how long it will take you to escape (that is: reach the edge of the room).

Input
On the first line an integer t (1 <= t <= 100): the number of test cases. Then for each test case:

a line with two integers separated by a space, h and w (1 <= h, w <= 80), the height and width of the room;

then h lines, each containing w characters, describing the room. Each character is one of . (period; an accessible space), # (a blocked space) or @ (your starting location).
There will be exactly one @ character in each room description.

Output
For each test case:

A line with an integer: the minimal number of steps necessary to reach the edge of the room, or -1 if no escape is possible.

Sample Input
2
9 13
/*

#

@……….

.#.#.#.

………..

.#.#.#.#.#.

.#…….#.

.#.#.#.#.#.

………..

.

4 6

.

.#.

…@

*/

Sample Output
31
-1
/*解题思路
困扰了半天 最后看了 以为大神写的博客 顿时豁然开朗
就是用bfs来做就可以了
*/

include

include

include

include

include

include

using namespace std;
typedef struct LNode{
int x;
int y;
int step;
int fx;//方向
}node;

int dir[5][2]={{0,0},{1,0},{0,1},{-1,0},{0,-1}};//没走过 向下 向右 向上 向左
int h,w;
int mp[82][82];
int sx ,sy;
int visit[82][82][5];
int bfs(){
queue q;
node now ;
now.x = sx;
now.y = sy;
now.step = 0;
now.fx = -1;
q.push(now);
while(!q.empty()){
now = q.front();
q.pop();
if(now.x==1||now.y==w||now.x==h||now.y==1)//已经到达边界了 此时直接退出
return now.step;
for(int i = 1;i<5;i++){
int xx = now.x + dir[i][0];
int yy = now.y + dir[i][1];
if(xx>=1&&yy>=1&&xx<=h&&yy<=w&&!visit[xx][yy][i]&&mp[xx][yy]){
if(i%2 == now.fx%2){//k%2==1 表示水平方向 否则为垂直方向
if(i==now.fx){//判断方向是不是一样的
if(i%2==0&&!mp[now.x-1][now.y]&&!mp[now.x +1][now.y]){
visit[xx][yy][i] = 1;
node nxt ;
nxt.x = xx;
nxt.y = yy;
nxt.step = now.step +1;
nxt.fx = i;
q.push(nxt);
}
if(i%2==1&!mp[now.x][now.y+1]&&!mp[now.x][now.y-1]){
visit[xx][yy][i] = 1;
node nxt ;
nxt.x = xx;
nxt.y = yy;
nxt.step = now.step +1;
nxt.fx = i;
q.push(nxt);
}
}
}
else{
visit[xx][yy][i] = 1;
node nxt ;
nxt.x = xx;
nxt.y = yy;
nxt.step = now.step +1;
nxt.fx = i;
q.push(nxt);
}
}
}

}
return -1;

}
int main(){
int ncase;
cin>>ncase;
while(ncase–){
cin>>h>>w;
memset(mp,0,sizeof(mp));
for(int i = 1;i<=h;i++){
string str;
cin>>str;
for(int j = 0;j

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值