PAT-2022年春季考试 - 甲级题解

试卷在此

7-1 Simple Lie Detection (20 分)

作者			陈越
单位			浙江大学
代码长度限制		16 KB
时间限制			400 ms
内存限制			64 MB

PAT-2022年春季考试 - 甲级题解_第1张图片
Lie detection usually uses a set of prepared questions to ask the testee, and the result is obtained by analyzing the testee’s reactions. The more advanced technology uses a polygraph (测谎仪) to monitor the physical activities of the testee. Our simple polygraph here is to make a judgment by analyzing the characteristics of the answers to the questions.

First, we ask the testee to complete N N N multiple choice questions, each with a single answer. Each question has 8 options, which are represented by the lowercase English letters a-h. Then we can obtain a string of length N N N, consisting of only the lowercase English letters a-h. Score each string, and those whose score exceeds a certain threshold (阈值) T T T are judged as “suspected liars”. The scoring rules are as follows:

  • the string starts with an f has its score − 2 -2 2;

  • the string ends with an a has its score − 1 -1 1;

  • for every longest segment of answeres where the same letter is chosen for consecutive questions, if the segment length is larger than 5, the score is to + 3 +3 +3;

  • if an a is immediately followed by e or h, the score is to − 4 -4 4;

  • for every longest segment of answeres where consecutively increasing letters are chosen to answer consecutive questions (such as abcd or defgh), if the segment length is larger than 3, the score is to + 5 +5 +5.

Your job is to score the answers and make a judgement.

Input Specification:

Each input file contains one test case. For each case, the first line gives 3 positive integers N N N ( 6 ≤ N ≤ 100 6\le N \le 100 6N100), the number of questions; T T T ( ≤ 100 \le 100 100), the threshold for judging a liar; and K K K ( ≤ 100 \le 100 100), the number of testees.

Then K K K lines follow, each gives the string of answers of a testee.

Output Specification:

For each testee, print in a line the total score of his/her answers. If the score exceeds the threshold, print !!! after the score.

Sample Input:

12 1 6
fghaebcdeddd
ahhhhhhgbaaa
cdefffffffff
fffffghecaaa
feeeeeeeegcb
aaaaaabbbbbb

Sample Output:

-1
-2
8!!!
-3
1
6!!!

给出k组询问,每组询问为一个长度为N的字符串:

①字符串以f开头,则分数-2;

②字符串以a结尾,则分数-1;

③如果连续相同字母的每个最长的片段长度大于5,则分数+3;

④如果a后紧跟eh,则分数-4;

⑤如果连续递增的每个最长的片段长度大于3,则分数+5,例如abcddefgh

每组询问计算对应的分数,如果分数超过阈值K则在分数后输出!!!

#include 
using namespace std;
int main()
{
	int l,t,n;
	cin>>l>>t>>n;
	while(n--)
	{
		int r=0,length1=1,length2=1;
		string s;
		cin>>s;
        //字符串以`f`开头,则分数-2;
		if(s[0]=='f')r-=2;
        //字符串以`a`结尾,则分数-1;
		if(s[l-1]=='a')r-=1;
		for(int i=1;i<l;i++)
		{
			if(s[i]==s[i-1])
			{	//如果当前字母和上一个字母相同,则length1加1
				length1++;
			}
			else
			{	//如果连续相同字母的每个最长的片段长度大于5,则分数+3;
				if(length1>5)r+=3;
				//初始化length1为1
				length1=1;
			}
			
			if(s[i]==s[i-1]+1)
			{	//如果当前字母比上一个字母大1,则length2加1
				length2++;
			}
			else
			{	//如果连续递增的每个最长的片段长度大于3,则分数+5,例如`abcd`和`defgh`。
				if(length2>3)r+=5;
				//初始化length2为1
				length2=1;
			}
			//如果`a`后紧跟`e`或`h`,则分数-4;
			if(s[i-1]=='a'&&(s[i]=='e'||s[i]=='h'))r-=4;
		}
		//如果连续相同字母的每个最长的片段长度大于5,则分数+3;
		if(length1>5)r+=3;
		//如果连续递增的每个最长的片段长度大于3,则分数+5,例如`abcd`和`defgh`。
		if(length2>3)r+=5;
		//每组询问计算对应的分数
		cout<<r;
		//如果分数超过阈值K则在分数后输出`!!!`
		if(r>t)cout<<"!!!";
		cout<<endl;
	}
	return 0;
} 

