UVa810 A Dicey Problem 筛子难题
生活随笔
收集整理的這篇文章主要介紹了
UVa810 A Dicey Problem 筛子难题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
將一個篩子放在M*N的地圖上,每次翻滾前翻滾后的位置的點數必須要與翻滾前篩子的上面的點數相同,給定起點,找出一條可循路徑,返回起點。
這題好像沒有強調最短路徑,可以用dfs做,我TL了,用的bfs,可能是輸出路徑調用了棧,也可能是bfs判斷條件多了。
以后有好的想法再改吧
#include<iostream> #include <string> #include <string.h> #include<vector> #include<stack> #include<queue> using namespace std; const int MAXN = 10 + 5; struct Point {int x, y;int face, top;Point(){}Point(int _x,int _y):x(_x),y(_y){}Point(int _x,int _y,int _face,int _top):x(_x),y(_y),face(_face),top(_top){}ostream operator<<(const Point &u) {cout << "(" << u.x << u.y<<")";}bool operator==(const Point &u) {return x == u.x&&y == u.y;} }; struct Node {int x, y; int face, top;Node(){}Node(int _x,int _y,int _face,int _top):x(_x),y(_y),face(_face),top(_top){} }mr[MAXN][MAXN][MAXN][MAXN]; int r, c; int mp[MAXN][MAXN]; int totop[MAXN][MAXN]; int vis[MAXN][MAXN][MAXN][MAXN]; //vector<Point> lw[MAXN*MAXN]; int main() {string s;totop[1][2] = 4; totop[1][3]= 2; totop[1][4] = 5; totop[1][5] = 3;totop[2][1] = 3; totop[2][3]= 6; totop[2][4] = 1; totop[2][6] = 4;totop[3][1] = 5; totop[3][2]= 1; totop[3][5] = 6; totop[3][6] = 2;totop[4][1] = 2; totop[4][2]= 6; totop[4][5] = 1; totop[4][6] = 5;totop[5][1] = 4; totop[5][3]= 1; totop[5][4] = 6; totop[5][6] = 3;totop[6][2] = 3; totop[6][3]= 5; totop[6][4] = 2; totop[6][5] = 4;while (cin >> s && s != "END") {memset(vis, 0, sizeof(vis));memset(mp, 0, sizeof(mp));//memset(totop, 0, sizeof(totop));cin >> r>> c; Point startpos;cin >> startpos.x >> startpos.y;cin >> startpos.top >> startpos.face;Point endpos = startpos;for (int i = 1; i <= r; i++) {for (int j = 1; j <= c; j++) {cin >> mp[i][j];}}queue<Point>route;route.push(startpos);vis[startpos.x][startpos.y][startpos.face][startpos.top] = 1;bool ispack = false;while (!route.empty()) {Point nowpos = route.front();vis[nowpos.x][nowpos.y][nowpos.face][nowpos.top] = 1;route.pop();if (nowpos.x > 1&&nowpos.x<=r ) {if (mp[nowpos.x - 1][nowpos.y]&&!vis[nowpos.x - 1][nowpos.y][7 - nowpos.top][nowpos.face] && (nowpos.top == mp[nowpos.x - 1][nowpos.y] || mp[nowpos.x - 1][nowpos.y] == -1)) {route.push(Point(nowpos.x - 1, nowpos.y, 7 - nowpos.top, nowpos.face)); mr[nowpos.x - 1][nowpos.y][7 - nowpos.top][nowpos.face] = Node(nowpos.x, nowpos.y,nowpos.face,nowpos.top);if (nowpos.x - 1== startpos.x&&nowpos.y==startpos.y)break;}}if (nowpos.x >= 1 && nowpos.x < r) {if (mp[nowpos.x + 1][nowpos.y]&&!vis[nowpos.x + 1][nowpos.y][7 - nowpos.face][nowpos.top] && (nowpos.top == mp[nowpos.x + 1][nowpos.y] || mp[nowpos.x + 1][nowpos.y] == -1)) {route.push(Point(nowpos.x + 1, nowpos.y, nowpos.top,7 - nowpos.face)); mr[nowpos.x + 1][nowpos.y][nowpos.top][7 - nowpos.face] = Node(nowpos.x, nowpos.y, nowpos.face, nowpos.top);if (nowpos.x +1 == startpos.x&&nowpos.y == startpos.y)break;}}if (nowpos.y > 1 && nowpos.y <= c) {if (mp[nowpos.x][nowpos.y - 1] &&!vis[nowpos.x][nowpos.y - 1][nowpos.face][totop[nowpos.face][nowpos.top]] && (nowpos.top == mp[nowpos.x][nowpos.y - 1] || mp[nowpos.x ][nowpos.y-1] == -1)) {route.push(Point(nowpos.x, nowpos.y - 1, nowpos.face, totop[nowpos.face][nowpos.top])); mr[nowpos.x ][nowpos.y-1][nowpos.face][totop[nowpos.face][nowpos.top]] = Node(nowpos.x, nowpos.y, nowpos.face, nowpos.top);if (nowpos.x == startpos.x&&nowpos.y-1 == startpos.y)break;} }if (nowpos.y >= 1 && nowpos.y < c) {if (mp[nowpos.x][nowpos.y + 1]&&!vis[nowpos.x][nowpos.y + 1][nowpos.face][totop[nowpos.top][nowpos.face]] && (nowpos.top == mp[nowpos.x][nowpos.y + 1] || mp[nowpos.x ][nowpos.y+1] == -1)) {route.push(Point(nowpos.x, nowpos.y + 1, nowpos.face, totop[nowpos.top][nowpos.face])); mr[nowpos.x][nowpos.y + 1][nowpos.face][totop[nowpos.top][nowpos.face]] = Node(nowpos.x, nowpos.y, nowpos.face, nowpos.top);if (nowpos.x == startpos.x&&nowpos.y+1 == startpos.y)break;}}//next: report the route}if (route.empty())cout <<s<<endl<< " No Solution Possible" << endl;else {Node e = mr[route.front().x][route.front().y][route.front().face][route.front().top];stack<Node> st;// cout << "(" << endpos.x << "," << endpos.y << ")";st.push(Node(endpos.x, endpos.y, endpos.face, endpos.top));while (e.x != endpos.x || e.y != endpos.y) {//cout << "(" << e.x << "," << e.y << ")";st.push(e);e = mr[e.x][e.y][e.face][e.top];}st.push(Node(endpos.x, endpos.y, endpos.face, endpos.top));//cout << "(" << endpos.x << "," << endpos.y << ")";cout << s << endl<<" ";int i = 0;while (!st.empty()) {Node r = st.top();cout << "(" << r.x << "," << r.y << ")"; i++;st.pop();if (i % 9==0&&!st.empty()) { cout << endl << " "; }}cout << endl;}}//system("pause");return 0; }?
總結
以上是生活随笔為你收集整理的UVa810 A Dicey Problem 筛子难题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UVa10410 Tree Recons
- 下一篇: UVA 725 Division(除法)