UVA10047-The Monocycle (BFS), monocyclebfs

Source: Internet
Author: User
Tags intel core i7

UVA10047-The Monocycle (BFS), monocyclebfs

Question Link


The wheel of a bicycle is divided into five slices and painted in five different colors. The bicycle is either riding to the next grid every 1 second, or turning left or right 90 .. At first, the bicycle is facing north and the color is green. When the target cell is reached, the base color must be green, but the orientation is unlimited. The shortest time to reach the target.

Train of Thought: Add two dimensions to the weight array, namely Orientation and color. Then we can use BFS to calculate the minimum time.

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int MAXN = 30;const int dx[] = {-1, 0, 1, 0};const int dy[] = {0, 1, 0, -1};struct node{    node (int xx, int yy, int dd, int cc, int tt) {        x = xx;         y = yy;        d = dd;        c = cc;        t = tt;    }    int x, y, d, c, t;};char g[MAXN][MAXN];int m, n, sx, sy, ex, ey;int vis[MAXN][MAXN][6][6];int bfs() {    queue<node> q;    while (!q.empty()) q.pop();    node s(sx, sy, 0, 0, 0);    q.push(s);    memset(vis, 0, sizeof(vis));    vis[sx][sy][0][0] = 1;    while (!q.empty()) {        node u = q.front();         q.pop();         if (u.x == ex && u.y == ey && u.c == 0)            return u.t;        for (int i = 0; i < 4; i++) {            node v = u;            if (i == u.d) {                v.x = u.x + dx[i];                v.y = u.y + dy[i];                 v.c = (u.c + 1) % 5;                v.t = u.t + 1;                if (v.x < 0 || v.x >= n || v.y < 0 || v.y >= m || g[v.x][v.y] == '#') continue;                if (!vis[v.x][v.y][v.d][v.c]) {                    vis[v.x][v.y][v.d][v.c] = 1;                    q.push(v);                }            }             else if ((u.d + 1) % 4 == i || (u.d + 3) % 4 == i){                if (!vis[v.x][v.y][i][v.c]) {                    vis[v.x][v.y][i][v.c] = 1;                    v.d = i;                    v.t = u.t + 1;                    q.push(v);                }            }        }     }    return -1; }int main() {    int t = 1, flag = 0;    while (scanf("%d%d", &n, &m) && n && m) {        for (int i = 0; i < n; i++)                     scanf("%s", g[i]);        for (int i = 0; i < n; i++) {            for (int j = 0; j < m; j++) {                if (g[i][j] == 'S') {                    sx = i;                     sy = j;                }                if (g[i][j] == 'T') {                    ex = i;                     ey = j;                }                  }          }         if (flag)             printf("\n");        flag = 1;        printf("Case #%d\n", t++);        int ans = bfs();        if (ans == -1)            printf("destination not reachable\n");        else             printf("minimum time = %d sec\n", ans);    }        return 0;}



ThinkPad T540p-20BFS02-J00 (Lenovo) 156 inch notebook how?

ThinkPad T540p 20BFS02J00 is equipped with the Fourth Generation Intelligent Intel Core i7-4700MQ processor, clock speed 2.4 GHz, when needed, the processor will automatically increase the processing speed, up to 3.4 GHz. It can handle multimedia production or major tasks and provide powerful processing performance. Integrated with Intel HD Graphics 4600 core Graphics card, it has 20 operating units and is equipped with an NVIDIA GeForce GT730M display chip and 1 GB independent Display memory, further enhancing the 3D Graphics processing capability. If necessary, the dual-graphics switch is automatically performed to ensure high performance while maximizing the endurance of the notebook. T540p 20BFS02J00 is an ideal choice for high performance. If it is only used to handle daily work, it does not need such an expensive notebook. The current price is more than nine thousand. Price Reference: www.amazon.cn/..semy5k


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.