首页 技术 正文
技术 2022年11月16日
0 收藏 564 点赞 2,679 浏览 1300 个字

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2579

题目大意:走迷宫。对于障碍点,只有当前(dep+1)%k才能走,问最少时间。

解题思路

只有一个关键:

每个点不是只可以走一次。最多可以走k次。

原因是对于一个点,可能是通过障碍点在k的倍数(即余数为0)步到达的,也可能是通过其它途径到达的,但是不是k的倍数(余数为1~k)。

这样,判断状态是否重叠的依据就是看在(x,y)点的余数状态了。vis的第三维非常重要。

#include "cstdio"
#include "string"
#include "cstring"
#include "iostream"
#include "queue"
using namespace std;
int n,m,k,sx,sy,ex,ey,T,vis[][][],dir[][]={-,,,,,-,,};
char map[][];
struct status
{
int x,y,dep;
status(int x,int y,int dep):x(x),y(y),dep(dep) {}
};
int bfs(int x,int y)
{
queue<status> Q;
Q.push(status(x,y,));
vis[x][y][]=true;
while(!Q.empty())
{
status t=Q.front();Q.pop();
for(int s=;s<;s++)
{
int X=t.x+dir[s][],Y=t.y+dir[s][],dep=t.dep+;
if(X<||X>n||Y<||Y>m) continue;
if(map[X][Y]=='#')
{
if(dep%k==&&!vis[X][Y][dep%k])
{
vis[X][Y][dep%k]=true;
Q.push(status(X,Y,dep));
}
}
else
{
if(vis[X][Y][dep%k]) continue;
if(X==ex&&Y==ey) return dep;
vis[X][Y][dep%k]=true;
Q.push(status(X,Y,dep));
}
}
}
return -;
}
int main()
{
//freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
string tt;
cin>>T;
while(T--)
{
memset(vis,,sizeof(vis));
cin>>n>>m>>k;
for(int i=;i<=n;i++)
{
cin>>tt;
for(int j=;j<tt.size();j++)
{
map[i][j+]=tt[j];
if(tt[j]=='Y') {sx=i;sy=j+;}
if(tt[j]=='G') {ex=i;ey=j+;}
}
}
int ans=bfs(sx,sy);
if(ans==-) cout<<"Please give me another chance!"<<endl;
else cout<<ans<<endl;
}
}
11899492 2014-10-18 10:10:31 Accepted 2579 0MS 964K 1734B C++ Physcal
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,958
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,482
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,328
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,111
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,743
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,777