| ||||||||||
| 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 | |||||||||
求助,代码虽然0MS AC了,但自测26*26时会有死循环...我想不出原因,直接调试数据太大也看不出什么,难道不能这么搜索?
# include <stdio.h>
# include <string.h>
int xn,yn,map[32][32],tot;
void init()
{
int i,j;
yn++;xn++;
memset(map,0,sizeof(map));
for(i=2;i<=yn;i++)
for(j=2;j<=xn;j++)
{
map[j][i]=1;
}
}
int ans[900][2],flag;
void dfs(int y,int x,int m)
{
if(flag) return;
ans[m][0]=x;ans[m][1]=y;
map[x][y]=0;
if(m==tot-1)
{
int i;
flag=1;
for(i=0;i<=m;i++)
{
printf("%c%d",ans[i][0]-2+'A',ans[i][1]-1);
}
return ;
}
if(map[x-2][y-1])
dfs(y-1,x-2,m+1);
if(map[x-2][y+1])
dfs(y+1,x-2,m+1);
if(map[x-1][y-2])
dfs(y-2,x-1,m+1);
if(map[x-1][y+2])
dfs(y+2,x-1,m+1);
if(map[x+1][y-2])
dfs(y-2,x+1,m+1);
if(map[x+1][y+2])
dfs(y+2,x+1,m+1);
if(map[x+2][y-1])
dfs(y-1,x+2,m+1);
if(map[x+2][y+1])
dfs(y+1,x+2,m+1);
map[x][y]=1;
}
int main()
{
//freopen("in","r",stdin);
int casen,i,j,k;
scanf("%d",&casen);
for(k=1;k<=casen;k++)
{
scanf("%d %d",&yn,&xn);tot=xn*yn;
init();
printf("Scenario #%d:\n",k);
flag=0;
dfs(2,2,0);
if(!flag) printf("impossible\n\n");
else printf("\n\n");
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator