The first question: AC70% n*n matrix, the shortest path from one point to another, # @ are all obstacles, all others can be taken, find the shortest number of steps.
Idea: Start from the starting point and start directly with bfs Search, and return the answer directly when you reach the end for the first time;
(This is an update): The 70% reason for the card is that the coordinates given in the title are yxyx, and then I treat it as xyxy.
Why do you have to do this with us! ! ! ! ! ! ! Who made this data! ! ! ! ! Is it useful to leave this pit on purpose? ? ? ? ? ? (One step is AK)
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
struct node
{
int t;
int x,y;
node(int t,int x,int y):t(t),x(x),y(y){}
friend bool operator < (node a,node b)
{
return a.t>b.t;
}
};
int foot[4][2]={-1,0,1,0,0,-1,0,1};
int main() {
int n,xb,yb,xe,ye;
cin>>n;
cin>>xb>>yb>>xe>>ye;getchar();
vector<string> a(n);
vector<vector<int> >p(n,vector<int>(n,0));
for (int i=0;i<n;i++)
cin>>a[i];
//for (int i=0;i<n;i++)
//cout<<a[i]<<endl;
priority_queue<node> q;
q.push({0,xb,yb});
p[xb][yb]=1;
int aa=1;
while(!q.empty())
{
node w=q.top();q.pop();
//cout<<w.x<<' '<<w.y<<' '<<w.t<<' '<<a[w.x][w.y]<<endl;
if (w.x==xe && w.y==ye) {aa=0;cout<<w.t<<endl;break;}
for (int i=0;i<4;i++)
{
int x=w.x+foot[i][0];
int y=w.y+foot[i][1];
if (x>=0 && x<n && y>=0 && y<n)
if (a[x][y]!='#'&&a[x][y]!='@')
if (p[x][y]==0)
{
q.push({w.t+1,x,y});
p[x][y]=1;
}
}
}
if (aa==1)cout<<-1<<endl;
return 0;
}
The second question: AC100% gives a string, if removing a character can form a palindrome, output that answer, and output the palindrome answer with the character at the left end deleted.
Idea: Direct brute force calculation, and try from the left to see if Form a palindrome, output the answer directly if successful
The topic is about whether to delete at most one character and turn it into a palindrome, then it should not be deleted, but 80% of it is not deleted. You must delete one by force, which is really disgusting
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
bool pd(string s)
{
int len=s.length();
for (int i=0;i<len/2;i++)
if (s[i]!=s[len-1-i])
return false;
return true;
}
int main() {
string s;
getline(cin,s);
//if (pd(s)) cout<<s<<endl;
//else
{
int aa=1;
for (int i=0;i<s.size();i++)
{
string s1=s;
s1.erase(s1.begin()+i);
if (pd(s1))
{
cout<<s1<<endl;
aa=0;
break;
}
}
if (aa==1) cout<<"false"<<endl;
}
return 0;
}
The third question: AC100% gives a directed tree and outputs the topological sorting
idea with the smallest lexicographic order : use the priority queue of the small root heap to traverse in turn
Code reference from
class Solution {
vector<int> a;
int n;
void getInt(string s) {
stringstream ss(s);
while(getline(ss, s, ',')) a.push_back(atoi(s.c_str()));
}
public:
string compileSeq(string input) {
a.clear();
getInt(input);
n = a.size();
string ans;
vector<int> indeg(n+1);
vector<vector<int>> tree(n);
for(int i = 0; i < n; ++i) {
if(a[i] == -1) continue;
tree[a[i]].push_back(i);
indeg[i]++;
}
priority_queue<int, vector<int>, greater<int>> heap;
for(int i = 0; i < n; ++i) {
if(indeg[i] == 0) heap.push(i);
}
while(!heap.empty()) {
int cur = heap.top();
heap.pop();
if(ans.size() == 0) ans.append(to_string(cur));
else ans.append(",").append(to_string(cur));
for(int v : tree[cur]) {
if((--indeg[v]) == 0) {
heap.push(v);
}
}
}
return ans;
}
};
Learn about the big guys
stringstream ss(s);
while(getline(ss, s, ',')) a.push_back(atoi(s.c_str()));
Usage