hdoj 2579 Dating with girls(2)【三重数组标记去重】
Dating with girls(2)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2705 Accepted Submission(s):
759
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.
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.
least time in seconds, else output "Please give me another chance!".
题意:给一个迷宫Y是起点,G是终点,#是石头不能通过 .是路可以走,但是当走到的步数step%k==0时#全部变为路可以通过,问从起点到终点的最少步数
题解:此题不用标记走过的路,但要避免走的路径重复走,同一个路径只在同一个时刻走过(当再次走到这个点且step%k刚好再次与此时相同时 不可以走),用一个三维数组标记
从起点到终点,bfs,不同的是对于图中的点,可能走多次,分别是在不同的时刻。
vis[ t%k ][ i ][ j ]=1:表示坐标( i,j )在 t %k 时刻走过,接下来再出现 t%k 就不用再走一次了。
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define MAX 110
#define INF 0x7ffff
using namespace std;
int n,m,k;
char map[MAX][MAX];
int vis[MAX][MAX][MAX];
int b,e;
struct node
{
int x,y;
int step;
friend bool operator <(node a,node b)
{
return a.step>b.step;
}
};
int judge(int a,int b)
{
if(a<0||a>=n||b<0||b>=m)
return 0;
return 1;
}
void bfs()
{
int move[4][2]={1,0,-1,0,0,1,0,-1};
node beg,end;
priority_queue<node>q;
while(!q.empty())
q.pop();
memset(vis,0,sizeof(vis));
beg.x=b;
beg.y=e;
beg.step=0;
vis[0][b][e]=1;
q.push(beg);
while(!q.empty())
{
end=q.top();
q.pop();
if(map[end.x][end.y]=='G')
{
printf("%d\n",end.step);
return ;
}
for(int i=0;i<4;i++)
{
beg.x=end.x+move[i][0];
beg.y=end.y+move[i][1];
if(judge(beg.x,beg.y))
{
if(map[beg.x][beg.y]!='#')
{
beg.step=end.step+1;
if(!vis[beg.step%k][beg.x][beg.y])
{
vis[beg.step%k][beg.x][beg.y]=1;
q.push(beg);
}
}
else
{
beg.step=end.step+1;
if(!vis[beg.step%k][beg.x][beg.y]&&beg.step%k==0)
{
vis[beg.step%k][beg.x][beg.y]=1;
q.push(beg);
}
}
}
}
}
printf("Please give me another chance!\n");
}
int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
for(i=0;i<n;i++)
scanf("%s",map[i]);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(map[i][j]=='Y')
{
b=i;
e=j;
}
}
}
bfs();
}
return 0;
}
hdoj 2579 Dating with girls(2)【三重数组标记去重】的更多相关文章
- hdu 2579 Dating with girls(2)
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2579 Dating with girls(2) Description If you have sol ...
- hdu 2579 Dating with girls(2) (bfs)
Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 【HDOJ】2579 Dating with girls(2)
简单BFS. /* 2579 */ #include <iostream> #include <queue> #include <cstdio> #include ...
- hdu 2578 Dating with girls(1) (hash)
Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 2578 Dating with girls(1) [补7-26]
Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 3784 继续xxx定律 & HDU 2578 Dating with girls(1)
HDU 3784 继续xxx定律 HDU 2578 Dating with girls(1) 做3748之前要先做xxx定律 对于一个数n,如果是偶数,就把n砍掉一半:如果是奇数,把n变成 3*n+ ...
- hdu 2578 Dating with girls(1) 满足条件x+y=k的x,y有几组
Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu 2578 Dating with girls(1)
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2578 Dating with girls(1) Description Everyone in the ...
- Dating with girls(1)(二分+map+set)
Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
随机推荐
- VSCode调试go
VSCode调试go语言出现:exec: "gcc": executable file not found in %PATH% 1.问题描述 由于安装VS15 Preview ...
- SmartGit初步使用
在Git如日中天的今天,我也不免俗的想用Git将业余时间写的代码管理一下. 什么是Git这里不多说,具体见廖雪峰的Git教程,ProGit等详细教程. 我们这里直接上手. 一.下载Git客户端 1.G ...
- js和Jquery获取选中select值和文本
<body> <select name="PaymentType" style="width:110px" > <option v ...
- hdu 4778
知道是状态压缩,但是不会做: 看题解学的: dp[i]表示现在状态是i,先手-后手的分数. #include<cstdio> #include<cstring> #includ ...
- JavaScript 按回车键提交搜索表单 easyui ajax方式
<!-- 搜索框 --> <form id="bUserSearchForm" style="padding:8px;" onkeydown= ...
- 使用div+css制作简单导航 以及要注意问题
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 如何登录mysql? cmd怎么连接mysql数据库
Mysql开源数据库,任何人都可以下载安装使用.那么安装好的mysql如何登陆连接mysql数据库呢? 连接mysql数据库的几种方法 一 Mysql命令行连接 一般对于刚刚安装好的mysql,如果勾 ...
- JAVA HASHMAP 如何用
HASHMAP最好与实例联系起来..它主要存的是键与值的关系. 举个例子如你现在有一个学生类import java.util.HashMap;public class Student {String ...
- Android-加载透明PNG图片变黑的问题
png和jpg作为两种最常用的图片格式,首先我们要知道他们的区别: 1.从一般图片的外观上来说,他们是无法直接判断的 2.从文件大小上来说,同样一张图png肯定比jpg的大 3.通过查资料咱们可以发现 ...
- BGP详解
相信各位站长在托管服务器或者选择虚拟主机的时候,提供商都会说他们的机房是双线机房,保证你的站点访问速度,那么这里所谓的双线机房到底是何意思,它又为何能提升站点的访问速度呢? 一遍小型机房的所谓双线路其 ...