hdu2579 BFS
来源:互联网 发布:淘宝优惠券的评价软件 编辑:程序博客网 时间:2024/05/16 12:31
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2579
广搜,只不过可以往回走,所以标记数组也从二维数组变成三维数组。
Ac code:
#include <iostream>#include <queue>#include <cstring>using namespace std;int r,c,k;#define N 105bool step[N][N][12];char Maze[N][N];struct Node {int x,y,step;};Node start,End;int d[4][2]={-1,0,1,0,0,1,0,-1};int BFS();bool IsValid(int x,int y,int s);int main(){int test,i,j,rs;cin>>test;while (test--){memset(step,0,sizeof(step));memset(Maze,0,sizeof(Maze));cin>>r>>c>>k;for(i=0;i<r;i++)for(j=0;j<c;j++){cin>>Maze[i][j];if(Maze[i][j]=='Y') { start.x=i; start.y=j;start.step=0;}if(Maze[i][j]=='G') { End.x=i; End.y=j;}}rs=BFS();if(rs) cout<<rs<<endl;else cout<<"Please give me another chance!"<<endl;}}bool IsValid(int x,int y,int s){if(x<0||x>=r||y<0||y>=c) return false;if(Maze[x][y]=='#'&&s%k!=0) return false;return true;}int BFS(){queue<Node> Q;Q.push(start);Node temp,t;int dx,dy,i,s;while (!Q.empty()){temp=Q.front();Q.pop();if(temp.x==End.x&&temp.y==End.y) return temp.step;for(i=0;i<4;i++){dx=temp.x+d[i][0];dy=temp.y+d[i][1];s=temp.step+1; if(IsValid(dx,dy,s)&&!step[dx][dy][s%k]) { t.x=dx; t.y=dy; t.step=s; Q.push(t); step[dx][dy][s%k]=1; }}}return 0;}
- hdu2579 BFS
- HDU2579(BFS)
- hdu2579之BFS
- hdu2579
- hdu2579 Dating with girls(2)--BFS
- HDU2579 Dating with girls(2) BFS
- ACM-BFS之Dating with girls(2)——hdu2579
- 动态规划三维 hdu2579
- BFS
- bfs
- BFS
- BFS
- bfs
- BFS
- bfs
- BFS
- BFS
- bfs
- [LeetCode]Search in Rotated Sorted Array II
- ZJUT 1151 谜之数独
- wikioi-1014 装箱问题
- [LeetCode]Search Insert Position
- android中LayoutInflater以及inflate
- hdu2579 BFS
- jasperreport导出PDF exportReportToPdf方法不执行
- [LeetCode]Set Matrix Zeroes
- 消灭程序员需要百年吗?
- [LeetCode]Simplify Path
- js 的encodeURIComponent() 处理ajax请求url中的 “+ ? ; = @ $ #”等等特殊字符
- [LeetCode]Sort Colors
- Windows游戏编程大师技巧(第二版)笔记-第一章
- openstack部署心得