放上紫薇学姐的题解

/*
    n道题,阈值为t(分数超过t要输出!!!) ,k个作答
    判题规则: 
    1.字符串首字母为f,分数-2
    2.字符串尾字母为a,分数-1
    3.每个相同相同字母的len>5的最长答案段,分数+3
        即aaaaaaabbbbbbccccccc,分数为9
    4.字母a后为e或者h,分数-4
    5.连续递增字母且len>3,分数+5 
*/

#include
using namespace std;
int main()
{
    int n,t,k;
    cin>>n>>t>>k;
    while(k--)
    {
        int res=0;
        string s;
        cin>>s;
        if(s[0]=='f')res-=2;
        if(s[n-1]=='a')res-=1;
        
        //for every longest segment of answeres where the same letter is chosen for consecutive questions, 
        //if the segment length is larger than 5, the score is to +3;
        //记录每个字母的最长连续段 
        int longest_len[200]={0};
        for(int i=0;i<n;)
        {
            int tmp_len=0;
            if(i+5<n&&s[i]==s[i+1]&&s[i]==s[i+2]&&s[i]==s[i+3]&&s[i]==s[i+4]&&s[i]==s[i+5])
            {
                i+=5;
                tmp_len+=5;
                while(s[i]==s[i+1]&&i+1<n)i++,tmp_len++;
            }
            else i++;
            if(tmp_len>longest_len[s[i-1]])longest_len[s[i-1]]=tmp_len;
        }
        //验证字母的连续段是否与longest_len相符,是则+5 
        for(int i=0;i<n;)
        {
            int tmp_len=0;
            if(i+5<n&&s[i]==s[i+1]&&s[i]==s[i+2]&&s[i]==s[i+3]&&s[i]==s[i+4]&&s[i]==s[i+5])
            {
                i+=5;
                tmp_len+=5;
                while(s[i]==s[i+1]&&i+1<n)i++,tmp_len++;
            }
            else i++;
            if(tmp_len==longest_len[s[i-1]]&&tmp_len)res+=3;
        }
        //注意every,测试点1和4会卡 
        for(int i=0;i+1<n;)
        {
            if(s[i]=='a'&&s[i+1]=='e'||s[i]=='a'&&s[i+1]=='h')
            {
                res-=4;
                i+=2;
            }
            else i++;
        }
        
        for(int i=0;i<n;)
        {
            if(i+3<n&&s[i+1]==s[i]+1&&s[i+2]==s[i]+2&&s[i+3]==s[i]+3)
            {
                res+=5;
                i+=3;
                while(s[i+1]==s[i]+1&&i+1<n)i++;
            }
            else i++;
        }
        
        if(res>t) cout<<res<<"!!!"<<endl;
        else cout<<res<<endl;
    }
    return 0;
}

7-2 The First K Largest Numbers (25 分)

作者			陈越
单位			浙江大学
代码长度限制		16 KB

Java (javac)
时间限制			2000 ms
内存限制			300 MB

Python (python2)
时间限制			2200 ms
内存限制			5 MB

Python (python3)
时间限制			2200 ms
内存限制			5 MB

其他编译器
时间限制			150 ms
内存限制			2 MB

The task is simple: find the first K K K largest numbers from a given sequence of N N N integers, and output them in descending order.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N N N ( ≤ 1 0 6 \le 10^6 106) and K K K ( ≤ 5 \le 5 5). Then N N N integer keys (in the range [ − 2 30 , 2 30 ] [-2^{30}, 2^{30}] [230,230]) are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in descending order the K K Kth largest numbers.

All the numbers in a line must be separated by a space, and there must be no extra space at the beginning or the end of the line.

Sample Input 1:

10 4
40 25 60 -15 30 -21 80 -1 -5 27

Sample Output 1:

80 60 40 30

Sample Input 2:

4 5
23 -17 99 1

Sample Output 2:

99 23 1 -17

给出N个数字,找出最大的K个数。

因为内存限制只有2M=2048K字节,而10e6个数需要4000000字节,大约4000k字节。

