您的位置:首页 > 其它

HDU 2579 Dating with girls(2)

2012-02-11 21:13 302 查看
http://acm.hdu.edu.cn/showproblem.php?pid=2579

BFS

View Code

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N=110;
const int dx[4]={0,1,0,-1};
const int dy[4]={1,0,-1,0};
char maze

;
int vis

[15];
int n,m,k;
struct sta
{
int x,y,dis;
};
queue<sta> q;
int bfs(int x,int y)
{
while (!q.empty()) q.pop();
sta u={x,y,0};
vis[x][y][0]=1;
q.push(u);
while (!q.empty())
{
u=q.front(); q.pop();
int x,y,d,nx,ny,dis;
x=u.x; y=u.y; dis=u.dis+1;
for (d=0;d<4;d++)
{
nx=x+dx[d]; ny=y+dy[d];
int bol1=vis[nx][ny][dis%k];
int bol2=!maze[nx][ny];
int bol3=maze[nx][ny]=='#' && dis%k!=0;
if (bol1 || bol2 || bol3) continue;
vis[nx][ny][dis%k]=1;
sta v={nx,ny,dis};
q.push(v);
if (maze[nx][ny]=='G') return dis;
}
}
return -1;
}
int main()
{
int T;
scanf("%d",&T);
int i,j,x,y,ans;
while (T--)
{
memset(vis,0,sizeof(vis));
memset(maze,0,sizeof(maze));
scanf("%d%d%d",&n,&m,&k);
for (i=1;i<=n;i++) scanf("%s",maze[i]+1);
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
if (maze[i][j]=='Y') {x=i; y=j;}
ans=bfs(x,y);
if (ans<0) printf("Please give me another chance!\n");
else printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: