UVA 10047 The Monocycle 解题报告
2013-06-21 21:51
369 查看
题意:给定一个轮子,轮子只能朝着一个反向运动。。轮子上有五种不同的颜色,给定一个迷宫,轮子在一个格子转动90度耗费的时间为1,向前的时间也为1 ,求起点和终点轮子覆盖的颜色相同的最短时间:
解题思路:开始以为visit只能代表迷宫的一个格子,原来它表示的是一种状态,,,用bfs ,,,str【】【】没初始化wrong了几次(注意啊)
解题代码:
View Code
解题思路:开始以为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
相关文章推荐
- UVAlive 6151 Beehives 解题报告
- 2018.1.30【 UVA - 10815 】解题报告(STL,set容器,string类)
- UVa 12018 - Juice Extractor 解题报告
- UVa 537 - Artificial Intelligence?解题报告
- UVa 465 - Overflow 解题报告
- UVA 10004 Bicoloring 解题报告
- UVa 10420 - List of Conquests解题报告
- 解题报告 之 UVA11054 Wine trading in Gergovia
- UVa 297 - Quadtrees解题报告
- 解题报告_Triangle Counting UVA - 11401_数学规律
- UVA 12013 Entertainment 解题报告
- Uva 1388 Graveyard 解题报告(数学)
- ***UVA12657 Boxes in a Line ACM解题报告(链表)
- UVa 1422 Processor 解题报告(二分)
- uva10935解题报告
- UVa 10827 Maximum sum on a torus 解题报告(前缀和)
- UVa -1584 Circular Sequence 解题报告 - C语言
- UVA 10878 解题报告
- UVA 755 解题报告
- Uva 580 - Critical Mass 解题报告(递推)