您的位置:首页 > 其它

UVA 10047 The Monocycle 解题报告

2013-06-21 21:51 369 查看
题意:给定一个轮子,轮子只能朝着一个反向运动。。轮子上有五种不同的颜色,给定一个迷宫,轮子在一个格子转动90度耗费的时间为1,向前的时间也为1 ,求起点和终点轮子覆盖的颜色相同的最短时间:

解题思路:开始以为visit只能代表迷宫的一个格子,原来它表示的是一种状态,,,用bfs ,,,str【】【】没初始化wrong了几次(注意啊)

解题代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
int dir,x,y,time,color;
}list[10000000];
int visit[30][30][10][10];//状态
int nextxdir[4] = {-1,0,1,0};
int nextydir[4] = {0,1,0,-1};//方向
int main()
{
int n,m,CASE = 0;
while(scanf("%d %d",&n,&m)!= EOF)
{
CASE ++;
if(n == 0 && m == 0 )
break;
memset(list,0,sizeof(list));
memset(visit,0,sizeof(visit));
char str[50][50];
memset(str,0,sizeof(str));
int bx,by,ex,ey;
for(int i = 1; i <= n; i ++)
{
scanf("%s",&str[i][1]);
for(int j = 1; j <= m; j ++)
{
if(str[i][j] == 'S')
{
bx = i;
by = j;
str[i][j] = '.';
}
if(str[i][j] == 'T')
{
ex = i ;
ey = j;
str[i][j] = '.';
}
}
}
list[1].dir = 0 ;
list[1].x = bx;
list[1].y = by;
list[1].time = 0;
list[1].color = 0 ;
visit[bx][by][0][0]  = 1;
int low = 1,high = 1 ,ok = 0;
while(low <= high)
{
if(list[low].x == ex && list[low].y == ey && list[low].color == 0)
{
ok = 1;
break;
}
for(int i = 0 ; i <= 3; i ++)
{
if(!visit[list[low].x][list[low].y][i][list[low].color] && i != (list[low].dir+2)%4)
{
visit[list[low].x][list[low].y][i][list[low].color] = 1;
high++;
list[high].dir = i ;
list[high].x = list[low].x;
list[high].y = list[low].y;
list[high].time = list[low].time+1;
list[high].color = list[low].color  ;
}
}//原地
int tx =  list[low].x + nextxdir[list[low].dir];
int ty =  list[low].y + nextydir[list[low].dir];
int tc = (list[low].color +1)%5;
if(!visit[tx][ty][list[low].dir][tc] && str[tx][ty] == '.')
{
visit[tx][ty][list[low].dir][tc] = 1;
high ++;
list[high].dir = list[low].dir ;
list[high].x = tx;
list[high].y = ty;
list[high].time = list[low].time+1;
list[high].color = tc  ;

}//前进
low++;
}
if(CASE != 1)
printf("\n");
printf("Case #%d\n",CASE);
if(ok)
printf("minimum time = %d sec\n",list[low].time);
else printf("destination not reachable\n");

}
return 0;

}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: