马的走法
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 40 Accepted Submission(s) : 14
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
在一个4×5的棋盘上,求马能返回初始位置的所有不同走法的总数(马走过的位置不能重复,马走“日”字)。
Input
从输入文件中读入数据。文件的第一行马的初始位置的个数n,后面n行是初始位置坐标。
Output
对于每个初始位置给出走法总数,如果不能回到初始位置,输出“ERROR”。
Sample Input
1 2 2
Sample Output
4596
#include<string.h> #include <stdio.h> #include <iostream> using namespace std; int g[5][6]; int d[8][2]={2,1,1,2,-1,2,-2,1,-2,-1,-1,-2,1,-2,2,-1}; int Sx,Sy; int cnt; void DFS(int x,int y) { int i,newx,newy; for (i=0;i<8;i++) { newx=x+d[i][0]; newy=y+d[i][1]; if (newx>=1&&newx<=4&&newy>=1&&newy<=5) { if (newx==Sx&&newy==Sy) cnt++; else if (g[newx][newy]==0) { g[newx][newy]=1; DFS(newx,newy); g[newx][newy]=0; } } } } int main() { int t; cin>>t; while(t--) { cin>>Sx>>Sy; if (Sx>=1&&Sx<=4&&Sy>=1&&Sy<=5) { memset(g,0,sizeof(g)); cnt=0; g[Sx][Sy]=1; DFS(Sx,Sy); if (cnt) cout<<cnt<<endl; else cout<<"ERROR"<<endl; } else cout<<"ERROR"<<endl; } return 0; }