热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

poj2049FindingNemo

FindingNemoTimeLimit:2000MSMemoryLimit:30000KTotalSubmissions:7372Accepted:1714Descripti
Finding Nemo
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 7372   Accepted: 1714

Description

Nemo is a naughty boy. One day he went into the deep sea all by himself. Unfortunately, he became lost and couldn‘t find his way home. Therefore, he sent a signal to his father, Marlin, to ask for help.
After checking the map, Marlin found that the sea is like a labyrinth with walls and doors. All the walls are parallel to the X-axis or to the Y-axis. The thickness of the walls are assumed to be zero.
All the doors are opened on the walls and have a length of 1. Marlin cannot go through a wall unless there is a door on the wall. Because going through a door is dangerous (there may be some virulent medusas near the doors), Marlin wants to go through as few doors as he could to find Nemo.
Figure-1 shows an example of the labyrinth and the path Marlin went through to find Nemo.
,

We assume Marlin‘s initial position is at (0, 0). Given the position of Nemo and the configuration of walls and doors, please write a program to calculate the minimum number of doors Marlin has to go through in order to reach Nemo.

Input

The input consists of several test cases. Each test case is started by two non-negative integers M and N. M represents the number of walls in the labyrinth and N represents the number of doors.
Then follow M lines, each containing four integers that describe a wall in the following format:
x y d t
(x, y) indicates the lower-left point of the wall, d is the direction of the wall -- 0 means it‘s parallel to the X-axis and 1 means that it‘s parallel to the Y-axis, and t gives the length of the wall.
The coordinates of two ends of any wall will be in the range of [1,199].
Then there are N lines that give the description of the doors:
x y d
x, y, d have the same meaning as the walls. As the doors have fixed length of 1, t is omitted.
The last line of each case contains two positive float numbers:
f1 f2
(f1, f2) gives the position of Nemo. And it will not lie within any wall or door.
A test case of M = -1 and N = -1 indicates the end of input, and should not be processed.

Output

For each test case, in a separate line, please output the minimum number of doors Marlin has to go through in order to rescue his son. If he can‘t reach Nemo, output -1.

Sample Input

8 9
1 1 1 3
2 1 1 3
3 1 1 3
4 1 1 3
1 1 0 3
1 2 0 3
1 3 0 3
1 4 0 3
2 1 1
2 2 1
2 3 1
3 1 1
3 2 1
3 3 1
1 2 0
3 3 0
4 3 1
1.5 1.5
4 0
1 1 0 1
1 1 1 1
2 1 1 1
1 2 0 1
1.5 1.7
-1 -1

Sample Output

5
-1

这个题光建图就坑了我很长时间,orz。。
大体意思是儿子掉水里了,爸爸去救,爸爸发现这片水域像个迷宫,有墙有门还有空地,问他爸爸最少经过几道门才能救出他儿子。

拿测试数据来说明我的建图方式:
  x y d t 表示坐标为xy,d表示平行于哪个坐标轴,t表示这个方向上的长度
来看图
  

,

我们以Nemo方格点为(1,1)  它上面的点为(1,2),这样以此类推,每个方格用一个3维数组表示: d[x][y][4] ,x,y表示坐标,4分别表示上左下右四个方向,
而d[x][y][x] 表示x方向上是门,是墙还是空地。
而建图的过程就是
void BuildGrap()
{
    for(int i = 0; i <200; i++){
        for(int j = 0; j <200; j++){
            d[i][j][0] = s[i][j+1][0];
            d[i][j][1] = s[i+1][j][1];
            d[i][j][3] = s[i][j][0];
            d[i][j][2] = s[i][j][1];

        }
    }
}
此外此题我是倒着做的,从nemo所处位置开始搜索,一直搜到最外围,然后可能存在多种情况,所以用优先队列可以确定第一个到达外围的步数。


