一个像贪吃蛇的问题?高手拿分

dawnhorizon 2003-05-04 02:15:31
http://acm.zju.edu.cn/show_problem.php?pid=1056

我的解法是这样的:
#include <iostream.h>

int offx[4]={0,1,0,-1};
int offy[4]={1,0,-1,0};
int all[52][52];
int head_x,head_y,tail_x,tail_y;
char step[100];

void move(int mhead_x,int mhead_y,int mtail_x,int mtail_y,int last,int forward)
{
all[mtail_x+offx[last]][mtail_y+offy[last]]-=1;
all[mhead_x+offx[forward]][mhead_y+offy[forward]]+=1;
tail_x=mtail_x+offx[last];
tail_y=mtail_y+offy[last];
head_x=mhead_x+offx[forward];
head_y=mhead_y+offy[forward];

}

int change(char ch)
{
switch (ch)
{
case 'E':return 0;
case 'S':return 1;
case 'W':return 2;
case 'N':return 3;
default:return 4;
}
}

int main()
{
int i,j,temp;

int n;
char ch,lch='E';
int inc=19;
int dec=0;
cin>>n;
while (n!=0)
{

for (i=0;i<52;i++)
{
for (j=0;j<52;j++)
{
all[i][j]=0;
}
}


for (i=11;i<31;i++)
{
all[25][i]=1;
}

for (i=0;i<100;i++)
{
step[i]='E';
}
head_x=25;
head_y=30;
tail_x=25;
tail_y=11;

for (i=0;i<n;i++)
{
cin>> ch;
step[inc]=ch;
lch=step[dec];
move(head_x,head_y,tail_x,tail_y,change(lch),change(ch));
if (all[head_x][head_y]>1)
{
cout<<"The worm ran into itself on move "<<i+1<<"."<<endl;
temp=i;
i=n+1;
}
for (int m=0;m<52;m++)
{
if ((all[0][m]>0) ¦ ¦(all[51][m]>0) ¦ ¦(all[m][0]>0) ¦ ¦(all[m][51]>0))
{
cout<<"The worm ran off the board on move "<<i+1<<"."<<endl;
temp=i;
i=n+1;
m=53;
}
}
inc++;
dec++;
}

if (i==n)
{
temp=i;
cout<<"The worm successfully made all "<<n<<" moves."<<endl;
}



if (temp!=i)
{
for (int k=temp+1;k<n;k++)
cin>>ch;
}

cin>>n;



}

return 0;
}


但是Wrong Answer.ft死了,给看一下吧,测试了很多,都可以啊
...全文
39 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
lymgf 2003-05-04
  • 打赏
  • 举报
回复
我的代码:

#include <stdio.h>

#define SIZE 50
#define LEN 20
#define INIT (25<<8)+11

int main()
{
int n;
char c;
short snake[LEN];
int i,j,bEnd,head;
scanf("%d\n",&n);
while (n)
{
bEnd=0;
head=19;
for (i=0;i<LEN;i++)
snake[i]=INIT+i;
for (i=0;i<n;i++)
{
scanf("%c",&c);
if (!bEnd)
{
switch (c)
{
case 'E':
snake[(head+1)%20]=snake[head]+1;
break;
case 'S':
snake[(head+1)%20]=snake[head]+0x100;
break;
case 'W':
snake[(head+1)%20]=snake[head]-1;
break;
case 'N':
snake[(head+1)%20]=snake[head]-0x100;
break;
}
head=(head+1)%20;
if ((snake[head]>>8)>SIZE || (snake[head] & 0xff)>SIZE
|| (snake[head]>>8)<1 || (snake[head ] & 0xff)<1)
{
bEnd=1;
printf("The worm ran off the board on move %d.\n",i+1);
}
else
for (j=1;j<=16;j++)
if (snake[head]==snake[(head+j)%20] && snake[head]==snake[(head+j)%20])
{
bEnd=1;
printf("The worm ran into itself on move %d.\n",i+1);
break;
}
}
}
if (!bEnd)
printf("The worm successfully made all %d moves.\n",n);
scanf("%d\n",&n);
}

return 0;
}
lymgf 2003-05-04
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/1736/1736289.xml?temp=.4831812

33,007

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