因为时间限制只有150ms,所以10e6个数直接使用cin会超时。

因为大顶堆不能保证有序,所以不能使用大顶堆维护最大的K个数。

①开一个长度为K的数组,维护最大的K个数。

#include 
using namespace std;
int a[6];
int main()
{
    int n,k;
    cin>>n>>k;
    int mi=min(n,k);
    for(int i=0;i<mi;i++)scanf("%d",&a[i]);
    sort(a,a+mi,greater<int>());
    for(int i=mi;i<n;i++)
    {	//每次插入到第K+1个位置,再排序,保证前K个最大的数
        scanf("%d",&a[mi]);
        sort(a,a+mi+1,greater<int>());
    }
    for(int i=0;i<mi;i++)
    {
        if(i==0)cout<<a[i];
        else cout<<" "<<a[i];
    }
    return 0;
}

②使用优先队列priority_queue,维护最大的K个数。

#include 
using namespace std;
int a[5],p=0;
int main()
{
	int n,k;
	cin>>n>>k;
	priority_queue<int,vector<int>,greater<int>>q;
	for(int i=0;i<n;i++)
	{
		int x;
		scanf("%d",&x);
		q.push(x);
		if(q.size()>5)q.pop();
	}
	while(!q.empty())
	{
		a[p++]=q.top();q.pop();
	}
	for(int i=p-1,j=0;j<min(n,k);i--,j++)
	{
		if(j==0)cout<<a[i];
		else cout<<" "<<a[i];
	}
	return 0;
} 

③也可使用multiset和map,但是有一定概率会TLE。

#include 
using namespace std;
int n,k,mi,i,j,x;
multiset<int>m;
int main()
{
	scanf("%d%d",&n,&k);
	mi=min(n,k);
	for(i=0;i<n;i++)
	{
		scanf("%d",&x);
		m.insert(x);
		if(m.size()>mi)m.erase(m.begin());
	}
	for(auto i=m.rbegin();j<mi;j++,i++)
	{
		if(j==0)printf("%d",*i);
		else printf(" %d",*i);
	}
	return 0;
} 
#include 
using namespace std;
int n,k,mi,i,j,kk,x,f=0;
map<int,int>m;
int main()
{
	scanf("%d%d",&n,&k);
	mi=min(n,k);
	for(i=0;i<n;i++)
	{
		scanf("%d",&x);
		m[x]++;
		if(m.size()>mi)m.erase(m.begin());
	}
	for(map<int,int>::reverse_iterator i=m.rbegin();j<mi;j++,i++)
	{
		for(kk=0;kk<i->second;kk++)
		{
			if(f!=0)printf(" ");
			printf("%d",i->first);
			f++;
			if(f==mi)return 0;
		}
	}
} 

放上紫薇学姐的题解

#include
using namespace std;
//存1e6会内存超限 
int a[100];
bool cmp(int n1,int n2)
{
    return n1>n2;
}
int main()
{
    int n,k;
    //cin会超时 
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
    {
        //只存前k+1个数并进行排序,很经典的面试题~ 
        //优先队列、multiSet、map等都可以过这题,try a try 
        int tmp=min(i,k+1);
        scanf("%d",&a[tmp]);
        sort(a+1,a+tmp+1,cmp);
    }
    for(int i=1;i<=k&&i<=n;i++)
    {
        if(i==1)printf("%d",a[i]);
        else printf(" %d",a[i]);
    }
    printf("\n");
    
    return 0;
}

7-3 Is It A Binary Search Tree - Again (25 分)

作者			陈越
单位			浙江大学
代码长度限制		16 KB
时间限制			400 ms
内存限制			64 MB

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node’s key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node’s key.
  • Both the left and right subtrees must also be binary search trees.

A binary tree with positive keys can be represented by an array, which stores the keys in level-order as in a complete tree, with all the NULL nodes represented by − 1 -1 1. For example, the following tree is stored in an array as { 40, 25, 60, -1, 30, -1, 80, -1, -1, 27 }.

PAT-2022年春季考试 - 甲级题解_第2张图片

