复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126#include <bits/stdc++.h> using namespace std; const int maxn = 20; struct Die { int x, y, tp, ft; Die() {} Die(int _x, int _y, int _tp, int _ft): x(_x), y(_y), tp(_tp), ft(_ft){} }; int dis[maxn][maxn][7][7]; int maze[maxn][maxn]; int top_front[maxn][maxn]; int r,c,beg,ed,btp,bft; int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};//下上右左 Die father[maxn][maxn][7][7]; void prepare() { top_front[1][5] = 3, top_front[1][3] = 2, top_front[1][2] = 4, top_front[1][4] = 5; top_front[2][1] = 3, top_front[2][3] = 6, top_front[2][6] = 4, top_front[2][4] = 1; top_front[3][1] = 5, top_front[3][5] = 6, top_front[3][6] = 2, top_front[3][2] = 1; top_front[4][1] = 2, top_front[4][2] = 6, top_front[4][6] = 5, top_front[4][5] = 1; top_front[5][1] = 4, top_front[5][4] = 6, top_front[5][6] = 3, top_front[5][3] = 1; top_front[6][2] = 3, top_front[6][3] = 5, top_front[6][5] = 4, top_front[6][4] = 2; } bool read() { string str; cin >> str; if(str=="END") return false; cout << str; memset(maze, 0, sizeof(maze)); memset(dis, -1, sizeof(dis)); memset(father, 0, sizeof(father)); cin >> r >> c >> beg >> ed >> btp >> bft; for(int i = 1; i <= r; i++) { for(int j = 1; j <= c; j++) cin >> maze[i][j]; } return true; } Die rotate(Die cur,int i) { Die next = cur; next.x += dir[i][0], next.y += dir[i][1]; if(i==0) {//上 next.ft = 7 - next.ft; swap(next.ft, next.tp); } else if(i==1) {//下 next.tp = 7 - next.tp; swap(next.ft, next.tp); } else if(i==2)//右 next.tp = top_front[cur.tp][cur.ft]; else //左 next.tp = 7-top_front[cur.tp][cur.ft]; return next; } bool inside(Die next) { return next.x>0 && next.x<=r && next.y>0 && next.y<=c; } bool feasible(Die next, Die cur) { if(maze[next.x][next.y]==-1) return true; else if(maze[next.x][next.y]==cur.tp) return true; return false; } void print(Die u) { vector<Die> vec; while(true) { vec.push_back(u); if(dis[u.x][u.y][u.tp][u.ft]==0) break; u = father[u.x][u.y][u.tp][u.ft]; } int cnt = 0; bool flag = false, f = false; for(int i = (int)vec.size()-1; i >= 0; i--) { if(cnt++%9==0) { if(!f) { cout << endl << " "; f = true; } else cout << "," << endl << " "; flag = false; } if(!flag) { cout << "(" << vec[i].x << "," << vec[i].y << ")"; flag = true; } else cout << ",(" << vec[i].x << "," << vec[i].y << ")"; } cout << ",(" << beg << "," << ed << ")"; cout << endl; } void bfs() { queue<Die> queue1; queue1.push(Die(beg,ed,btp,bft)); dis[beg][ed][btp][bft] = 0; while(!queue1.empty()) { Die cur = queue1.front(); queue1.pop(); for(int i = 0; i < 4; i++) { Die next = rotate(cur, i); cout << next.x << " " << next.y << " " << next.tp << " " << next.ft << endl; if(!inside(next) || !feasible(next, cur)) continue; if(next.x==beg && next.y==ed) { print(cur); return; } if(dis[next.x][next.y][next.tp][next.ft]<0) { queue1.push(next); dis[next.x][next.y][next.tp][next.ft] = dis[cur.x][cur.y][cur.tp][cur.ft]+1; father[next.x][next.y][next.tp][next.ft] = cur; } } } cout << "n No Solution Possible"<<endl; return; } int main() { freopen("i.txt","r",stdin);; freopen("o.txt","w",stdout); prepare(); while(read()) { bfs(); } return 0; }
最后
以上就是鲤鱼花瓣最近收集整理的关于UVa 810 - A Dicey Problem的全部内容,更多相关UVa内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复