UVA 816 - Abbott's Revenge
2017-09-06 09:17
302 查看
题目大意:走迷宫,最多为九宫格,每个位置进入时的方向不同,可以选择的出位置方向不同(向前F,向左L,向右R),根据输入情况而定。输入入口,方向(NWSW),出口,接下来输入不同位置的进入方向和对应的出位置方向。求入口到出口的最短路径。
解题思路:某学长说的好,如果错了。。。那就重新在做一次。一个结构体用与记录路径(方向用数字代替),标记数组三维,x,y,和方向。一个结构体用于记录不同位置进入的和可出的位置(方向用字母表示,FLR变为NWSE)。bfs解决。相对的复杂一些。入口和出口一致时,也不可能直接到达,需要行进一步后再继续判断。dfs输出,注意格式。
ac代码:
解题思路:某学长说的好,如果错了。。。那就重新在做一次。一个结构体用与记录路径(方向用数字代替),标记数组三维,x,y,和方向。一个结构体用于记录不同位置进入的和可出的位置(方向用字母表示,FLR变为NWSE)。bfs解决。相对的复杂一些。入口和出口一致时,也不可能直接到达,需要行进一步后再继续判断。dfs输出,注意格式。
ac代码:
#include <iostream> #include <map> #include <cstring> using namespace std; struct node{ int x; int y; int dir; int pre; }no[1005]; struct point{ char in[10]; char out[10][10]; }po[20][20]; map <char, int>ma; char name[1005], dirs[4]={'N','W','S','E'}, tempdir; int goal[2], vis[20][20][4], temp, ten, front, rear; int dx[4]={-1, 0, 1, 0}, dy[4]={0, -1, 0, 1}, ex1, ex2; void read() { for (int i=0; i<4; i++) ma[dirs[i]] = i; int temp1, temp2, temp3, len, cnt; char ch[15]; while (1){ scanf("%d", &temp1); if (!temp1) break; scanf("%d%s", &temp2, ch); cnt = 0; memset(po[temp1][temp2].in, 0, sizeof(po[temp1][temp2].in)); for (int i=0; i<10; i++) for (int j=0; j<10; j++) memset(po[temp1][temp2].out[i], 0, sizeof(po[temp1][temp2].out[i])); while (strcmp(ch, "*")){ len = strlen(ch); for (int i=0; i<4; i++) if (ch[0] == dirs[i]) temp3 = i; po[temp1][temp2].in[cnt] = dirs[temp3]; for (int i=1; i<len; i++) if (ch[i] == 'F') po[temp1][temp2].out[cnt][i-1] = ch[0]; else if (ch[i] == 'L') po[temp1][temp2].out[cnt][i-1] = dirs[(temp3+1)%4]; else po[temp1][temp2].out[cnt][i-1] = dirs[(temp3+3)%4]; vis[temp1][temp2][temp3] = -1; scanf("%s", ch); cnt++; } } } bool bfs() { front = rear = 0; rear++; int X, Y, D, len1, len2, tt, kk; vis[no[0].x][no[0].y][no[0].dir] = -1; D = no[0].dir; no[0].x += dx[D]; no[0].y += dy[D]; while (front < rear){ D = no[front].dir; X = no[front].x; Y = no[front].y; if (X == goal[0] && Y == goal[1]) return 1; if (vis[X][Y][D] == -1){ len1 = strlen(po[X][Y].in); for (int i=0; i<len1; i++) if (ma[po[X][Y].in[i]] == D){ len2 = strlen(po[X][Y].out[i]); for (int j=0; j<len2; j++){ tt = ma[po[X][Y].out[i][j]]; no[rear].x = X + dx[tt]; no[rear].y = Y + dy[tt]; no[rear].dir = tt; no[rear++].pre = front; } } vis[X][Y][D] = 0; } front++; } return 0; } void prin(node a) { if (a.pre == -1){ printf(" (%d,%d)", a.x, a.y); ten++; return ; } else{ prin(no[a.pre]); if (!(ten % 10)) printf("\n "); printf(" (%d,%d)", a.x, a.y); ten++; } } int main() { while (scanf("%s", name)!=EOF && strcmp(name, "END")){ scanf("%d%d %c %d%d", &no[0].x, &no[0].y, &tempdir, &goal[0], &goal[1]); for (int i=0; i<4; i++) if (dirs[i] == tempdir) 4000 no[0].dir = i; ex1 = no[0].x, ex2 = no[0].y; no[0].pre = -1; memset(vis, 0, sizeof(vis)); read(); temp = bfs(); printf("%s\n", name); if (temp){ printf(" (%d,%d)", ex1, ex2); ten = 1; prin(no[front]); printf("\n"); } else printf(" No Solution Possible\n"); } return 0; }
相关文章推荐
- UVa 816 Abbott's Revenge
- Abbott's Revenge UVA 816(图 BFS)
- uva 816 - Abbott's Revenge(bfs)
- Uva816 Abbott's Revenge【bfs】【例题6-14】
- uva 816 Abbott's Revenge
- UVA 816 Abbott’s Revenge
- Abbott's Revenge UVA - 816(BFS典型例题)
- Uva - 816 - Abbott's Revenge
- UVA 816 Abbott's Revenge
- [BFS]Abbott’s Revenge, World Finals 2000, Uva816
- Uva-816 - Abbott's Revenge
- UVA 816 Abbott's Revenge BFS求最短路+路径输出(详细注释)
- uva 816 有点复杂的BFS和宽度优先搜索模板 Abbott's Revenge
- Abbott's Revenge UVA - 816
- UVa (一道比较复杂的广搜) 816 Abbott’s Revenge
- Uva oj 816 Abbott Revenge(BFS最短路)
- UVa 816 - Abbott's Revenge ( BFS )
- UVA - 816 Abbott's Revenge
- uva816 Abbott's Revenge
- UVA 816 - Abbott's Revenge(BFS)