杭电 1010,我的总是WA,下面是我的c语言代码,请大牛指点。http://acm.hdu.edu.cn/showproblem.php?pid=10
#include<stdio.h>#defineMAX10000struct{introw,col;intcount;//记录走了几步charflag[10][10];/...
#include<stdio.h>
#define MAX 10000
struct
{
int row,col;
int count;//记录走了几步
char flag[10][10];//用来记录走过的格子,不能重复
}que[MAX];
int main()
{
int m,n,T,i,j,k;
int vary[8]={-1,0,0,1,1,0,0,-1};
int front,rear,start_r,start_c,tag,temp1,temp2;
char maze[10][10],ch;
while(scanf("%d%d%d",&m,&n,&T),m||n||T)
{
getchar();
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
ch=getchar();
maze[i][j]=ch;
if(ch=='S')
{
start_r=i;
start_c=j;
}
}
getchar();
}
front = 0;rear = 0;tag = 0;
que[rear].row = start_r;
que[rear].col = start_c;
que[rear].count = 0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
que[rear].flag[i][j]=0;
}
}
que[rear].flag[start_r][start_c]=1;
rear++;
while(tag == 0&&front != rear)
{
for(i=0;i<8;i=i+2)
{
temp1 = que[front].row + vary[i];
temp2 = que[front].col + vary[i+1];
if(temp1 >= 0&&temp1 < m&&temp2 >= 0&&temp2 < n&&que[front].flag[temp1][temp2] == 0&&maze[temp1][temp2]!='X')
{
if(maze[temp1][temp2] == 'D')
{
if(que[front].count+1 == T)
{
tag = 1;
printf("YES\n");
break;
}
}
else
{
if(que[front].count<T)//大于等于T的已经不可能在规定的T时间内到达D,不再入队,只对小于T的入队
{
que[rear].row = temp1;
que[rear].col = temp2;
que[rear].count = que[front].count + 1;
for(j=0;j<m;j++)
{
for(k=0;k<n;k++)
{
que[rear].flag[j][k]=que[front].flag[j][k];
}
}//对每一次路线都记录已经走过的格子
que[rear].flag[temp1][temp2]=1;
rear = (rear + 1) % MAX;
}
}
}
}
front = (front + 1) % MAX;
}
if(tag == 0)printf("NO\n");
}
} 展开
#define MAX 10000
struct
{
int row,col;
int count;//记录走了几步
char flag[10][10];//用来记录走过的格子,不能重复
}que[MAX];
int main()
{
int m,n,T,i,j,k;
int vary[8]={-1,0,0,1,1,0,0,-1};
int front,rear,start_r,start_c,tag,temp1,temp2;
char maze[10][10],ch;
while(scanf("%d%d%d",&m,&n,&T),m||n||T)
{
getchar();
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
ch=getchar();
maze[i][j]=ch;
if(ch=='S')
{
start_r=i;
start_c=j;
}
}
getchar();
}
front = 0;rear = 0;tag = 0;
que[rear].row = start_r;
que[rear].col = start_c;
que[rear].count = 0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
que[rear].flag[i][j]=0;
}
}
que[rear].flag[start_r][start_c]=1;
rear++;
while(tag == 0&&front != rear)
{
for(i=0;i<8;i=i+2)
{
temp1 = que[front].row + vary[i];
temp2 = que[front].col + vary[i+1];
if(temp1 >= 0&&temp1 < m&&temp2 >= 0&&temp2 < n&&que[front].flag[temp1][temp2] == 0&&maze[temp1][temp2]!='X')
{
if(maze[temp1][temp2] == 'D')
{
if(que[front].count+1 == T)
{
tag = 1;
printf("YES\n");
break;
}
}
else
{
if(que[front].count<T)//大于等于T的已经不可能在规定的T时间内到达D,不再入队,只对小于T的入队
{
que[rear].row = temp1;
que[rear].col = temp2;
que[rear].count = que[front].count + 1;
for(j=0;j<m;j++)
{
for(k=0;k<n;k++)
{
que[rear].flag[j][k]=que[front].flag[j][k];
}
}//对每一次路线都记录已经走过的格子
que[rear].flag[temp1][temp2]=1;
rear = (rear + 1) % MAX;
}
}
}
}
front = (front + 1) % MAX;
}
if(tag == 0)printf("NO\n");
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询