,,
  1 /*======================================================================
  2  *           Author :   kevin
  3  *         Filename :   FindingNemo.cpp
  4  *       Creat time :   2014-05-26 20:27
  5  *      Description :
  6 ========================================================================*/
  7 #include 
  8 #include 
  9 #include 
 10 #include 
 11 #include 
 12 #include 
 13 #define clr(a,b) memset(a,b,sizeof(a))
 14 #define M 205
 15 #define INF 0x7f7f7f7f
 16 using namespace std;
 17 int d[M][M][4],s[M][M][2];
 18 int vis[M][M],cnt[M][M];
 19 int lastx,lasty,maxx,maxy;
 20 int dir[4][2] = {{0,1},{1,0},{-1,0},{0,-1}};
 21 struct Node     //优先队列,按cnt统计的步数排序
 22 {
 23     int x,y;
 24     bool operator <(const Node &a) const{
 25         return cnt[a.x][a.y] < cnt[x][y];
 26     }
 27 }node;
 28 void BFS()
 29 {
 30     priority_queueque;
 31     Node temp;
 32     node.x = lastx; node.y = lasty;
 33     que.push(node);
 34     vis[lastx][lasty] = 1;
 35     while(que.empty() != true){
 36         int x,y;
 37         temp = que.top();
 38         que.pop();
 39         for(int i = 0; i <4; i++){
 40             x = temp.x + dir[i][0]; y = temp.y + dir[i][1];
 41             if(x <0 || y <0) continue;
 42             if(x > 199 || y > 199) continue;
 43             if(d[x][y][3-i] != 1){  //若不是墙
 44                 node.x = x; node.y = y;
 45                 if(d[x][y][3-i] == 0){  //若是门
 46                     if(vis[x][y]) //若被访问过,更新cnt值
 47                         cnt[x][y] = min(cnt[x][y],cnt[temp.x][temp.y]+1);
 48                     else{
 49                         cnt[x][y] = cnt[temp.x][temp.y]+1;
 50                         que.push(node);
 51                     }
 52                 }
 53                 else{ //若是空地
 54                     if(vis[x][y])//若被访问过,更新cnt值
 55                         cnt[x][y] = min(cnt[x][y],cnt[temp.x][temp.y]);
 56                     else{
 57                         cnt[x][y] = cnt[temp.x][temp.y];
 58                         que.push(node);
 59                     }
 60                 }
 61                 vis[x][y] = 1; //访问标记
 62                 if(x > maxx || y > maxy){ //达到边界,将步数更新到cnt[0][0]
 63                     cnt[0][0] = min(cnt[0][0],cnt[x][y]);
 64                     break;
 65                 }
 66             }
 67         }
 68     }
 69 }
 70 void BuildGrap()  //建图
 71 {
 72     for(int i = 0; i <200; i++){
 73         for(int j = 0; j <200; j++){
 74             d[i][j][0] = s[i][j+1][0];
 75             d[i][j][1] = s[i+1][j][1];
 76             d[i][j][3] = s[i][j][0];
 77             d[i][j][2] = s[i][j][1];
 78 
 79         }
 80     }
 81 }
 82 int main(int argc,char *argv[])
 83 {
 84     int n,m,a,b,dd,t;
 85     double xx,yy;
 86     while(scanf("%d%d",&n,&m)!=EOF){
 87         clr(d,-1);
 88         clr(s,-1);
 89         clr(vis,0);
 90         clr(cnt,0);
 91         if(n == -1 && m == -1) break;
 92         maxx = maxy = 0;
 93         for(int i = 0; i ){
 94             scanf("%d%d%d%d",&a,&b,&dd,&t);
 95             if(dd == 1){
 96                 for(int k = 0; k ){
 97                     s[a][b][dd] = 1;
 98                     b++;
 99                 }
100             }
101             if(dd == 0){
102                 for(int k = 0; k ){
103                     s[a][b][dd] = 1;
104                     a++;
105                 }
106             }
107             if(maxx < a){
108                 maxx = a;
109             }
110             if(maxy < b){
111                 maxy = b;
112             }
113         }
114         for(int i = 0; i ){
115             scanf("%d%d%d",&a,&b,&dd);
116             s[a][b][dd] = 0;
117         }
118         scanf("%lf%lf",&xx,&yy);
119         lastx = floor(xx);
120         lasty = floor(yy);
121         if(lastx <0 || lastx > 199 || lasty <0 || lasty > 199){
122             printf("0\n");
123             continue;
124         }
125         BuildGrap();
126         cnt[0][0] = INF;
127         BFS();
128         if(cnt[0][0] == INF){
129             printf("-1\n");
130         }
131         else{
132             printf("%d\n",cnt[0][0]);
133         }
134     }
135     return 0;
136 }
View Code

 

 

poj 2049 -- Finding Nemo,,

poj 2049 -- Finding Nemo


推荐阅读
  • SQL Server 2008 到底需要使用哪些端口?
    SQLServer2008到底需要使用哪些端口?-下面就来介绍下SQLServer2008中使用的端口有哪些:  首先,最常用最常见的就是1433端口。这个是数据库引擎的端口,如果 ... [详细]
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了brain的意思、读音、翻译、用法、发音、词组、同反义词等内容,以及脑新东方在线英语词典的相关信息。还包括了brain的词汇搭配、形容词和名词的用法,以及与brain相关的短语和词组。此外,还介绍了与brain相关的医学术语和智囊团等相关内容。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • Android日历提醒软件开源项目分享及使用教程
    本文介绍了一款名为Android日历提醒软件的开源项目,作者分享了该项目的代码和使用教程,并提供了GitHub项目地址。文章详细介绍了该软件的主界面风格、日程信息的分类查看功能,以及添加日程提醒和查看详情的界面。同时,作者还提醒了读者在使用过程中可能遇到的Android6.0权限问题,并提供了解决方法。 ... [详细]
  • 本文介绍了贝叶斯垃圾邮件分类的机器学习代码,代码来源于https://www.cnblogs.com/huangyc/p/10327209.html,并对代码进行了简介。朴素贝叶斯分类器训练函数包括求p(Ci)和基于词汇表的p(w|Ci)。 ... [详细]
  • ShiftLeft:将静态防护与运行时防护结合的持续性安全防护解决方案
    ShiftLeft公司是一家致力于将应用的静态防护和运行时防护与应用开发自动化工作流相结合以提升软件开发生命周期中的安全性的公司。传统的安全防护方式存在误报率高、人工成本高、耗时长等问题,而ShiftLeft提供的持续性安全防护解决方案能够解决这些问题。通过将下一代静态代码分析与应用开发自动化工作流中涉及的安全工具相结合,ShiftLeft帮助企业实现DevSecOps的安全部分,提供高效、准确的安全能力。 ... [详细]
  • 本文讨论了在使用Git进行版本控制时,如何提供类似CVS中自动增加版本号的功能。作者介绍了Git中的其他版本表示方式,如git describe命令,并提供了使用这些表示方式来确定文件更新情况的示例。此外,文章还介绍了启用$Id:$功能的方法,并讨论了一些开发者在使用Git时的需求和使用场景。 ... [详细]
  • 由于同源策略的限制,满足同源的脚本才可以获取资源。虽然这样有助于保障网络安全,但另一方面也限制了资源的使用。那么如何实现跨域呢,以下是实现跨域的一些方法。 ... [详细]
author-avatar
Bboy龙超
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有