当前位置: 首页 > 编程日记 > 正文

迷宫寻宝(搜索)

迷宫寻宝(一)

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
描述

一个叫ACM的寻宝者找到了一个藏宝图,它根据藏宝图找到了一个迷宫,这是一个很特别的迷宫,迷宫里有N个编过号的门(N<=5),它们分别被编号为A,B,C,D,E.为了找到宝藏,ACM必须打开门,但是,开门之前必须在迷宫里找到这个打开这个门所需的所有钥匙(每个门都至少有一把钥匙),例如:现在A门有三把钥匙,ACM就必须找全三把钥匙才能打开A门。现在请你编写一个程序来告诉ACM,他能不能顺利的得到宝藏。

输入
输入可能会有多组测试数据(不超过10组)。
每组测试数据的第一行包含了两个整数M,N(1<N,M<20),分别代表了迷宫的行和列。接下来的M每行有N个字符,描述了迷宫的布局。其中每个字符的含义如下:
.表示可以走的路
S:表示ACM的出发点
G表示宝藏的位置
X表示这里有墙,ACM无法进入或者穿过。
A,B,C,D,E表示这里是门,a,b,c,d,e表示对应大写字母的门上的钥匙。
注意ACM只能在迷宫里向上下左右四个方向移动。

最后,输入0 0表示输入结束。
输出
每行输出一个YES表示ACM能找到宝藏,输出NO表示ACM找不到宝藏。
样例输入
4 4 
S.X. 
a.X. 
..XG 
.... 
3 4 
S.Xa 
.aXB 
b.AG 
0 0
样例输出
YES 
NO

一直找不到WA点,此题测试数据有问题,两个AC代码所得结果不同
  1 #include <iostream>
  2 #include <algorithm>
  3 #include <cstdio>
  4 #include <cstring>
  5 using namespace std;
  6 struct gate
  7 {
  8     char name;
  9     int num;
 10 }men[5];
 11 int dirx[4]={0,0,-1,1};
 12 int diry[4]={1,-1,0,0};
 13 char map[21][21];
 14 int m,n;
 15 int Num[5];
 16 char cha[6]="abcde";
 17 int sx,sy,gx,gy;
 18 bool flag=0;
 19 int visi[21][21];
 20 void init()
 21 {
 22     memset(Num,0,sizeof(Num));
 23     memset(visi,0,sizeof(visi));
 24     memset(map,0,sizeof(map));
 25     for(int i=0;i<5;i++)
 26         men[i].name=cha[i];
 27     for(int i=0;i<5;i++)
 28         men[i].num=0;
 29     flag=0;
 30 }
 31 int dfs(int x,int y)    /*去找钥匙*/
 32 {
 33     int i,j;
 34     if(x<0||x>=m||y>=n||y<0||visi[x][y]||map[x][y]=='X')
 35         return 0;
 36     for(i=0;i<5;i++)
 37     {
 38         if((cha[i]-('a'-'A'))==map[x][y])
 39         {
 40             if(Num[i]!=men[i].num)
 41                 return 0;
 42             if(Num[i]==0&&men[i].num==0)
 43                 return 0;
 44             map[x][y]='.';
 45         }
 46 
 47     }
 48     visi[x][y]=1;
 49     for(i=0;i<5;i++)
 50     {
 51         if(map[x][y]==cha[i])
 52         {
 53             Num[i]++;
 54             map[x][y]='.';
 55         }
 56     }
 57     for(i=0;i<4;i++)
 58     {
 59         int dx=x+dirx[i];
 60         int dy=y+diry[i];
 61         dfs(dx,dy);
 62     }
 63     return 0;
 64 }
 65 int judge(int x,int y)
 66 {
 67     for(int i=0;i<5;i++)
 68         if(map[x][y]==(cha[i]-('a'-'A')))
 69         {
 70             if(Num[i]==men[i].num)
 71             {
 72                 if(Num[i]==0)
 73                     return 1;
 74                 map[x][y]='.';
 75                 return 0;
 76             }
 77             else 
 78                 return 1;
 79         }
 80         return 0;        /*能走*/
 81 }
 82 void DFS(int x,int y)
 83 {
 84     int i,j;
 85     if(x<0||x>=m||y>=n||y<0||map[x][y]=='X'||judge(x,y))
 86         return;
 87     if(flag)
 88         return;
 89     if(x==gx&&y==gy)
 90     {
 91         flag=1;
 92         //cout<<"zhong";
 93     //    cout<<x<<y<<endl;
 94         return;
 95     }
 96     map[x][y]='X';
 97     for(i=0;i<4;i++)
 98     {
 99         int dx=x+dirx[i];
100         int dy=y+diry[i];            
101         DFS(dx,dy);
102     }
103     map[x][y]='.';
104     return;
105 }
106 /*void bianli()
107 {
108     for(int i=0;i<m;i++)
109     {
110         for(int j=0;j<n;j++)
111             cout<<map[i][j];
112         cout<<endl;
113     }
114 }*/
115 int main()
116 {
117     int i,j,k;
118     //freopen("in.txt","r",stdin);
119     while(cin>>m>>n)
120     {
121         if(m==0&&n==0)
122             break;
123         init();
124         for(i=0;i<m;i++)
125         {
126             for(j=0;j<n;j++)
127             {
128                 cin>>map[i][j];
129                 if(map[i][j]=='S'){sx=i,sy=j;}
130                 if(map[i][j]=='G'){gx=i,gy=j;}
131                 for(k=0;k<5;k++)             //求各钥匙实际数目
132                     if(map[i][j]==cha[k])
133                         men[k].num++;        
134             }
135         }
136         /*for(i=0;i<5;i++)
137             cout<<men[i].num<<" ";
138         cout<<endl;*/
139         dfs(sx,sy);
140         /*for(i=0;i<5;i++)
141             cout<<Num[i]<<" ";
142         cout<<endl;*/
143         DFS(sx,sy);
144                 //bianli();
145         if(flag)
146             cout<<"YES"<<endl;
147         else
148             cout<<"NO"<<endl;
149     }    
150 }

别人的AC代码

  1 #include <iostream>
  2 #include <cstring>
  3 
  4 using namespace std;
  5 
  6 struct node
  7 {
  8     int x;
  9     int y;
 10     int num;
 11 }q[5];
 12 char map[25][25];
 13 int maxkeyOfDoor[5];    //存储打开一个门所有的钥匙数
 14 int findkeyOfDoor[5];   //存储现今已经找到的钥匙数
 15 bool flag;
 16 
 17 void DFS(int i, int j);
 18 void check();
 19 
 20 void DFS(int i, int j)
 21 {
 22     if(map[i][j] != 'X')
 23     {
 24         switch (map[i][j])
 25         {
 26             case 'a':
 27             case 'b':
 28             case 'c':
 29             case 'd':
 30             case 'e':
 31                 ++findkeyOfDoor[map[i][j]-'a'];
 32                 break;
 33             case 'A':
 34             case 'B':
 35             case 'C':
 36             case 'D':
 37             case 'E':
 38                 q[map[i][j] - 'A'].x = i;
 39                 q[map[i][j] - 'A'].y = j;
 40                 ++q[map[i][j] - 'A'].num;
 41                 return;
 42             case 'G':
 43                 flag = true;
 44                 return;
 45         }
 46         map[i][j] = 'X';   // 标记为已走
 47         DFS(i-1, j);
 48         DFS(i+1, j);
 49         DFS(i, j-1);
 50         DFS(i, j+1);
 51         check();
 52     }
 53 }
 54 
 55 void check()
 56 {
 57     for(int i = 0; i < 5; ++i)
 58     {
 59         if(q[i].num)
 60         {
 61             if(findkeyOfDoor[i] == maxkeyOfDoor[i])  //说明打开该门所需的钥匙已经全部找到,即可以打开门
 62             {
 63                 map[q[i].x][q[i].y] = 'X';
 64                 DFS(q[i].x-1, q[i].y);
 65                 DFS(q[i].x+1, q[i].y);
 66                 DFS(q[i].x, q[i].y-1);
 67                 DFS(q[i].x, q[i].y+1);
 68             }
 69         }
 70     }
 71 }
 72 
 73 int main()
 74 {
 75     int tx, ty, m, n;
 76    // freopen("in.txt","r",stdin);
 77     while(cin >> m >> n && (m || n))
 78     {
 79         flag = false;
 80         memset(maxkeyOfDoor, 0, sizeof(maxkeyOfDoor));
 81         memset(findkeyOfDoor, 0, sizeof(findkeyOfDoor));
 82         memset(q, 0, sizeof(q));
 83         /*
 84         for(int i = 0; i <= m + 1; ++i)
 85             map[i][0] = map[i][n + 1] = 'X';
 86         for(int i = 0; i <= n + 1; ++i)
 87             map[0][i] = map[m + 1][i] = 'X';  //添加两个fou循环可以防止越界
 88         */
 89         memset(map, 'X', sizeof(map));  //这样写也可以提交
 90         
 91         for(int i = 1; i <= m; ++i)
 92         {
 93             for(int j = 1; j <= n; ++j)
 94             {
 95                 cin >> map[i][j];
 96                 if(map[i][j] == 'S')
 97                 {
 98                     tx = i;
 99                     ty = j;
100                 }
101                 else if(map[i][j] >= 'a' && map[i][j] <= 'e')
102                     ++maxkeyOfDoor[map[i][j] - 'a'];
103             }
104         }
105         DFS(tx, ty);
106         if(flag)
107             cout << "YES" << endl;
108         else
109             cout << "NO" << endl;
110     }
111     return 0;
112 }

转载于:https://www.cnblogs.com/a1225234/p/4899635.html

相关文章:

理解Python的迭代器(转)

原文地址: http://python.jobbole.com/81916/ 另外一篇文章: http://www.cnblogs.com/kaituorensheng/p/3826911.html 什么是迭代 可以直接作用于for循环的对象统称为可迭代对象(Iterable)。 可以被next()函数调用并不断返回下一个值的对象称为迭代器(Iterator)。 所有的Iterab…

快捷导航动画制作

做了一个仿大众点评的快捷导航动画效果&#xff0c;点击导航内的箭头&#xff0c;导航缩放&#xff0c;点击快捷导航再伸展。 看效果图&#xff1a; 实现代码&#xff1a; <block wx:if"{{!isCustom}}"><view class"home_and_reSource" animati…

instant apps_Android Instant Apps 101:它们是什么以及它们如何工作

instant appsby Tomislav Smrečki通过TomislavSmrečki Android Instant Apps are a cool new way to consume native apps without prior installation. Only parts of the app are downloaded and launched, giving the users a native look and feel in a couple of secon…

数据库分享一: MySQL的Innodb缓存相关优化

无论是对于哪一种数据库来说&#xff0c;缓存技术都是提高数据库性能的关键技术&#xff0c;物理磁盘的访问速度永 远都会与内存的访问速度永远都不是一个数量级的。通过缓存技术无论是在读还是写方面都可以大大提 高数据库整体性能。Innodb_buffer_pool_size 的合理设置Innodb…

用过美德乐吸奶器的宝妈们感觉比国产吸奶器怎么样啊?

药效好不好&#xff0c;看疗效就知道。吸奶器好不好看评价就知道。我们来看看美德乐吸奶器 天猫旗舰店 : http://medela.wang 的宝妈们的评价如可 拔奶神器&#xff0c;绝对好过贝亲&#xff01;最初一次七八十&#xff0c;后来一百多&#xff0c;现在可以翻个倍。结合宝宝吮吸…

小程序地图多个 circles 使用demo

效果图&#xff1a; 代码&#xff1a; var that; const app getApp() const util require("../../utils/util.js") const data require("../../utils/map.js") Page({data: {pageShow: false,scale: 15,obj: {},longitude: 116.34665554470486,latitud…

编写文档_如何通过编写优质文档来使自己的未来快乐

编写文档by Gabriele Cimato加布里埃莱西马托(Gabriele Cimato) 如何通过编写优质文档来使自己的未来快乐 (How to make your future self happy by writing good docs) 或者&#xff0c;在清除旧代码库时如何减少痛苦 (Or how to be less miserable when dusting off an old …

(转载)人人都会OSGI--实例讲解OSGI开发

http://longdick.iteye.com/blog/457310转载于:https://www.cnblogs.com/eecs2016/articles/7422310.html

小程序json字符串转 json对象 { name :你好} 转成 { name :你好}

解决后端接口返回 var obj "{ name :"你好"}" 类似这样的数据&#xff0c;对象或者数组外面包了一层引号&#xff0c; 把这种数据转成 var obj { name :"你好"}&#xff1b; 直接上代码&#xff1a; // pages/test/test.js Page({jsonStrToJ…

每天写的叫工作日志,每周写的总结叫周报,每月写的叫月报

有些时候&#xff0c;老板会突发让您求每天都要写工作周报&#xff0c;什么项目什么任务&#xff0c;完成情况&#xff0c;完成花费的时间等&#xff0c;然后汇总部门周报&#xff1b;也不是写不出&#xff0c;只是不知道有时候重复做一个项目&#xff0c;到底每天有什么好写&a…

为什么分散刷新没有死时间_分散项目为何失败(以及如何处理)

为什么分散刷新没有死时间The most exciting thing about working in the decentralized economy is the fact that no one has any idea as to where it’ll all end up!在去中心decentralized economy工作最令人振奋的事情是&#xff0c;没有人知道最终的结果&#xff01; T…

.NET Core 常用加密和Hash工具NETCore.Encrypt

前言 在日常开发过程中&#xff0c;不可避免的涉及到数据加密解密&#xff08;Hash&#xff09;操作&#xff0c;所以就有想法开发通用工具&#xff0c;NETCore.Encrypt就诞生了。目前NETCore.Encrypt只支持.NET Core ,工具包含了AES,DES,RSA加密解密&#xff0c;MD5&#xff0…

url 通配符解析成参数

需求&#xff1a;url 参数是通配符&#xff0c;需要把通配符解析成参数并且拼接到 url 中 例如&#xff1a;https://xxx.cn/index.html$a1$b2; 解析成 https://xxx.cn/index.html?a1&b2; 时间关系&#xff0c;直接上代码&#xff0c;有时间再补上注释 下面是小程序页…

性能测试分享:系统架构

性能测试分享&#xff1a;系统架构 转载于:https://www.cnblogs.com/poptest/p/4904584.html

graphql是什么_为什么GraphQL是避免技术债务的关键

graphql是什么GraphQL (not to be confused with GraphDB or Open Graph or even an actual graph) is a remarkably creative solution to a relatively common problem: How do you enable front end developers to access backend data in exactly the way they need it?Gr…

JS如何判断json是否为空

1、判断json是否为空 jQuery.isEmptyObject()&#xff1b; 2、遍历json function getHsonLength(json{var jsonLength0;for (var i in json){jsonLength;}return jsonLength;}转载于:https://www.cnblogs.com/donaldworld/p/7423811.html

微软算法100题11 求二叉树中两节点之间的最大距离

第11 题求二叉树中节点的最大距离...如果我们把二叉树看成一个图&#xff0c;父子节点之间的连线看成是双向的&#xff0c;我们姑且定义"距离"为两节点之间边的个数。写一个程序&#xff0c;求一棵二叉树中相距最远的两个节点之间的距离 思路: 一棵树中节点的最大距…

小程序订阅消息 订阅消息开发

微信小程序交流QQ群&#xff1a; 173683895 173683866 526474645 。 群内打广告或者脏话一律飞机票 订阅消息 当用户勾选了订阅面板中的“总是保持以上选择&#xff0c;不再询问”时&#xff0c;模板消息会被添加到用户的小程序设置页&#xff0c;通过 wx.getSetting…

meetup_如何使用标准库和Node.js构建Meetup Slack机器人

