1.不同正整数的个数
题解思路:这题用y总的话来说就是一道大水题,很多方法,我直接放上我的代码仅供参考吧。
#include<bits/stdc++.h>
using namespace std;
int st[1000];
int main()
{
int cnt;
int n,a[1000];
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
st[a[i]]=1;
}
for(int i=1;i<610;i++)
{
if(st[i]==1)cnt++;
}
cout<<cnt<<endl;
return 0;
}
2.最短路径
题解思路:这题一看就是BFS,不过要注意边界问题,要满足两种情况,1.不能有环,这样会影响最短路的判断,2路径间不能相邻,除了路劲移动时相连的之外
#include<bits/stdc++.h>
using namespace std;
const int N=210, B=N/2;
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
bool st[N][N];//判重数组
int get(char c)
{
if(c=='U')return 0;
if(c=='R')return 1;
if(c=='D')return 2;
return 3;
}
int main()
{
string str;
cin>>str;
int x=B,y=B;
bool res=true;
st[x][y]=true;
for(auto c:str)
{
int d=get(c);
x+=dx[d],y+=dy[d];
if(st[x][y])res=false;
for(int i=0;i<4;i++)
if(i!=(d^2)&&st[x+dx[i]][y+dy[i]])//d^2表示方向取反
res=false;
st[x][y]=true;
}
if(res)puts("YES");
else puts("NO");
return 0;
}