马的遍历问题-问题分析
来源:互联网 发布:网络传输协议四层 编辑:程序博客网 时间:2024/05/08 11:46
#include<stdio.h>
int a[100][100] = {0};
int tot;
int check(int n, int m)
{
if(a[n][m]==0)
return 1;
else
return 0;
}
int sb(int n, int m, int s, int I, int J)
{
int i, j, N, M;
if(s == n*m+1)
{
tot++;
printf("%d\n", tot);
for(N=0; N<n; N++)
{
for(M=0; M<m; M++)
printf("%3d ", a[N][M]);
printf("\n");
}
}
else
{
if(s == 1)
{
for(i=0; i<n; i++)
for(j=0; j<m; j++)
{
a[i][j] = s;
sb(n, m, s+1, i, j);
a[i][j] = 0;
}
}
if(s>1)
{
for(i=0; i<n; i++)
for(j=0; j<m; j++)
{
if(((i==I+2)&&(j==J+1))||((i==I+2)&&(j==J-1))||
((i==I+1)&&(j==J+2))||((i==I-1)&&(j==J+2))||
((i==I-2)&&(j==J+1))||((i==I-2)&&(j==J-1))||
((i==I-1)&&(j==J-2))||((i==I+1)&&(j==J-2)))
if(check(i, j))
{
a[i][j] = s;
sb(n, m, s+1, i, j);
a[i][j] = 0;
}
}
}
}
}
int main()
{
int i, j, n, m;
while(scanf("%d%d", &n, &m)!=EOF && n!=0)
{
tot = 0;
sb(n, m, 1, 0, 0);
}
}
int a[100][100] = {0};
int tot;
int check(int n, int m)
{
if(a[n][m]==0)
return 1;
else
return 0;
}
int sb(int n, int m, int s, int I, int J)
{
int i, j, N, M;
if(s == n*m+1)
{
tot++;
printf("%d\n", tot);
for(N=0; N<n; N++)
{
for(M=0; M<m; M++)
printf("%3d ", a[N][M]);
printf("\n");
}
}
else
{
if(s == 1)
{
for(i=0; i<n; i++)
for(j=0; j<m; j++)
{
a[i][j] = s;
sb(n, m, s+1, i, j);
a[i][j] = 0;
}
}
if(s>1)
{
for(i=0; i<n; i++)
for(j=0; j<m; j++)
{
if(((i==I+2)&&(j==J+1))||((i==I+2)&&(j==J-1))||
((i==I+1)&&(j==J+2))||((i==I-1)&&(j==J+2))||
((i==I-2)&&(j==J+1))||((i==I-2)&&(j==J-1))||
((i==I-1)&&(j==J-2))||((i==I+1)&&(j==J-2)))
if(check(i, j))
{
a[i][j] = s;
sb(n, m, s+1, i, j);
a[i][j] = 0;
}
}
}
}
}
int main()
{
int i, j, n, m;
while(scanf("%d%d", &n, &m)!=EOF && n!=0)
{
tot = 0;
sb(n, m, 1, 0, 0);
}
}
0 0
- 马的遍历问题-问题分析
- 马的遍历问题
- 马的遍历问题
- 马的遍历问题
- 马的遍历问题
- 马的遍历问题
- 马的遍历问题
- [算法]图算法之骑士遍历问题(象棋中马的遍历问题)分析,C语言实现
- 中国象棋马的遍历问题
- cursor遍历的问题
- 树的遍历问题
- 【5003】马遍历问题
- 【数据结构】马踏棋盘的遍历问题
- 【经典算法问题】马的遍历【回溯】
- 遍历问题
- 遍历问题
- 遍历问题
- 关于hashmap的遍历问题
- 分支-08. 高速公路超速处罚(15)
- Windows与Linux的一些个人理解
- UVA - 11609 Teams (排列组合数公式)
- 2.2.1 使用不可变值
- Jquery 时间处理
- 马的遍历问题-问题分析
- 离散数学及其应用—— ch4 归纳与递归
- 学习 $.messager.show 的简单使用
- JAVA中的public static void main(String[] args)
- ubuntu设置时区,网上同步时间
- android 4.2 OutOfMemoryError
- 编程中无穷大常量的设定技巧
- HDU 3452 Bonsai(网络流之最小割)
- poj1149PIGS(最大流+建图)