The Monocycle

来源:互联网 发布:java程序的执行过程 编辑:程序博客网 时间:2024/04/29 11:57

题目链接

  • 分析:
    此题重点在于明白什么是一个点的状态。对于之前接触的普通的dfs判重,一个点的状态就是位置即xy坐标
  • 理解:
    首先明白前提或者目标是使一个点的信息尽可能少。一个点可以记录多个信息,如果不同的信息之间有优劣性(除过信息p,其他信息都相同的两个点可以判断出孰优孰劣),那么就不用算作点的状态(因为可以通过其他方式来筛选出更优的点,以减少点的状态数),对于dfs来说,一个点的信息有x坐标、y坐标、到达这个点的步数,显然步数是有明显的优劣性的,所以不用记录。
    对于这个题来说,点的信息比较多:x坐标、y坐标、到达这个点的步数、时间、方向。显然,对于其他信息都相同而时间不同的点可以判断出优劣性。那么就需要记录4个信息即可。
const int MAXN = 30;struct Node{    int x, y, dir, step, time;    Node (int a = 0, int b = 0, int c = 0, int d = 0, int e = 0)    {        x = a, y = b, dir = c, step = d, time = e;    }} tn;//点的x坐标,y坐标,方向,车轮颜色bool vis[MAXN][MAXN][4][5];char m[MAXN][MAXN];queue<Node> q;int kase, a, b, ansx, ansy;int dx[] = {-1, 0, 1, 0};int dy[] = {0, 1, 0, -1};void move(int nx, int ny, int nd, int ns, int nt){    if (m[nx][ny] != '#' && !vis[nx][ny][nd][ns % 5])    {        vis[nx][ny][nd][ns % 5] = true;        q.push(Node(nx, ny, nd, ns, nt));    }}bool bfs(Node& ans){    while (!q.empty())    {        ans = q.front();        q.pop();        if (ans.x == ansx && ans.y == ansy && ans.step % 5 == 0)            return true;        move(ans.x, ans.y, (ans.dir + 3) % 4, ans.step, ans.time + 1);        move(ans.x, ans.y, (ans.dir + 1) % 4, ans.step, ans.time + 1);        move(ans.x + dx[ans.dir], ans.y + dy[ans.dir], ans.dir, ans.step + 1, ans.time + 1);    }    return false;}int main(){//    freopen("in.txt", "r", stdin);    int kase = 1;    while (~RII(a, b) && a)    {        if (kase != 1) puts("");        CLR(vis, false);        CLR(m, '#');        while (!q.empty()) q.pop();        FE(i, 1, a) FE(j, 1, b)        {            cin >> m[i][j];            if (m[i][j] == 'S')            {                vis[i][j][0][0] = true;                tn = Node(i, j, 0, 0, 0);                q.push(tn);            }            else if (m[i][j] == 'T')                ansx = i, ansy = j;        }        printf("Case #%d\n", kase++);        if (bfs(tn))            printf("minimum time = %d sec\n", tn.time);        else            puts("destination not reachable");    }    return 0;}


7 0
原创粉丝点击