Now given a sequence of integers in an array, you are supposed to tell if it corresponds to a BST, and output its inorder traversal sequence.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N N N ( ≤ 1000 \le 1000 1000). Then N N N integer keys (in the range (0, 100000)) are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in a line YES if the sequence does correspond to a BST, or NO if not. Then print in the next line the inorder traversal sequence of that tree. It is guaranteed that the tree is non-empty.

All the numbers in a line must be separated by a space, and there must be no extra space at the beginning or the end of the line.

Sample Input 1:

10
40 25 60 -1 30 -1 80 -1 -1 27

Sample Output 1:

YES
25 27 30 40 60 80

Sample Input 2:

11
40 50 60 -1 30 -1 -1 -1 -1 -1 35

Sample Output 2:

NO
50 30 35 40 60

给出一颗二叉树的层序遍历,-1表示NULL,判断是否为二叉搜索树,并输出中序遍历。

二叉搜索树:具有以下性质的二叉树,对于每个结点

①结点的左子树仅包含小于该点权值的结点;

②结点的左子树仅包含大于或等于该点权值的结点;

③左右子树也为二叉搜索树。

二叉搜索树的中序遍历有序。

思路:根据层序遍历,dfs记录中序遍历的序列,判断是否有序。

#include 
using namespace std;
int n,a[1010],in[1010],p=0,f=1;
void inOrder(int x)
{
	if(x*2+1<n&&a[x*2+1]!=-1)inOrder(x*2+1);
	in[p++]=a[x];
	if(x*2+2<n&&a[x*2+2]!=-1)inOrder(x*2+2);
}
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)cin>>a[i];
	inOrder(0);
	for(int i=1;i<p;i++)
	{	//如果中序遍历无序则不是二叉搜索树
		if(in[i]<in[i-1])f=0;
	}
	cout<<(f==0?"NO":"YES")<<endl;
	for(int i=0;i<p;i++)
	{
		if(i==0)cout<<in[i];
		else cout<<" "<<in[i];
	}
	return 0;
} 

放上紫薇学姐的题解

#include
using namespace std;
int n,t[2050];
int a[2050],len=1;
//二叉搜索树的中序变历为升序 
void inOrder(int cur)
{
    if(t[cur]==-1||cur>n)return;
    inOrder(cur*2);
    a[len++]=t[cur];
    inOrder(cur*2+1);
}

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)cin>>t[i];
    inOrder(1);
    
    int b[2050]={0};
    for(int i=1;i<len;i++)b[i]=a[i];
    sort(b+1,b+len);
    
    //用b(排序后的a)判断a是否为升序 
    bool flag=0;
    for(int i=1;i<len;i++)
    {
        if(b[i]!=a[i])flag=1;
    }
    if(!flag)cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    
    for(int i=1;i<len;i++)
    {
        if(i==1)cout<<a[i];
        else cout<<" "<<a[i];
    }
    cout<<endl;
    return 0;
}

7-4 The Rescue (30 分)

作者			陈越
单位			浙江大学
代码长度限制		16 KB
时间限制			400 ms
内存限制			64 MB

PAT-2022年春季考试 - 甲级题解_第3张图片

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Roxx9ebk-1648102066231)(~/cc812835-9801-422b-9a4a-1c6848d26a77.png)]

When a group of hikers get lost in a forest and call the rescue team, the team leader would send the hikers a sequence of instructions. By following these instructions, the hikers can always move to a spot that the rescue team can reach, no matter where their original location was. Your job is to generate such a sequence for each given map.

A map is a rectangular(矩形) area consists of n × m n\times m n×m unit square areas. There is alway one square X that is marked to be the rescue spot. Some of the areas are blocked and marked to be #, meaning that the hikers cannot enter this block. We assume that the boundaries of the map are all blocked.

An instruction is an integer that represents a direction: 0 for North, 1 for East, 2 for South, and 3 for West. For example, the sequence of instructions 3001 tells the hikers to move toward West and enter the neighboring area first, then head toward North and cross two units, and finally move toward East into the neighboring area. If the destination area is blocked, the hikers must stay in the current area and wait for the next instruction. The sequence you generate must be able to direct the hikers to move into X no matter where they were in that map. Once they arrive at X, they will ignore the rest of the instructions.

There are many different ways of generating the sequence. Here we design a greedy algorithm, hoping to generate a not-too-long sequence. The algorithm works as the following:

  • Step 1: find the shortest paths from X to every other areas which the hikers might be in, and pick the furthest area A.
  • Step 2: generate sequence s s s for directing from A to X along the shortest path.
  • Step 3: update the map according to s s s – that is, mark all the possible areas that the hikers might be in after following s s s.
  • Step 4: if X is the only possible area that contains the hikers, stop; else goto Step 1.

The final sequence can be obtained by concatenating all the s s s’ generated in order.

Notice that while the shortest path might not be unique, you are supposed to output the smallest sequence. Sequence a 1 , a 2 , ⋯   , a n { a_1, a_2, \cdots , a_n } a1,a2,,an is smaller than sequence b 1 , b 2 , ⋯   , b n { b_1, b_2, \cdots , b_n } b1,b2,,bn if ther exists 1 ≤ k ≤ n 1\le k \le n 1kn so that a i = b i a_i = b_i ai=bi for all i < k ii<k, and a k < b k a_k < b_k ak<bk.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive numbers 3 ≤ n , m ≤ 100 3\le n, m \le 100 3n,m100. Then n n n lines follow, each contains m m m characters to represent the map. The rescue spot is X, a block is #, and O represents an open area.

Output Specification:

Output in a line the sequence of instructions. There must be no space between the numbers, nor at the beginning or the end of the line.

It is guaranteed that the solution exists.

Sample Input:

4 5
O#OOO
OOXOO
OO##O
OOO#O

Sample Output:

0011300121133

Hint:

During the 1st round, there are 3 candidates for A, as shown below:

O#OOO
OOXOO
OO##O
AOA#A

To direct them to X with the smallest sequence, we must choose the one at the lower-left corner, with the sequence 0011. Since at the very beginning, the hikers might be in any of the open areas, we apply 0011 to every open area, and obtain a map as shown below:

?#OO?
OOXO?
OO##O
OO?#O

where ? means that currently it is a possible position of the hikers. Now clearly we must pick the ? in the last row to be the next A, and direct it to X with 3001. After applying this sequence to other areas marked by ?, we update the map as the following:

?#OO?
OOXOO
OO##O
OOO#O

Now both ?'s have the same path length to X. We pick the upper-left corner to be A since it has the smaller sequence 211. The map is updated as the following:

O#OOO
OOXO?
OO##O
OOO#O

Finally apply 33, we can direct the last ? to X.

Hence the output sequence is 0011 3001 211 33.

①找到可能有人的(刚开始所有的O都可能有人),到X的最短路径最长的每个点,标记为A

②找到 沿着最短路径从A到X的最小序列s

③所有的人同时按s序列走,遇到#或墙则停止,此时所有可能有人的地方标记为

④如果X是唯一可能有人的地方,则结束;否则返回步骤①。

大模拟,考试的时候只有10位同学AC,我也只拿了19分,TLE,我是用了m*n次广搜来记录所有点到X的最短距离。

学姐的思路是首先一遍bfs计算并保存所有O点到X的最短路径,之后的每一次循环只需要从最短路径最长的点里进行广搜找最小序列就可以了。

放上紫薇学姐的题解

/*        
        0
        ↑
    3←       →1
        ↓
        2 

    ?为人所在位置 
    有三个距离最远(4)位置,pick了代码最small的,给了他0011指令
        ?#???
        ??X??
        ??##?
        ???#?
    起初图中所有0点都可能有人,所有人执行指令0011后(撞墙不走、到X不走),可能在四个?点处 
        ?#OO?
        OOXO?
        OO##O
        OO?#O
    pick了距离最远(4)的最下面一排的问号,给了他3001指令...... 
    
    
    ans的本质是:所以遍布在各个位置的人,听到这些指令后一一执行,或早或晚最后都能到X这个位置 

*/

#include
using namespace std;
const int INF=0x3f3f3f;
char p[105][105];
int len[105][105],vis[105][105];
typedef pair<int, int> PII;
map<PII,int> is_hiker;

struct node
{
    int x,y; 
    //{x,y}到X的路 
    string path;
};

