1.bfs广搜`#include<bits/stdc++.h>
using namespace std;
int a[100][100],v[100][100];
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
struct point
{
int x;
int y;
int step;
};
queuer;
int main()
{
/*
5 4
1 1 2 1
1 1 1 1
1 1 2 1
1 2 1 1
1 1 1 2
1 1 4 3
*/
//输入
int n,m,startx,starty,p,q;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
scanf("%d",&a[i][j]);
}
scanf("%d%d%d%d",&startx,&starty,&p,&q);
//BFS
point start;
start.x=startx;
start.y=starty;
start.step =0;
r.push(start);//将起点入队
v[startx][starty]=1;
int flag=0;
while(!r.empty())
{
int x=r.front().x,y=r.front().y;
if(xp&&yq)
{
flag=1;
printf("%d",r.front().step);
break;
}
for(int k=0;k<=3;k++)
{
int tx,ty;
tx=x+dx[k];
ty=y+dy[k];
if(a[tx][ty]==1&&v[tx][ty]==0)
{
//入队
point temp;
temp.x=tx;
temp.y=ty;
temp.step =r.front().step+1;
r.push(temp);
v[tx][ty]=1;
}
}
r.pop();//对首元素出队;
}
if(flag==0)
printf("no answer!");
return 0;
}
`
运用队列进行存储每一步走到哪里,找到目标就停止
2.错排
#include<bits/stdc++.h>
using namespace std;
long long a[21];
void init()
{
a[1]=0;
a[2]=1;
for(int i=3;i<=20;i++)
a[i]=(i-1)*(a[i-1]+a[i-2]);
}
int main()
{
int n;
init();
while(cin>>n)
{
printf("%d\n",a[n]);
}
return 0;
}
一开始想了好久,后来才知道有错排公式
3.链表的插入与删除(手写)个人感觉手写链表更加易懂详见洛谷的p1160
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct node{
int L, R;
}a[100003];
int n, m;
inline void addRight(int x, int pos) { //插入右边
a[x].L = pos;
a[a[pos].R].L = x;
a[x].R = a[pos].R;
a[pos].R = x;
}
inline void addLeft(int x, int pos) { //插入左边
a[x].R = pos;
a[a[pos].L].R = x;
a[x].L = a[pos].L;
a[pos].L = x;
}
inline void del(int x) {
if(a[x].L == -1) return;
a[a[x].L].R = a[x].R;
a[a[x].R].L = a[x].L;
a[x].L = -1;
a[x].R = -1;
}
inline void go() {
int x = a[0].R;
while(1) {
cout<<x<<" ";
if(a[x].R == -1) break;
x = a[x].R;
}
}
inline void init() {
for(int i = 1; i <= n; ++i) a[i].L = a[i].R = -1;
a[1].R = -1; a[1].L = 0; a[0].R = 1;
}
int main() {
scanf("%d", &n);
int cmd1, cmd2;
init();
for(int i = 2; i <= n; ++i) {
scanf("%d %d", &cmd1, &cmd2);
if(!cmd2) addLeft(i, cmd1);
else addRight(i, cmd1);
}
scanf("%d", &m);
for(int i = 1; i <= m; ++i) {
scanf("%d", &cmd1);
del(cmd1);
}
go();
return 0;
}
4.进制转换与回文数(去补了补之前的usaco
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct node{
int L, R;
}a[100003];
int n, m;
inline void addRight(int x, int pos) { //插入右边
a[x].L = pos;
a[a[pos].R].L = x;
a[x].R = a[pos].R;
a[pos].R = x;
}
inline void addLeft(int x, int pos) { //插入左边
a[x].R = pos;
a[a[pos].L].R = x;
a[x].L = a[pos].L;
a[pos].L = x;
}
inline void del(int x) {
if(a[x].L == -1) return;
a[a[x].L].R = a[x].R;
a[a[x].R].L = a[x].L;
a[x].L = -1;
a[x].R = -1;
}
inline void go() {
int x = a[0].R;
while(1) {
cout<<x<<" ";
if(a[x].R == -1) break;
x = a[x].R;
}
}
inline void init() {
for(int i = 1; i <= n; ++i) a[i].L = a[i].R = -1;
a[1].R = -1; a[1].L = 0; a[0].R = 1;
}
int main() {
scanf("%d", &n);
int cmd1, cmd2;
init();
for(int i = 2; i <= n; ++i) {
scanf("%d %d", &cmd1, &cmd2);
if(!cmd2) addLeft(i, cmd1);
else addRight(i, cmd1);
}
scanf("%d", &m);
for(int i = 1; i <= m; ++i) {
scanf("%d", &cmd1);
del(cmd1);
}
go();
return 0;
}
5.USACO Training 1.5.2 Prime Palindromes
#include<bits/stdc++.h>
using namespace std;
bool book[10000001];
void prime(int b) {
memset(book, true, sizeof(book));
book[1]=false;
int n=sqrt(b);
for (int i=2;i<=n;i++) {
if (book[i]) {
for (int j=2;j<=b/i;j++)
book[i*j]=false;
}
}
}
bool isHWS(int num) {
int temp=num,ans=0;
while (temp!=0) {
ans=ans*10+temp%10;
temp/=10;
}
if (ans==num)
return true;
else
return false;
}
int main() {
int a,b;
cin>>a>>b;
if (b>=10000000)
b=9999999;
prime(b);
if(a>b)
return 0;
if (a%2==0) a++;
for (int i=a;i<=b;i+=2) {
if (book[i] && isHWS(i))
cout<<i<<endl;
}
return 0;
}
这题就是素数筛选与回文数的判断,原理很简单,不过第一遍做的时候超时了。
还有就是学了学map与set这两个容器的相关知识