取数游戏
#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
struct node{int v;bool f;
};
node a[10][10];
int n,m,ma;
void f(int,int,int);
int main(){cin>>n>>m;for(int i=0;i<n;i++){for(int j=0;j<m;j++){cin>>a[i][j].v;a[i][j].f=0;}}f(1,1,0);cout<<ma;return 0;
}
void f(int i,int j,int sum){if(i==2){ma=max(ma,sum);return;}int val=a[i][j].v;j++;if(j==m+1){j=0;i++;}if(a[i][j].f==0){a[i+1][j].f=1;a[i-1][j].f=1;a[i][j+1].f=1;a[i][j-1].f=1;a[i+1][j+1].f=1;a[i-1][j-1].f=1;a[i+1][j-1].f=1;a[i-1][j+1].f=1;f(i,j,sum+val);a[i+1][j].f=0;a[i-1][j].f=0;a[i][j+1].f=0;a[i][j-1].f=0;a[i+1][j+1].f=0;a[i-1][j-1].f=0;a[i+1][j-1].f=0;a[i-1][j+1].f=0;}f(i,j,sum);}
走出迷宫的最少步数2
#include<bits/stdc++.h>
using namespace std;
int m[5][9]={0};
int xx[4]={2,1,-1,-2};
int yy[4]={1,2,2,1};
int b[50],c[50];
int f(int x,int y,int deep);
int main()
{f(0,0,1);return 0;
}
int f(int x,int y,int deep)
{if(x==4&&y==8){ for(int j=1;j<deep;j++){cout<<b[j]<<","<<c[j]<<"->";}cout<<"4,8";cout<<endl;}else{for(int i=0;i<4;i++){if(x+xx[i]>=0&&x+xx[i]<=4&&y+yy[i]>=0&&y+yy[i]<=8){b[deep]=x;c[deep]=y;m[x][y] = 1;f(x+xx[i],y+yy[i],deep+1);m[x][y] = 0;}}}
}
马的遍历
#include<bits/stdc++.h>
using namespace std;
int n;
int a[20][20];
int dx[8] = {-2,-2,-1,1,2,2,1,-1};
int dy[8] = {-1,1,2,2,1,-1,-2,-2};
int flag = 0;
void f(int x,int y,int n);
int main()
{cin >> n;a[1][1] = 1;f(1,1,1);for(int i = 1; i <= n; ++i){for(int j = 1; j <= n; ++j){cout << a[i][j];if(j < n)cout << " ";}cout << endl;}return 0;
}
void f(int x,int y,int n)
{int x1,y1;if(n == n*n){flag = 1;return;}for(int i = 0; i < 8; ++i){x1 = x + dx[i];y1 = y + dy[i];if(x1>0&&x1<=n&&y1>0&&y1<=n&&!a[x1][y1]){n = n+1;a[x1][y1] = n;f(x1,y1,n);n = n-1;if(flag) return;a[x1][y1] = 0;}x1 = x - dx[i];y1 = y - dy[i];}
}
放苹果
#include<iostream>
using namespace std;
int a[20][20];
int f(int m,int n);int main()
{int m,n,k;cin>>k;for(int i=1;i<=k;i++){cin>>m>>n;f(m,n);cout<<a[m][n]<<endl;}return 0;
}
int f(int m,int n)
{int i,j;for(i=1;i<=n;i++){a[0][i]=1;}for(i=1;i<=m;i++){a[i][1]=1;}for(i=1;i<=m;i++){for(j=2;j<=n;j++){if(i<j){a[i][j]=a[i][i];}else{a[i][j]=a[i][j-1]+a[i-j][j];}}}
}