| ||||||||||
| 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 | |||||||||
我数组模拟栈,循环模拟递归成功0 ms#include <iostream>
using namespace std;
int step[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
int main()
{
int a,b,ch[26][26],ss[26*26][3],s,p,x,y,j,xx,yy,ci=0;
cin>>a;
while(cin>>b>>a)
{
memset(ch,0,sizeof(ch));
p=a*b-1;
s=0;
ch[0][0]=1;//visit
x=0;
y=0;
j=0;
while(s>=0)
{
if (j<8)
for (;j<8;j++)
{
xx=x+step[j][0];
yy=y+step[j][1];
if (ch[xx][yy]==0 && xx>=0 && xx<a && yy>=0 && yy<b)
{
ss[s][0]=x+65;
x=xx;
ss[s][1]=y+1;
y=yy;
ss[s][2]=j;
s++;
ch[xx][yy]=1;
j=0;
break;
}
}
if (s==p)
break;
if (j==8)
{
s--;
if (s>=0)
{
ch[x][y]=0;
x=ss[s][0]-65;
y=ss[s][1]-1;
j=ss[s][2]+1;
}
}
}
++ci;
printf("Scenario #%d:\n",ci);
if (s==p)
{
for (p=0;p<s;p++)
cout<<char(ss[p][0])<<ss[p][1];
x+=65;
cout<<char(x)<<y+1;
}
else
cout<<"impossible";
cout<<endl<<endl;
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator