• USACO The Tamworth Two


     这道题就是给你一张10*10的图和农夫和牛的位置, 农夫和牛刚开始往北走, 遇到障碍物或者边界会顺时针转向, 问你他们啥时候相遇。。不能相遇输出0.。直接将农夫和牛的位置和方向作为状态bfs即可。。代码如下:

    /*
        ID: m1500293
        LANG: C++
        PROG: ttwo
    */
    
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    
    using namespace std;
    char Map[30][30];
    int fx, fy, fdir;
    int cx, cy, cdir;
    int dx[] = {-1, 0, 1, 0};
    int dy[] = {0, 1, 0, -1};
    bool inside(int x, int y) { return (x>=0&&x<10&&y>=0&&y<10); }
    struct State
    {
        int fx, fy, fdir;
        int cx, cy, cdir;
        int time;
        State() {}
        State(int fx1, int fy1, int fdir1, int cx1, int cy1, int cdir1, int time1)
        {
            fx = fx1; fy = fy1; fdir = fdir1;
            cx = cx1; cy = cy1; cdir = cdir1;
            time = time1;
        }
    };
    
    int vis[11][11][5][11][11][5];
    
    int bfs()
    {
        memset(vis, 0, sizeof(vis));
        queue<State> que;
        que.push(State(fx, fy, fdir, cx, cy, cdir, 0));
        vis[fx][fy][fdir][cx][cy][cdir] = 1;
        while(!que.empty())
        {
            State tp = que.front(); que.pop();
            if(tp.fx==tp.cx && tp.fy==tp.cy)
                return tp.time;
            int nfx=tp.fx+dx[tp.fdir], nfy=tp.fy+dy[tp.fdir], nfdir=tp.fdir;
            if(!inside(nfx, nfy) || Map[nfx][nfy]=='*')
            {
                nfx=tp.fx; nfy=tp.fy; nfdir=(tp.fdir+1)%4;
            }
    
            int ncx=tp.cx+dx[tp.cdir], ncy=tp.cy+dy[tp.cdir], ncdir=tp.cdir;
            if(!inside(ncx, ncy) || Map[ncx][ncy]=='*')
            {
                ncx=tp.cx; ncy=tp.cy; ncdir=(tp.cdir+1)%4;
            }
            if(vis[nfx][nfy][nfdir][ncx][ncy][ncdir]) return -1;            //发现重复
            que.push(State(nfx, nfy, nfdir, ncx, ncy, ncdir, tp.time+1));
            vis[nfx][nfy][nfdir][ncx][ncy][ncdir] = 1;
        }
        return -1;
    }
    
    int main()
    {
        freopen("ttwo.in", "r", stdin);
        freopen("ttwo.out", "w", stdout);
        for(int i=0; i<10; i++)
        {
            scanf("%s", Map[i]);
            for(int j=0; j<10; j++)
            {
                if(Map[i][j] == 'F')
                {
                    fx=i; fy=j; fdir=0;
                    Map[i][j] = '.';
                }
                else if(Map[i][j] == 'C')
                {
                    cx=i; cy=j; cdir=0;
                    Map[i][j] = '.';
                }
            }
        }
        int res = bfs();
        if(res < 0)
            printf("0
    ");
        else
            printf("%d
    ", res);
        return 0;
    }
  • 相关阅读:
    从程序员到技术总监,分享10年开发经验
    CF739E Gosha is hunting
    hdu 4891 模拟
    hdu4888 最大流(构造矩阵)
    hdu4888 最大流(构造矩阵)
    hdu4885 有 限制的最短路
    hdu4885 有 限制的最短路
    hdu4884 模拟
    hdu4884 模拟
    POJ1789简单小生成树
  • 原文地址:https://www.cnblogs.com/xingxing1024/p/5077294.html
Copyright © 2020-2023  润新知