zoukankan      html  css  js  c++  java
  • LightOJ 1055 BFS暴力

    题目链接


    超级麻烦的一题,开始还理解错题目意思了。。。!看着udebug的数据才明白,重读一遍题目。。。
    三个机器人移动一致,碰到障碍物的机器人就无法向障碍物方向移动,机器人自己也算障碍物。麻烦在机器人当作障碍物的地方。

    标记状态BFS暴力


    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    #include <vector>
    #include <algorithm>
    #include <queue>
    #include <map>
    #include <string>
    #include <math.h>
    #include <ctype.h>
    using namespace std;
    typedef pair<int,int> P;
    typedef long long LL;
    const int INF = 0x3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-9;
    const int N = 100 + 5;
    int t,n,kase = 0;
    int dirx[] = {1,0,0,-1};
    int diry[] = {0,-1,1,0};
    char mp[15][15];
    int stx[3],sty[3];
    int edx[3],edy[3],edp;
    int d[12][12][12][12][12][12];
    struct Node
    {
        int ax,ay;
        int bx,by;
        int cx,cy;
        Node(){}
        Node(int a, int b, int c, int d, int e, int f)
        {
            ax = a; ay = b;
            bx = c; by = d;
            cx = e; cy = f;
        }
    };
    
    int bfs(int ax, int ay, int bx, int by, int cx, int cy)
    {
        queue<Node> Q;
        memset(d, -1, sizeof(d));
        Node s(ax,ay,bx,by,cx,cy);
        Q.push(s);
        d[ax][ay][bx][by][cx][cy] = 0;
        while(!Q.empty())
        {
            Node tmp = Q.front(); Q.pop();
            int ax = tmp.ax, ay = tmp.ay;
            int bx = tmp.bx, by = tmp.by;
            int cx = tmp.cx, cy = tmp.cy;
            for(int i = 0; i < 4; i++)
            {
                int nax = ax + dirx[i], nay = ay + diry[i];
                int fa = 0,fb = 0,fc = 0;
                if(nax < 0 || nay < 0 || nax >= n || nay >= n || mp[nax][nay] == '#')
                {
                    nax = ax, nay = ay; fa = 1;
                }
                int nbx = bx + dirx[i], nby = by + diry[i];
                if(nbx < 0 || nby < 0 || nbx >= n || nby >= n || mp[nbx][nby] == '#' )
                {
                    nbx = bx, nby = by; fb = 1;
                }
                int ncx = cx + dirx[i], ncy = cy + diry[i];
                if(ncx < 0 || ncy < 0 || ncx >= n || ncy >= n || mp[ncx][ncy] == '#' )
                {
                     ncx = cx, ncy = cy; fc = 1;
                }
                if(fa && nbx == nax && nby == nay) nbx = bx, nby = by, fb = 1;
                if(fa && ncx == nax && ncy == nay) ncx = cx, ncy = cy, fc = 1;
                if(fb && nax == nbx && nay == nby) nax = ax, nay = ay, fa = 1;
                if(fb && ncx == nbx && ncy == nby) ncx = cx, ncy = cy, fc = 1;
                if(fc && nbx == ncx && nby == ncy) nbx = bx, nby = by, fb = 1;
                if(fc && nax == ncx && nay == ncy) nax = ax, nay = ay, fa = 1;
                
                if(fa && nbx == nax && nby == nay) nbx = bx, nby = by, fb = 1;
                if(fa && ncx == nax && ncy == nay) ncx = cx, ncy = cy, fc = 1;
                if(fb && nax == nbx && nay == nby) nax = ax, nay = ay, fa = 1;
                if(fb && ncx == nbx && ncy == nby) ncx = cx, ncy = cy, fc = 1;
                if(fc && nbx == ncx && nby == ncy) nbx = bx, nby = by, fb = 1;
                if(fc && nax == ncx && nay == ncy) nax = ax, nay = ay, fa = 1;
                if(d[nax][nay][nbx][nby][ncx][ncy] >= 0) continue;
    
                int ans = d[ax][ay][bx][by][cx][cy] + 1;
                d[nax][nay][nbx][nby][ncx][ncy] = ans;
                if(mp[nax][nay] == 'X' && mp[nbx][nby] == 'X' && mp[ncx][ncy] == 'X')
                    return ans;
                Q.push(Node(nax,nay,nbx,nby,ncx,ncy));
            }
        }
        return -1;
    }
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        scanf("%d", &t);
        while(t--)
        {
            scanf("%d", &n);
            for(int i = 0; i < n; i++)
                scanf("%s", mp[i]);
            edp=0;
            for(int i = 0; i < n; i++)
            {
                for(int j = 0; j < n; j++)
                {
                    if(mp[i][j] == 'A')
                    {
                        mp[i][j] = '.';
                        stx[0] = i; sty[0] = j;
                    }
                    else if(mp[i][j] == 'B')
                    {
                        mp[i][j] = '.';
                        stx[1] = i; sty[1] = j;
                    }
                    else if(mp[i][j] == 'C')
                    {
                        mp[i][j] = '.';
                        stx[2] = i; sty[2] = j;
                    }
                }
            }
            int ans = bfs(stx[0],sty[0],stx[1],sty[1],stx[2],sty[2]);
            printf("Case %d: ", ++kase);
            if(ans == -1)
                printf("trapped
    ");
            else
                printf("%d
    ", ans);
        }
        return 0;
    }
    
  • 相关阅读:
    flex布局水平垂直居中
    react-native-vector-icons自定义图标
    RN真机USB 调试
    react-native 路由 react-native-router-flux
    使用react-native-router-flux路由的踩坑日记,问题点: 就是不报错,启动项目模拟器直接白屏闪退
    RN坑:如果你运行时出现了错误,但是你又把错误改了,但是你重跑项目,发现还是报你改错误前的错误,那么,你就得去清楚缓存啦,
    react-native调试方法
    Unable to load script.Make sure you‘re either running a metro server( run ‘react-native start‘ ) or that your bundle ‘index.android.bundle‘ is packaged correctly for release.
    React-Native样式表
    reactNative给webstorm创建代码模板
  • 原文地址:https://www.cnblogs.com/Alruddy/p/7400458.html
Copyright © 2011-2022 走看看