您的位置:首页 > 其它

HDU 1728 逃离迷宫 广搜

2012-09-04 20:07 344 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1728

题意:

  一个迷宫...从起点走到终点,拐弯次数有限制的情况下找到最优的解。

坑爹:

  本来用结构体来记录坐标、走的步数和拐弯次数,但是发现如果搜一条路径发现行不通的话这条路上所有点都被标记了,当我再次经过这里面的某个点

的时候就不能通过了。比如说我一开始行不通的那条路通过某个点是左右通过的,但我下次再要从上或者从下经过这个点的时候就会过不了。

解法:

  每次向一个方向拓展时将这一个方向所有满足条件的点全部进队,这样就解决了某个点不会因为之前被标记过而导致下一次以另外一个方向进入这个店

的时候就不给经过的问题。

View Code

#include <iostream>
#include <queue>
using namespace std;

int n ;
int m;
char str[105][105];
bool map[105][105];

int po[4][2] = {1,0  , 0  ,1  , -1 ,0, 0 ,-1 };

struct Node
{
int i,j;
int sum ;
}node[1000005];

int main ()
{
int N;
cin>>N;
while (N -- )
{
memset(map,0,sizeof (map));

cin>>n>>m;
int k;
int x1;
int x2;
int y1;
int y2;

for (int  i = 0; i < n; i++)
{
cin>>str[i];
}
scanf ("%d%d%d%d%d",&k,&x1,&y1,&x2,&y2);
x1 -- ;
x2 -- ;
y1 -- ;
y2 -- ;

if (str[y1][x1] == '*' || str[y2][x2] == '*')
{
cout<<"no"<<endl;
continue;
}

if (x1 == x2 && y1 == y2 )
{
cout<<"yes"<<endl;
continue ;
}

queue <int > Q;
int t = 0;
node[t].i = y1;
node[t].j = x1;
node[t].sum = 0;

map[y1][x1] = 1;

Q.push(t);

int stai;
int staj;
int edi;
int edj;
int flag  = 0;

while (!Q.empty ())
{
int temp = Q.front ();
Q.pop();

stai = node[temp].i;
staj = node[temp].j;
if ( stai == y2 && staj == x2 && node[temp].sum <= k + 1)
{
flag = 1;
break;
}
if (node[temp].sum > k + 1)
{
break;
}

for (int i=0; i<4; i++)
{
edi = stai + po[i][0];
edj = staj + po[i][1];
while (edi >= 0 && edi <n && edj >= 0 && edj  < m && str[edi][edj] == '.'  )
{
if (map[edi][edj])
{
edi = edi + po[i][0];
edj = edj + po[i][1];
continue;
}

t ++ ;
node[t] .i = edi;
node[t]. j = edj;
node[t].sum = node[temp].sum + 1;
map[edi][edj] = 1;
Q.push(t);

edi = edi + po[i][0];
edj = edj + po[i][1];
}
}
}
if (flag )
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: