PAT Mooc datastructure 6-1
Saving James Bond - Hard Version
This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was captured by a group of drug dealers. He was sent to a small piece of land at the center of a lake filled with crocodiles. There he performed the most daring action to escape -- he jumped onto the head of the nearest crocodile! Before the animal realized what was happening, James jumped again onto the next big head... Finally he reached the bank before the last crocodile could bite him (actually the stunt man was caught by the big mouth and barely escaped with his extra thick boot).
Assume that the lake is a 100 by 100 square one. Assume that the center of the lake is at (0,0) and the northeast corner at (50,50). The central island is a disk centered at (0,0) with the diameter of 15. A number of crocodiles are in the lake at various positions. Given the coordinates of each crocodile and the distance that James could jump, you must tell him a shortest path to reach one of the banks. The length of a path is the number of jumps that James has to make.
Input Specification:
Each input file contains one test case. Each case starts with a line containing two positive integers N (<=100), the number of crocodiles, and D, the maximum distance that James could jump. Then N lines follow, each containing the (x, y) location of a crocodile. Note that no two crocodiles are staying at the same position.
Output Specification:
For each test case, if James can escape, output in one line the minimum number of jumps he must make. Then starting from the next line, output the position (x, y) of each crocodile on the path, each pair in one line, from the island to the bank. If it is impossible for James to escape that way, simply give him 0 as the number of jumps. If there are many shortest paths, just output the one with the minimum first jump, which is guaranteed to be unique.
Sample Input 1:
- 17 15
- 10 -21
- 10 21
- -40 10
- 30 -50
- 20 40
- 35 10
- 0 -10
- -25 22
- 40 -40
- -30 30
- -10 22
- 0 11
- 25 21
- 25 10
- 10 10
- 10 35
- -30 10
Sample Output 1:
- 4
- 0 11
- 10 21
- 10 35
1.题目分析
这个题目就是典型的最短路径问题~
使用BFS对图进行层级遍历,只要在任一层发现可以跳出的话,那么就逆序打印出来全体路径。
至于这个层次的问题,其实很好实现,因为在BFS入队的过程中,是将一个节点的链接节点依此入队。在入队的时候,只要将此节点的步点数加1写入下一层中即可完成层数的累积。
同时,为了完成最后的打印工作,需要使用一个数组,完成类似于链表的工作。每次压入新的节点时,就要将新节点对应的父节点数组值设置为其父节点的index,这样在推出时逆序压入堆栈,再重新打印出来,即可完成逆序打印工作。
有两个小点值得注意,一个是如果007非常牛逼,直接就可以跳出来的情况需要特殊处理一下。
二是按照题目要求,如果存在相同步数的路径,需要选出第一跳最短的那一条路径。为了实现这一功能,我在将鳄鱼节点录入时,先按照距离原点的距离升序排序。这样,在进行BFS的时候,总是先从短距离向长距离遍历,保证了第一跳最短距离的哪条路径最先被发现。
2.伪码实现
P = 007StartPoint;
if (Could Get Out at the StartPoint)
return FirstGetOut;
EnQueue(P,Queue)
while(IsNotEmpty(Queue))
{
P = DeQueue(Queue);
Find All the Point NextP that jump from P
{
if(NextP is not reached)
{
Jumped(NextP) = Jumped(P) + 1
father(NextP) = P
if(CouldGetOutFrom(NextP))
return NextP;
EnQueue(NextP, Queue)
}
}
}
return CouldNotJumpOut
3. 通过代码:
- #define NONE -1
- #define DISK 100000
- #define MISTAKE -11
- #define FIOUT 5000000
- #include <stdio.h>
- #include <stdlib.h>
- static int jumped[][];
- void InitialJumped()
- {
- int i;
- int j;
- for(i=;i<=;i++)
- {
- for(j=;j<=;j++)
- {
- jumped[i][j]=NONE;
- }
- }
- }
- int IsJumped(int x,int y)
- {
- if(jumped[x+][y+] != NONE)
- {
- return ;
- }
- else
- {
- return NONE;
- }
- }
- void JumpOn(int x,int y,int jumpNum)
- {
- jumped[x+][y+]= jumpNum;
- }
- int GetJump(int x,int y)
- {
- return jumped[x+][y+];
- }
- /////////End of Visited /////////////
- //////////Begin of Croc//////////////
- typedef struct Croc{
- //location of this Croc
- int x;
- int y;
- int dis;
- }tCroc;
- void SetDis(tCroc* C)
- {
- C->dis = (C->x)*(C->x) + (C->y)*(C->y);
- }
- int GetDis(tCroc* C)
- {
- return C->dis;
- }
- //If bond could reach from ori to des.
- int Reachable(tCroc* ori,tCroc* des,int step)
- {
- int oriX;
- int oriY;
- int desX;
- int desY;
- int distanceSquare;
- int stepSquare;
- oriX = ori->x;
- oriY = ori->y;
- desX = des->x;
- desY = des->y;
- distanceSquare = (oriX-desX)*(oriX-desX)+(oriY-desY)*(oriY-desY);
- stepSquare = step*step;
- if(stepSquare >= distanceSquare)
- {
- return ;
- }
- else
- {
- return ;
- }
- }
- int GetOut(tCroc *ori,int step)
- {
- if(ori->x + step >= )
- {
- return ;
- }
- if(ori->x - step <= -)
- {
- return ;
- }
- if(ori->y + step >= )
- {
- return ;
- }
- if(ori->y - step <= -)
- {
- return ;
- }
- return ;
- }
- //////////End of Croc////////////////
- /////////DFS of Croc/////////////////
- int DFSofCroc(tCroc *ori,tCroc list[],int numOfCroc,int step)
- {
- int i;
- int localStep;
- JumpOn(ori->x,ori->y,);
- if((ori->x == )&& (ori->y == ))
- {
- localStep = step+;
- }
- else
- {
- localStep = step;
- }
- if(GetOut(ori,localStep)==)
- {
- return ;
- }
- for(i = ;i<numOfCroc;i++)
- {
- if(Reachable(ori,&list[i],localStep)==)
- {
- if(IsJumped(list[i].x,list[i].y)==NONE)
- {
- int result;
- result = DFSofCroc(&list[i],list,numOfCroc,step);
- if(result == )
- {
- return ;
- }
- }
- }
- }
- return ;
- }
- //////////////////End of DFS Croc without COUNT/////////////////
- /////////////////Begin of queue//////////////
- typedef struct queueNode{
- tCroc * thisCroc;
- struct queueNode * nextCroc;
- }QNode;
- typedef struct CrocQueue{
- QNode *head;
- QNode *tail;
- }tCrocQueue;
- tCrocQueue* InitialQueue()
- {
- tCrocQueue* temp = malloc(sizeof(tCrocQueue));
- temp->head = NULL;
- temp->tail = NULL;
- return temp;
- }
- void EnQueue(QNode *node,tCrocQueue *Q)
- {
- if(Q->head == NULL)
- {
- Q->head = node;
- Q->tail = node;
- return ;
- }
- else
- {
- Q->tail->nextCroc = node;
- Q->tail = node;
- return;
- }
- }
- QNode * DeQueue(tCrocQueue *Q)
- {
- if(Q->head == NULL)
- {
- return NULL;
- }
- else
- {
- QNode *temp = Q->head;
- if(Q->head == Q->tail)
- {
- Q->head = NULL;
- Q->tail = NULL;
- }
- else
- {
- Q->head = Q->head->nextCroc;
- }
- return temp;
- }
- }
- int IsQueueEmpty(tCrocQueue *Q)
- {
- if(Q->head == NULL)
- {
- return ;
- }
- else
- {
- return ;
- }
- }
- ////////////////End of queue /////////////////
- ////////////////Begin of BFS/////////////////
- int BFS(tCroc *ori, tCroc list[], int Sum, int step, tCrocQueue *Q, int father[])
- {
- int count;
- int myfather;
- myfather = DISK;
- count = ;
- JumpOn(ori->x,ori->y,count);
- if(GetOut(ori,step+)==)
- {
- return FIOUT;
- }
- QNode * thisNode = malloc(sizeof(QNode));
- thisNode->thisCroc = ori;
- EnQueue(thisNode,Q);
- while(IsQueueEmpty(Q)==)
- {
- int i ;
- int localStep;
- QNode* temp = DeQueue(Q);
- count = GetJump(temp->thisCroc->x,temp->thisCroc->y);
- if((temp->thisCroc->x == )&&(temp->thisCroc->y==))
- {
- localStep = step + ;
- myfather = DISK;
- }
- else
- {
- localStep = step;
- for(i = ; i < Sum ; i++)
- {
- if((temp->thisCroc->x == list[i].x)&&(temp->thisCroc->y== list[i].y))
- {
- myfather = i;
- break;
- }
- }
- }
- for(i = ; i < Sum ;i++)
- {
- if(Reachable(temp->thisCroc,&list[i],localStep)==)
- {
- if(IsJumped(list[i].x,list[i].y)==NONE)
- {
- JumpOn(list[i].x,list[i].y,(count+));
- father[i] = myfather;
- if(GetOut(&list[i],step)==)
- {
- return i;
- }
- thisNode = malloc(sizeof(QNode));
- thisNode->thisCroc = &list[i];
- EnQueue(thisNode,Q);
- }
- }
- }
- }
- return NONE;
- }
- ////////////////End of BFS///////////////////
- ////////////////Begin Stack//////////////////
- typedef struct stackNode{
- tCroc *thisNode;
- struct stackNode * next;
- }tStackNode;
- typedef struct CrocStack{
- tStackNode* Top;
- tStackNode* Bottom;
- }tCrocStack;
- tCrocStack* InitialStack()
- {
- tCrocStack * temp = malloc(sizeof(tCrocStack));
- temp->Top = NULL;
- temp->Bottom = NULL;
- return temp;
- }
- void Push(tStackNode *node,tCrocStack * S)
- {
- if(S->Top == NULL)
- {
- S->Top = node;
- S->Bottom = node;
- }
- else
- {
- node->next = S->Top;
- S->Top = node;
- }
- }
- tStackNode *Pop(tCrocStack *S)
- {
- if(S->Top == NULL)
- {
- return NULL;
- }
- else
- {
- tStackNode * temp = S->Top;
- if(S->Top == S->Bottom)
- {
- S->Bottom = NULL;
- S->Top = NULL;
- }
- else
- {
- S->Top = S->Top->next;
- }
- return temp;
- }
- }
- int IsStackEmpty(tCrocStack *S)
- {
- if(S->Top == NULL)
- {
- return ;
- }
- else
- {
- return ;
- }
- }
- /////////////////End of Stack////////////////
- int main()
- {
- int numOfCrocs;
- int step;
- int i;
- InitialJumped();
- scanf("%d %d",&numOfCrocs,&step);
- tCroc crocs[numOfCrocs];
- int preCro[numOfCrocs];
- //Put all the cordinates X,Y into array
- for(i=;i<numOfCrocs;i++)
- {
- int j;
- int tempX;
- int tempY;
- scanf("%d %d",&tempX,&tempY);
- if( tempX > || tempX< - || tempY > || tempY < - )
- {
- i--;
- numOfCrocs--;
- continue;
- }
- crocs[i].x = tempX;
- crocs[i].y = tempY;
- SetDis(&crocs[i]);
- for(j=i-;j>=;j--)
- {
- if(crocs[j].x == crocs[i].x && crocs[j].y == crocs[i].y)
- {
- i--;
- numOfCrocs--;
- j=MISTAKE;
- break;
- }
- }
- if(j==MISTAKE)
- {
- continue;
- }
- for(j=i;j>;j--)
- {
- if(GetDis(&crocs[j])<GetDis(&crocs[j-]))
- {
- tCroc temp = crocs[j-];
- crocs[j-]=crocs[j];
- crocs[j]=temp;
- }
- else
- {
- break;
- }
- }
- preCro[i] = NONE;
- }
- // printf("---------------\n");
- // for(i=0;i<numOfCrocs;i++)
- // {
- // printf("%d %d %d\n",crocs[i].x,crocs[i].y,crocs[i].dis);
- // }
- // printf("---------------\n");
- tCroc *Zero = malloc(sizeof(tCroc));
- Zero->x = ;
- Zero->y = ;
- tCrocQueue * MyQueue = InitialQueue();
- int result;
- result = BFS(Zero,crocs,numOfCrocs,step,MyQueue,preCro);
- if(result == NONE)
- {
- printf("");
- return ;
- }
- else if(result == FIOUT)
- {
- printf("");
- }
- else
- {
- int Dis = GetJump(crocs[result].x,crocs[result].y);
- printf("%d\n",Dis+);
- }
- tCrocStack * MyStack = InitialStack();
- while(result != DISK)
- {
- tStackNode *temp = malloc(sizeof(tStackNode));
- temp->thisNode = &crocs[result];
- Push(temp,MyStack);
- result = preCro[result];
- }
- while(IsStackEmpty(MyStack)==)
- {
- int printX;
- int printY;
- tStackNode *temp = Pop(MyStack);
- printX = temp->thisNode->x;
- printY = temp->thisNode->y;
- printf("%d %d\n",printX,printY);
- }
- // result = DFSofCroc(Zero,crocs,numOfCrocs,step);
- // if(result == 1)
- // {
- // printf("Yes");
- // }
- // else
- // {
- // printf("No");
- // }
- return ;
- }
PAT Mooc datastructure 6-1的更多相关文章
- PAT mooc DataStructure 4-2 SetCollection
数据结构习题集-4-2 集合的运用 1.题目: We have a network of computers and a list of bi-directional connections. Eac ...
- PAT MOOC dataStructure 4-1
数据结构练习 4-1 AVL 树 1. 题目: Input Specification: Each input file contains one test case. For each case, ...
- PAT B1080 MOOC期终成绩(C++)
PAT甲级目录 | PAT乙级目录 题目描述 B1080 MOOC期终成绩 解题思路 可利用 map 将字符串型的学号转换为整型的序号,方便查找.输入全部成绩后,遍历每个学生同时计算最终成绩,然后将成 ...
- PAT 乙级 1080 MOOC期终成绩 (25 分)
1080 MOOC期终成绩 (25 分) 对于在中国大学MOOC(http://www.icourse163.org/ )学习“数据结构”课程的学生,想要获得一张合格证书,必须首先获得不少于200分的 ...
- PAT 1080 MOOC期终成绩(25)(STL-map及multiset+思路+测试点分析)
1080 MOOC期终成绩(25 分) 对于在中国大学MOOC(http://www.icourse163.org/ )学习"数据结构"课程的学生,想要获得一张合格证书,必须首先获 ...
- PAT 1080 MOOC期终成绩
https://pintia.cn/problem-sets/994805260223102976/problems/994805261493977088 对于在中国大学MOOC(http://www ...
- PAT Basic 1080 MOOC期终成绩 (25 分)
对于在中国大学MOOC(http://www.icourse163.org/ )学习“数据结构”课程的学生,想要获得一张合格证书,必须首先获得不少于200分的在线编程作业分,然后总评获得不少于60分( ...
- 【PAT】B1080 MOOC期终成绩(25 分)
还是c++好用,三部分输入直接用相同的方法, 用map映射保存学生在结构体数组中的下标. 结构体保存学生信息,其中期末成绩直接初始化为-1, 注意四舍五入 此题还算简单 #include<ios ...
- PAT乙级考前总结(三)
特殊题型 1027 打印沙漏 (20 分) 题略,感觉有点像大学里考试的题.找规律即可. #include <stdio.h>#include <iostream>using ...
随机推荐
- sudo 命令情景分析
Linux 下使用 sudo 命令,可以让普通用户也能执行一些或者全部的 root 命令.本文就对我们常用到 sudo 操作情景进行简单分析,通过一些例子来了解 sudo 命令相关的技巧. 情景一:用 ...
- Java反射特性--获取其他类实例并调用其方法
1. 代码结构 .├── com│ └── test│ └── MyTest.java└── MainCall.java 2. 代码内容 MyTest.java: package com.te ...
- NYOJ 762
容斥原理 http://blog.csdn.net/shiren_bod/article/details/5787722
- nodejs中使用RabbitMq消息中心系统的方式
方式一:通过npm安装amqp库 方式二:通过rabbit.js库http://www.squaremobius.net/rabbit.js/ AMQP:高级消息队列协议,是应用层协议的一个开放标准, ...
- 1089 最长回文子串 V2(Manacher算法)
1089 最长回文子串 V2(Manacher算法) 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 回文串是指aba.abba.cccbccc.aaaa ...
- web前端
设置透明: filter: alpha(opacity=70); opacity: 0.7; 设置文字与文字之间的间隙 letter-spacing: 5px; 设置单词与单词之间的间隙 word-s ...
- 【原】KMeans与深度学习模型结合提高聚类效果
这几天在做用户画像,特征是用户的消费商品的消费金额,原始数据(部分)是这样的: id goods_name goods_amount 男士手袋 1882.0 淑女装 2491.0 女士手袋 345.0 ...
- 尝试使用Memcached遇到的狗血问题
乘着有时间,尝试下利用Memcached进行分布式缓存,其中遇到了不少问题及狗血的事情,开篇记录下,希望对您有帮助. 我之前的项目为:Asp.Net MVC4 + Nhibernate + MSSQL ...
- 在VS中用正则表达式查找或替换
2005VS和2008VS正则替换规则如下(2013VS不适合,不需要的同学可以直接跳到2013VS的操作办法): Example: 查找#incldue中带有gl开头的头文件的,用include.+ ...
- 使用antd UI组件有感
公司使用的的react.js的版本提14.7的,JS版本使用的是ES6语法,因此在使用antd过程中,有些许不愉快的记录,分享给大家,一起学习: 如果是react 14.7版本时,使用getField ...