| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
哪位高手帮忙看看,为什么过不了啊??(附代码)#include <stdio.h>
#include <stack>
#include <queue>
const int maxn=8;
int p,q;
int board[maxn][maxn];
typedef struct
{
int i,j;
}sss;
sss square[maxn*maxn];
using namespace std;
stack<sss> s;
int fk[maxn][2]={{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}};
void init()
{
int i,j;
for(i=0;i<maxn;i++)
for(j=0;j<maxn;j++)
{
board[i][j]=0;
square[i*maxn+j].i=i;
square[i*maxn+j].j=j;
}
}
bool DFS(int x,int y,int n)
{
s.push(square[x*maxn+y]);
board[x][y]=1;
if(n==p*q) return true;
int k,xx,yy;
for(k=0;k<maxn;k++)
{
xx=fk[k][0]+x;yy=fk[k][1]+y;
if(xx>=p||yy>=q||xx<0||yy<0) continue;
if(board[xx][yy]) continue;
if(DFS(xx,yy,n+1)) return true;
}
s.pop();
board[x][y]=0;
return false;
}
void doit()
{
char ch;
sss temp;
if(DFS(0,0,1))
{
queue<sss> q;
while(!s.empty())
{
q.push(s.top());
s.pop();
}
while(!q.empty())
{
s.push(q.front());
q.pop();
}
while(!s.empty())
{
temp=s.top();
s.pop();
ch=temp.j+65;
printf("%c%d",ch,temp.i+1);
}
}
else
printf("impossible");
printf("\n\n");
}
int main()
{
int N;
int i;
scanf("%d",&N);
init();
for(i=0;i<N;i++)
{
scanf("%d%d",&p,&q);
printf("Scenario #%d:\n",i+1);
doit();
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator