ACM-BFS之Dating with girls(2)——hdu2579
2014-04-25 21:27
423 查看
Dating with girls(2)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2579
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1959 Accepted Submission(s): 551
Problem Description
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.
Input
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.
Output
For each cases, if you can find the girl, output the least time in seconds, else output "Please give me another chance!".
Sample Input
1
6 6 2
...Y..
...#..
.#....
...#..
...#..
..#G#.
Sample Output
7
Dating with girls之续,两道题除了故事上有衔接,其他好像没有相关的地方。。。
上一道题是排序+二分查找,这道题就是BFS。
但是,与简单的BFS不同的是,“墙” 有时候会消失,所以,一般除了极个别情况,一般都能约会成功。
这道题,从题意中可以明白,每个格子并非只能走一次。
所以这个遍历判断数组VIS需要变化一下。
曾经也做过类似的,以一个条件为基础。
条件:这次到达这里的时间短语曾经到达这里的时间,则可以走这个格子。
所以要建立一个三维数组,除了横纵坐标外,另一个就是step%k。
数组内容就是到达这个格子相应最短时间。
恩,约个会,追个妹纸真不容易啊,悲剧的程序猿= =。
/*
Author:Tree
From: http://blog.csdn.net/lttree Dating with girls2
hdu 2579
BFS——广度优先搜索
VIS数组变种
*/
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
struct Coor
{
int x,y,step;
};
int n,m,k,f_x,f_y,dis[4][2]={1, 0, 0, -1, -1, 0, 0, 1};
bool flag;
int vis[111][111][11];
char mapp[111][111];
void bfs(int x,int y)
{
queue <Coor> q;
int i;
Coor pre,lst;
memset(vis,-1,sizeof(vis));
pre.x=x;
pre.y=y;
pre.step=0;
q.push(pre);
while( !q.empty() )
{
pre=q.front();
q.pop();
if(pre.x==f_x && pre.y==f_y)
{
flag=1;
cout<<pre.step<<endl;
return;
}
for(i=0;i<4;++i)
{
lst.x=pre.x+dis[i][0];
lst.y=pre.y+dis[i][1];
lst.step=pre.step+1;
// 越界
if( lst.x<0 || lst.y<0 || lst.x>=n || lst.y>=m ) continue;
// 下一步是石头,但是不消失
if( mapp[lst.x][lst.y]=='#' && lst.step%k!=0 ) continue;
// 判断下一步是否曾经走过,若走过,当step%k相同时所到达时间必须少于曾经走到此处时间
if( vis[lst.x][lst.y][lst.step%k]!=-1 && vis[lst.x][lst.y][lst.step%k]<=lst.step ) continue;
vis[lst.x][lst.y][lst.step%k]=lst.step;
q.push(lst);
}
}
}
int main()
{
int i,j,test;
int s_x,s_y;
cin>>test;
while( test-- )
{
cin>>n>>m>>k;
for(i=0;i<n;++i)
for(j=0;j<m;++j)
{
cin>>mapp[i][j];
if(mapp[i][j]=='Y')
{s_x=i;s_y=j;}
else if(mapp[i][j]=='G')
{f_x=i;f_y=j;}
}
flag=0;
bfs(s_x,s_y);
if(!flag) cout<<"Please give me another chance!"<<endl;
}
return 0;
}
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2579
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1959 Accepted Submission(s): 551
Problem Description
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.
Input
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.
Output
For each cases, if you can find the girl, output the least time in seconds, else output "Please give me another chance!".
Sample Input
1
6 6 2
...Y..
...#..
.#....
...#..
...#..
..#G#.
Sample Output
7
Dating with girls之续,两道题除了故事上有衔接,其他好像没有相关的地方。。。
上一道题是排序+二分查找,这道题就是BFS。
但是,与简单的BFS不同的是,“墙” 有时候会消失,所以,一般除了极个别情况,一般都能约会成功。
这道题,从题意中可以明白,每个格子并非只能走一次。
所以这个遍历判断数组VIS需要变化一下。
曾经也做过类似的,以一个条件为基础。
条件:这次到达这里的时间短语曾经到达这里的时间,则可以走这个格子。
所以要建立一个三维数组,除了横纵坐标外,另一个就是step%k。
数组内容就是到达这个格子相应最短时间。
恩,约个会,追个妹纸真不容易啊,悲剧的程序猿= =。
/*
Author:Tree
From: http://blog.csdn.net/lttree Dating with girls2
hdu 2579
BFS——广度优先搜索
VIS数组变种
*/
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
struct Coor
{
int x,y,step;
};
int n,m,k,f_x,f_y,dis[4][2]={1, 0, 0, -1, -1, 0, 0, 1};
bool flag;
int vis[111][111][11];
char mapp[111][111];
void bfs(int x,int y)
{
queue <Coor> q;
int i;
Coor pre,lst;
memset(vis,-1,sizeof(vis));
pre.x=x;
pre.y=y;
pre.step=0;
q.push(pre);
while( !q.empty() )
{
pre=q.front();
q.pop();
if(pre.x==f_x && pre.y==f_y)
{
flag=1;
cout<<pre.step<<endl;
return;
}
for(i=0;i<4;++i)
{
lst.x=pre.x+dis[i][0];
lst.y=pre.y+dis[i][1];
lst.step=pre.step+1;
// 越界
if( lst.x<0 || lst.y<0 || lst.x>=n || lst.y>=m ) continue;
// 下一步是石头,但是不消失
if( mapp[lst.x][lst.y]=='#' && lst.step%k!=0 ) continue;
// 判断下一步是否曾经走过,若走过,当step%k相同时所到达时间必须少于曾经走到此处时间
if( vis[lst.x][lst.y][lst.step%k]!=-1 && vis[lst.x][lst.y][lst.step%k]<=lst.step ) continue;
vis[lst.x][lst.y][lst.step%k]=lst.step;
q.push(lst);
}
}
}
int main()
{
int i,j,test;
int s_x,s_y;
cin>>test;
while( test-- )
{
cin>>n>>m>>k;
for(i=0;i<n;++i)
for(j=0;j<m;++j)
{
cin>>mapp[i][j];
if(mapp[i][j]=='Y')
{s_x=i;s_y=j;}
else if(mapp[i][j]=='G')
{f_x=i;f_y=j;}
}
flag=0;
bfs(s_x,s_y);
if(!flag) cout<<"Please give me another chance!"<<endl;
}
return 0;
}
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- ACM posters
- zoj3549 快速幂
- 浅谈manacher算法 最长回文子串(Longest Palindromic Substring)
- 前缀表达式,中缀表达式,后缀表达式转化和计算
- c/c++中让输入以回车换行键结束输入
- 图论学习笔记之一——Floyd算法
- 用单调栈解决最大连续矩形面积问题
- NWERC2010 NKOJ2178 Stock Prices
- 2011ACM福州网络预选赛B题 HDU4062 Abalone
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)