1 条题解
-
0
C :
#include<stdio.h> long long a[21][21]={0}, b[21][21]={0}; long long fangfa(int n,int m,int x,int y) { int i,j; a[x][y]=1; b[0][0]=1; if(x==2&&y==1||x==1&&y==2) return 0; if(0<=x-2&&x-2<=n&&0<=y-1&&y-1<=m) a[x-2][y-1]=1; if(0<=x+2&&x+2<=n&&0<=y-1&&y-1<=m) a[x+2][y-1]=1; if(0<=x-1&&x-1<=n&&0<=y-2&&y-2<=m) a[x-1][y-2]=1; if(0<=x+1&&x+1<=n&&0<=y-2&&y-2<=m) a[x+1][y-2]=1; if(0<=x+2&&x+2<=n&&0<=y+1&&y+1<=m) a[x+2][y+1]=1; if(0<=x-2&&x-2<=n&&0<=y+1&&y+1<=m) a[x-2][y+1]=1; if(0<=x-1&&x-1<=n&&0<=y+2&&y+2<=m) a[x-1][y+2]=1; if(0<=x+1&&x+1<=n&&0<=y+2&&y+2<=m) a[x+1][y+2]=1; for(i=1;i<=n;i++) if(a[i][0]==0) b[i][0]=b[i-1][0]; for(j=1;j<=m;j++) if(a[0][j]==0) b[0][j]=b[0][j-1]; for(i=1;i<=n;i++) for(j=1;j<=m;j++) { if(a[i][j]) continue; b[i][j]=b[i-1][j]+b[i][j-1]; } return b[n][m]; } int main() { int n,m,x,y; scanf("%d%d%d%d",&n,&m,&x,&y); printf("%lld\n",fangfa(n,m,x,y)); return 0; }
C++ :
#include <iostream> using namespace std; long long dp[25][25]={0}; int dx[9]={0,-1,-2,-2,-1, 1, 2, 2, 1}; int dy[9]={0, 2, 1,-1,-2, 2, 1,-1,-2}; int n,m,x,y; int main() { cin>>n>>m>>x>>y; n+=2; m+=2; x+=2; y+=2; for (int i=0;i<9;++i) dp[x+dx[i]][y+dy[i]]=-1; for (int i=2;i<=n;++i){ if (dp[i][2]==-1) break; dp[i][2]=1; } for (int i=2;i<=m;++i){ if (dp[2][i]==-1) break; dp[2][i]=1; } if (dp[2][2]==-1 || dp[n][m]==-1) return 0; for (int i=3;i<=n;++i) for (int j=3;j<=m;++j) if (dp[i][j]!=-1){ if (dp[i-1][j]!=-1) dp[i][j]+=dp[i-1][j]; if (dp[i][j-1]!=-1) dp[i][j]+=dp[i][j-1]; } cout<<dp[n][m]<<endl; return 0; }
Pascal :
const c:array[1..8]of integer=(1,1,-1,-1,2,2,-2,-2); d:array[1..8]of integer=(-2,2,-2,2,1,-1,-1,1); var b:array[-2..21,-2..21]of boolean; i,j,n,m,x,y:integer; f:array[0..21,0..21]of int64; begin readln(n,m,x,y); fillchar(b,sizeof(b),true); fillchar(f,sizeof(f),0); b[x,y]:=false; for i:=1 to 8 do b[x+c[i],y+d[i]]:=false; f[0,0]:=1; for i:=1 to m do if not b[0,i] then break else f[0,i]:=f[0,i-1]; for i:=1 to n do if not b[i,0] then break else f[i,0]:=f[i-1,0]; for i:=1 to n do for j:=1 to m do if b[i,j] then f[i,j]:=f[i-1,j]+f[i,j-1]; writeln(f[n,m]); end.
Java :
import java.util.Scanner; public class Main { public static final int MAX_N = 25; public static int n,m; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); n = scanner.nextInt(); m = scanner.nextInt(); int x = scanner.nextInt(); int y = scanner.nextInt(); long dp[][] = new long[MAX_N][MAX_N]; boolean map[][] = new boolean[MAX_N][MAX_N]; int horseFang[][] ={{2,1},{2,-1},{-2,1},{-2,-1},{1,2},{1,-2},{-1,2},{-1,-2}}; dp[0][0]=1; map[x][y]=true; for(int i=0;i<8;i++){//标记控制点 int xx=x+horseFang[i][0]; int yy=y+horseFang[i][1]; if(inMap(xx,yy)){ map[xx][yy]=true; } } if(map[0][0])//特判 System.out.println(0); for(int i=0;i<=n;i++) for(int j=0;j<=m;j++) if(!map[i][j]){ if(i>=1) dp[i][j]+=dp[i-1][j]; if(j>=1) dp[i][j]+=dp[i][j-1]; } System.out.println(dp[n][m]); } private static boolean inMap(int xx,int yy){ if(xx<0||yy<0||xx>n||yy>m) return false; return true; } } /* #define MAXN 25 #define LL long long int mapp[MAXN][MAXN]; LL dp[MAXN][MAXN]; int horseFang[MAXN][2]={2,1,2,-1,-2,1,-2,-1,1,2,1,-2,-1,2,-1,-2}; int x,y,n,m; bool inMap(int xx,int yy){ if(xx<0||yy<0||xx>n||yy>m) return false; return true; } void init(){ memset(dp,0,sizeof(dp)); memset(mapp,0,sizeof(mapp)); dp[0][0]=1; mapp[x][y]=1; for(int i=0;i<8;i++){ int xx=x+horseFang[i][0]; int yy=y+horseFang[i][1]; if(inMap(xx,yy)){ mapp[xx][yy]=1; } } } void solve(){ if(mapp[0][0]){ cout<<0<<endl; return; } for(int i=0;i<=n;i++) for(int j=0;j<=m;j++) if(!mapp[i][j]){ if(i>=1) dp[i][j]+=dp[i-1][j]; if(j>=1) dp[i][j]+=dp[i][j-1]; } cout<<dp[n][m]; } int main(){ cin>>n>>m>>x>>y; init(); solve(); return 0; } */
- 1
信息
- ID
- 217
- 时间
- 1000ms
- 内存
- 125MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者