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