//先搜出x到所有0的距离 
void bfs(int x,int y)
{
    memset(vis,0,sizeof vis); 
    queue<PII>q;
    q.push({x,y});
    vis[x][y]=1;
    while(!q.empty())
    {
        PII cur=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int tx=cur.first,ty=cur.second;
            if(i==0)tx--;
            if(i==1)ty++;
            if(i==2)tx++;
            if(i==3)ty--;
            if(!vis[tx][ty]&&is_hiker[{tx,ty}])
            {
                vis[tx][ty]=1;
                len[tx][ty]=len[cur.first][cur.second]+1;
                q.push({tx,ty});
            }
        }
    }
}

//求出{x,y}到x的最small的路,反之求不出来 
string cal_path(int x,int y)
{
    memset(vis,0,sizeof vis);
    queue<struct node> q;
    struct node start={x,y,""};
    q.push(start);
    vis[x][y]=1;
    while(true)
    {
        struct node cur=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int tx=cur.x,ty=cur.y;
            if(i==0)tx--;
            if(i==1)ty++;
            if(i==2)tx++;
            if(i==3)ty--;
            if(!vis[tx][ty]&&p[tx][ty]!='#')
            {
                vis[tx][ty]=1;
                char ch='0'+i;
                if(p[tx][ty]=='X')return cur.path+ch;
                struct node nex={tx,ty,cur.path+ch};
                q.push(nex);
            }
        }
    }
}

//模拟从{tx,ty}按照s指令会走到哪个位置 
pair<int, int> walk(int tx,int ty,string s)
{
    for(int i=0;i<s.size();i++)
    {
        //遇墙不走 
        if(s[i]=='0'&&p[tx-1][ty]!='#')tx--;
        if(s[i]=='1'&&p[tx][ty+1]!='#')ty++;
        if(s[i]=='2'&&p[tx+1][ty]!='#')tx++;
        if(s[i]=='3'&&p[tx][ty-1]!='#')ty--;
        //终点不走 
        if(p[tx][ty]=='X')return {tx,ty};
    }
    return {tx,ty};
}

int main()
{
    int n,m;
    cin>>n>>m;
    //把边围起来 
    int x,y;
    queue<PII> hiker_place;
    for(int i=0;i<=m+1;i++)p[0][i]='#',p[n+1][i]='#',len[0][i]=len[n+1][i]=INF;
    for(int i=0;i<=n+1;i++)p[i][0]='#',p[i][m+1]='#',len[i][0]=len[i][m+1]=INF;
    for(int i=1;i<=n;i++)
    {
        getchar();
        for(int j=1;j<=m;j++)
        {
            cin>>p[i][j];
            if(p[i][j]=='X')x=i,y=j;
            if(p[i][j]=='#')len[i][j]=len[j][i]=INF;
            //hiker_place是人可能在的地方的队列集合,is_hiker是判断该点是否可能有人 
            if(p[i][j]=='O')hiker_place.push({i,j}),is_hiker[{i,j}]=1;
        }
    }
    //找X到O的最远距离 
    bfs(x,y);
        
    string ans="";
    while(!hiker_place.empty())
    {
        int max_len=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(len[i][j]==INF)continue;
                else if(is_hiker[{i,j}]&&len[i][j]>max_len)max_len=len[i][j];
            }
        }

        string tmp=""; 
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(is_hiker[{i,j}]&&len[i][j]==max_len)
                {
                    string path=cal_path(i,j);
                    if(tmp==""||path<tmp)tmp=path;
                }
            }
        }
        ans+=tmp;
        
        //清空人的位置,将该步所有人的可能位置按照tmp指令进行模拟,得到新的所有人的可能位置,赋值给queue及map 
        is_hiker.clear();
        queue<pair<int, int> > tmp_place;
        while(!hiker_place.empty())
        {
            pair<int, int> cur=hiker_place.front(),nex;
            hiker_place.pop();
            nex=walk(cur.first,cur.second,tmp);
            if(!is_hiker[nex]&&p[nex.first][nex.second]!='X')
            {
                is_hiker[nex]=1;
                tmp_place.push(nex);
            }
        }
        hiker_place=tmp_place;
    }
    cout<<ans<<endl;
    return 0;
}

[1] PAT-2022年春季考试-甲级,题解

你可能感兴趣的:(PAT甲级,C/C++,C++,c++)