hdoj 2579 Dating with girls(2) 【bfs(标记状态)】
2015-11-13 22:12
330 查看
Dating with girls(2)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2707 Accepted Submission(s): 761
[align=left]Problem Description[/align]
If you have solved the problem Dating with girls(1).I think you can solve this problem too.This problem is also about dating with girls. Now you are in a maze and the girl you want to date with is also in the maze.If you can find
the girl, then you can date with the girl.Else the girl will date with other boys. What a pity!
The Maze is very strange. There are many stones in the maze. The stone will disappear at time t if t is a multiple of k(2<= k <= 10), on the other time , stones will be still there.
There are only ‘.’ or ‘#’, ’Y’, ’G’ on the map of the maze. ’.’ indicates the blank which you can move on, ‘#’ indicates stones. ’Y’ indicates the your location. ‘G’ indicates the girl's location . There is only one ‘Y’ and one ‘G’. Every seconds you can move
left, right, up or down.
![](http://acm.hdu.edu.cn/data/images/C-191-1008-1.png)
[align=left]Input[/align]
The first line contain an integer T. Then T cases followed. Each case begins with three integers r and c (1 <= r , c <= 100), and k(2 <=k <= 10).
The next r line is the map’s description.
[align=left]Output[/align]
For each cases, if you can find the girl, output the least time in seconds, else output "Please give me another chance!".
[align=left]Sample Input[/align]
1 6 6 2 ...Y.. ...#.. .#.... ...#.. ...#.. ..#G#.
[align=left]Sample Output[/align]
7
[align=left]Source[/align]
HDU 2009-5 Programming Contest
思路:
这道题和之前做的题的区别就在于石头会消失,那么也就是在需要用vis不仅要标记位置是否走过,而且要将时间和位置同时标记,而且在mp等于#的时候如果时间能够整除k,那么就能过!其他的和之前做的bfs一模一样!
代码:
#include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #define INF 0x3f3f3f3f using namespace std; int r,c,k; char mp[105][105]; int vis[105][105][15]; int x,y,ex,ey; int dx[4]={1,0,0,-1}; int dy[4]={0,1,-1,0}; int ans; struct node { int x,y,step; }a,temp; int judge() { if(temp.x<1||temp.x>r) return 0; if(temp.y<1||temp.y>c) return 0; if(mp[temp.x][temp.y]=='#'&&temp.step%k!=0) return 0; if(vis[temp.x][temp.y][temp.step%k]==1) return 0; if(temp.step>=ans) return 0; } void bfs() { a.x=x; a.y=y; a.step=0; queue<node>q; q.push(a); memset(vis,0,sizeof(vis)); vis[x][y][0]=1; while(!q.empty()) { a=q.front(); q.pop(); for(int i=0;i<4;i++) { temp.x=a.x+dx[i]; temp.y=a.y+dy[i]; temp.step=a.step+1; if(judge()) { if(temp.x==ex&&temp.y==ey) { if(ans>temp.step) { ans=temp.step; } continue; } q.push(temp); vis[temp.x][temp.y][temp.step%k]=1; } } } } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d%d",&r,&c,&k); getchar(); for(int i=1;i<=r;i++) { scanf("%s",mp[i]+1); for(int j=1;j<=c;j++) { if(mp[i][j]=='Y') { x=i; y=j; } else if(mp[i][j]=='G') { ex=i; ey=j; } } } ans=INF; bfs(); if(ans==INF) { printf("Please give me another chance!\n"); } else { printf("%d\n",ans); } } return 0; }
相关文章推荐
- 不知所终的一周
- JAVA C# C/C++之比较学:初始化顺序
- Day6
- iOS中网络请求--AFNetworking
- 关于开源分布式文件系统的性能调优方法
- 类和对象 引用属性和方法举例
- Hadoop问题集锦
- vs2013下如何调试main带参数的程序
- 安卓按两次返回键退出程序
- Yii框架的一些系统函数
- normalize()函数的使用方法
- 第2章 一切都为对象
- javaScript-变量
- android studio gradle更新错误
- Volley框架之使用GET取得Cookie
- 杭电1075 What Are You Talking About
- SQL 左外连接,右外连接,全连接,内连接
- 杭电1251
- 剑指Offer-17-合并有序链表
- 机器学习实战(三)