http://acm.split.hdu.edu.cn/showproblem.php?pid=2579

简单bfs题,刚开始在纠结怎么存放vis,因为步数可能有几百步,这么多格子开数组的话也太多了,后来想到只要保存步数%k的状态就好了,bfs到达该点的步数肯定是最优的。

#include<iostream>
#include<string>
#include<cstring>
#include<queue>
using namespace std; string s[];
int r,c,k,dir[][] = {-,,,-,,,,},vis[][][],flag; struct point
{
int x,y,counts;
}start; int main()
{
int n;
cin >> n;
while(n--)
{
flag = ;
memset(vis,,sizeof(vis));
cin >> r >> c >> k;
for(int i = ;i < r;i++) cin >> s[i];
for(int i = ;i < r;i++)
{
for(int j = ;j < c;j++)
{
if(s[i][j] == 'Y')
{
start.x = i;
start.y = j;
goto there;
}
}
}
there:
start.counts = ;
queue<point> q;
q.push(start);
vis[start.x][start.y][] = ;
while(!q.empty())
{
int x = q.front().x,y = q.front().y,counts = q.front().counts;
q.pop();
if(s[x][y] == 'G')
{
flag = ;
cout << counts << endl;
break;
}
for(int i = ;i < ;i++)
{
int xx = x+dir[i][],yy = y+dir[i][],z = (counts+)%k;
if(xx < || xx >= r || yy < || yy >= c) continue;
if(s[xx][yy] == '#' && z) continue;
if(vis[xx][yy][z]) continue;
point temp;
temp.x = xx;
temp.y = yy;
temp.counts = counts+;
q.push(temp);
vis[xx][yy][z] = ;
}
}
if(!flag) cout << "Please give me another chance!" << endl;
}
return ;
}

最新文章

  1. 提交按钮ajax方式
  2. 关于近段时间论坛型APP 的一段舍弃
  3. JQuery 实现锚点链接之间的平滑滚动
  4. ThinkPHP去重 distinct和group by
  5. 137. Single Number II
  6. C#中的占位符
  7. hdu 1241 Oil Deposits(DFS求连通块)
  8. LabView培训
  9. nginx相关参考博客
  10. Windows系统命令行net user命令用法
  11. less和scss
  12. (转载)SQL Server2008附加数据库之后显示为只读时解决方法
  13. redis 的简单使用
  14. bzo1606: [Usaco2008 Dec]Hay For Sale 购买干草
  15. [ZJOI2012]波浪弱化版(带技巧的DP)
  16. Segments
  17. 洛谷 P1350 车的放置
  18. 多线程编程中条件变量和的spurious wakeup 虚假唤醒
  19. 去掉if
  20. Optimal Milking(POJ2112+二分+Dinic)

热门文章

  1. Spring Boot中@Scheduled注解的使用方法
  2. MVC 统一验证Token demo
  3. ECOS问题解决记录
  4. .Net Core使用分布式缓存Redis:Lua脚本
  5. 洛谷p1502窗口的星星 扫描线
  6. 死磕面试 - Dubbo基础知识37问(必须掌握)
  7. 150行代码打造.net core生产力工具,你值得拥有
  8. Alibaba Nacos 服务发现组件集群部署
  9. 在eclipse里用jdbc连接MySQL
  10. 关于idea修改当前使用的git账户的问题