meetupby Janeth Ledezma简妮丝莱德兹玛(Janeth Ledezma) 如何使用标准库和Node.js构建Meetup Slack机器人 (How to build a Meetup Slack bot with Standard Library and Node.js) In this guide, you will learn how to set up a Slack application that will display infor…

.NET使用OpenSSL生成的pem密钥文件[1024位]

using System; using System.Text; using System.Security.Cryptography; using System.Web; using System.IO;namespace Thinhunan.Cnblogs.Com.RSAUtility {public class PemConverter{/// <summary>/// 将pem格式公钥转换为RSAParameters/// </summary>/// <…

[2014百度之星资格赛]

第一个问题&#xff1a; Energy Conversion Problem Description魔法师百小度也有遇到难题的时候——如今。百小度正在一个古老的石门面前&#xff0c;石门上有一段古老的魔法文字&#xff0c;读懂这样的魔法文字须要耗费大量的能量和大量的脑力。过了许久。百小度最终读懂魔法…

视频录制,压缩实现源码

实现代码&#xff1a; <!DOCTYPE html> <html><head><meta charset"utf-8"><title></title><meta name"viewport" content"widthdevice-width, initial-scale1.0"><!-- <script src"./js…

alexa技能个数_如何在您的技能中使用Alexa演示语言

alexa技能个数by Garrett Vargas通过Garrett Vargas 如何在您的技能中使用Alexa演示语言 (How to use Alexa Presentation Language in your skill) Amazon recently released the Alexa Presentation Language (APL). APL provides a richer display for multimodal skills. …

HTML与XML总结

阅览《孙欣HTML》和《刘炜XML》过了一段时间&#xff0c;在这里学到的内容用思维导图来概括。HTML与XML都是标记语言。 同样点&#xff1a; HTML文档与XML文档有类似的结构。前者是&#xff08;head和body&#xff09;后者是&#xff08;声明和主体&#xff09;&#xff0c;大致…

ant PageHeaderWrapper 返回上一页

PageHeaderWrapper 返回上一页实现代码&#xff1a; <PageHeaderWrappertitle{false}content{<a onClick{() > router.goBack()}><Icon type"left" />返回</a>}breadcrumb{{routes: [{ path: /, breadcrumbName: 首页 },{ path: /pay_orde…

ruby 新建对象_Ruby面向对象编程的简介

ruby 新建对象by Saul Costa由Saul Costa Object-oriented programming (OOP) is a programming paradigm organized around objects. At a high level, OOP is all about being able to structure code so that its functionality can be shared throughout the application.…

ASP.NET导出文件FileResult的使用

本文给大家讲一下ASP.NET MVC中如何使用FileResult来导出文件&#xff0c;首先网上相关例子有很多大神都有讲&#xff0c;我在这只是稍微说一点不同——为什么我的导出没有反应呢&#xff1f; 这个问题&#xff0c;我找了半天也没有找到&#xff0c;最后是在一个网友的评论中体…

【AHOI 2016初中组】 自行车比赛 - 贪心

题目描述 小雪非常关注自行车比赛&#xff0c;尤其是环滨湖自行车赛。一年一度的环滨湖自行车赛&#xff0c;需要选手们连续比赛数日&#xff0c;最终按照累计得分决出冠军。今年一共有 N 位参赛选手。每一天的比赛总会决出当日的排名&#xff0c;第一名的选手会获得 N 点得分&…

【Ant Design Pro 三】样式动态绑定 react样式绑定

第一步&#xff0c;创建样式文件&#xff0c;在页面目录下根据自己习惯创建一个less文件&#xff0c;用来写样式类 第二部&#xff0c;引用该文件 import styles from ./details.less; //details.less 代码&#xff1a; .menu {width: 95%; } .navigation-menu{width: 90%; …

react hooks使用_如何使用React和Hooks检测外部点击

react hooks使用by Andrei Cacio通过安德烈卡西奥(Andrei Cacio) 如何使用React和Hooks检测外部点击 (How to detect an outside click with React and Hooks) “外部点击”是什么意思&#xff1f; (What does “Outside Click” mean?) You can think of it as the “anti-b…