题目链接:The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light

题解:

题意自己翻译,此题首先肯定是要广搜的,不过要开一个1e5*1e5的数组好像有点困难,

所以用结构体来存每个点的下标,然后从源点开始广搜。定义一个pair<node,int>,第一个存节点信息,第二个存到当前节点的步数,因为还要处理到达每个节点的状态。

状态:走奇数步并且状态为1与走偶数步状态为0的结果是一样的,都是向左或向右移动。走偶数步并且状态为1与走奇数步状态为0的结果也是一样,可以向上或向下移动。

最后定义一个pair的队列。

代码:

#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <string.h>
#include <utility>
#define IO ios::sync_with_stdio(0);\
    cin.tie();cout.tie();
using namespace std;
;
struct node
{
    int i,j,value;
    int flag;
} res[N];
pair <node,int> p;
queue <pair<node,int > > q;
int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        memset(res,,sizeof(res));
        ;
        cin >> n >> m;
        ; i <= n; i++)
            ; j <= m; j++)
                cin >> x,res[++num].value = x,res[num].i = i,res[num].j=j;
        int x1,y1,x2,y2;
        cin >>x1>>y1>>x2>>y2;
        ;
        res[(x1-)*m + y1].flag = ;
        p.first = res[(x1-)*m + y1];
        p.second = sum;
        q.push(p);
        ,ff = ;
        while(!q.empty())
        {
            pair <node,int> pp;
            pp = q.front();
            q.pop();
            int i = pp.first.i,j = pp.first.j,value = pp.first.value,sum = pp.second;
            if(i==x2&&j==y2)
            {
                ans = sum;
                ff = ;
                break;
            }
            )&&value==)||(!(sum&)&&value==))
            {
                <=n&&res[i*m+j].flag==)
                {
                    pp.first.i = i+,pp.first.j = j,pp.first.value = res[(i)*m+j].value;
                    pp.first.flag = ;
                    res[(i)*m+j].flag = ;
                    pp.second = sum+;
                    q.push(pp);
                }
                >&&res[(i-)*m+j].flag==)
                {
                    pp.first.i = i-,pp.first.j = j,pp.first.value = res[(i-)*m+j].value;
                    pp.first.flag = ;
                    res[(i-)*m+j].flag = ;
                    pp.second = sum+;
                    q.push(pp);
                }
            }
            else
            {
                <=m&&res[(i-)*m+j+].flag==)
                {
                    pp.first.i = i,pp.first.j = j+,pp.first.value = res[(i-)*m+j+].value;
                    pp.first.flag = ;
                    res[(i-)*m+j+].flag = ;
                    pp.second = sum+;
                    q.push(pp);
                }
                >&&res[(i-)*m+j-].flag==)
                {
                    pp.first.i = i,pp.first.j = j-,pp.first.value = res[(i-)*m+j-].value;
                    pp.first.flag = ;
                    res[(i-)*m+j-].flag = ;
                    pp.second = sum+;
                    q.push(pp);
                }
            }
        }
        printf();
        while(!q.empty())q.pop();
    }
    ;
}
/*
4
2 3
1 1 0
0 1 0
1 3 2 1
2 3
1 0 0
1 1 0
1 3 1 2
2 2
1 0
1 0
1 1 2 2
1 2
0 1
1 1 1 1
*/

zoj 4020 The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light(广搜)的更多相关文章

  1. ZOJ 4016 Mergeable Stack(from The 18th Zhejiang University Programming Contest Sponsored by TuSimple)

    模拟题,用链表来进行模拟 # include <stdio.h> # include <stdlib.h> typedef struct node { int num; str ...

  2. ZOJ 4019 Schrödinger's Knapsack (from The 18th Zhejiang University Programming Contest Sponsored by TuSimple)

    题意: 第一类物品的价值为k1,第二类物品价值为k2,背包的体积是 c ,第一类物品有n 个,每个体积为S11,S12,S13,S14.....S1n ; 第二类物品有 m 个,每个体积为 S21,S ...

  3. Mergeable Stack 直接list内置函数。(152 - The 18th Zhejiang University Programming Contest Sponsored by TuSimple)

    题意:模拟栈,正常pop,push,多一个merge A B 形象地说就是就是将栈B堆到栈A上. 题解:直接用list 的pop_back,push_back,splice 模拟, 坑:用splice ...

  4. 152 - - G Traffic Light 搜索(The 18th Zhejiang University Programming Contest Sponsored by TuSimple )

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5738 题意 给你一个map 每个格子里有一个红绿灯,用0,1表示 ...

  5. The 18th Zhejiang University Programming Contest Sponsored by TuSimple -C Mergeable Stack

    题目链接 题意: 题意简单,就是一个简单的数据结构,对栈的模拟操作,可用链表实现,也可以用C++的模板类来实现,但是要注意不能用cin cout,卡时间!!! 代码: #include <std ...

  6. The 18th Zhejiang University Programming Contest Sponsored by TuSimple

    Pretty Matrix Time Limit: 1 Second      Memory Limit: 65536 KB DreamGrid's birthday is coming. As hi ...

  7. The 19th Zhejiang University Programming Contest Sponsored by TuSimple (Mirror) B"Even Number Theory"(找规律???)

    传送门 题意: 给出了三个新定义: E-prime : ∀ num ∈ E,不存在两个偶数a,b,使得 num=a*b;(简言之,num的一对因子不能全为偶数) E-prime factorizati ...

  8. The 19th Zhejiang University Programming Contest Sponsored by TuSimple (Mirror)

    http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId=391 A     Thanks, TuSimple! Time ...

  9. The 17th Zhejiang University Programming Contest Sponsored by TuSimple J

    Knuth-Morris-Pratt Algorithm Time Limit: 1 Second      Memory Limit: 65536 KB In computer science, t ...

随机推荐

  1. 002.比较vector对象是否相等

    1.使用vector模板 //编写一段程序,比较vector对象是否相等 //注:该例类似于一个[彩票游戏] #include <iostream> #include <ctime& ...

  2. matlab求一个矩阵中各元素出现的个数(归一化)

    function [m,n] = stamatrix(a) %网上找到的方法,感觉很巧妙 x=a(:); x=sort(x); d=diff([x;max(x)+1]); count = diff(f ...

  3. 使用 html2canvas 实现浏览器截图

    基于上一篇<h5 本地上传图片预览 源码下载>,今天分享一个图片上传后, 根据所上传的图片颜值随机生成一个答案, 并且可以生成一张专属于自己的名片. 首先上传预览我们已经实现了, 所以接下 ...

  4. ZooKeeper Java例子(六)

    A Simple Watch Client 为了向你介绍ZooKeeper Java API,我们开发了一个非常简单的监视器客户端.ZooKeeper客户端监视一个ZooKeeper节点的改变并且通过 ...

  5. HDU 5869 Different GCD Subarray Query 树状数组+离线

    Problem Description This is a simple problem. The teacher gives Bob a list of problems about GCD (Gr ...

  6. 【BZOJ】1270 [BeijingWc2008]雷涛的小猫

    [算法]DP [题解]f1[i]表示第i棵树当前高度能得到的最多果子数 f2[i]表示高度i能得到的最多果子数. 于是有: f1[j]=max(f1[j],f2[i+delta])+mp[j][i]; ...

  7. Coursera在线学习---第八节.K-means聚类算法与主成分分析(PCA)

    一.K-means聚类中心初始化问题. 1)随机初始化各个簇类的中心,进行迭代,直到收敛,并计算代价函数J. 如果k=2~10,可以进行上述步骤100次,并分别计算代价函数J,选取J值最小的一种聚类情 ...

  8. OSI与TCP/IP各层的结构与功能,都有哪些协议

    前言: 今天更新一下计算机网络的一些非常重要的知识,可能很多人都不知学计算机网络有什么用,我想说的是它真的比较重要,像咱们学校只要是学计算机这个专业都要学习这门课程.另外大家要是去一些像BAT,阿里, ...

  9. Java案例之士兵作战功能实现

    实现的功能比较简单,主要用到了多态的,抽象类以及模板方法模式这几个知识点.效果图如下,哈哈 ,大神勿喷,后面我会把这些知识点详细介绍出来,即使Java学的不好,只要有一点其他语言基础或者没有应该都能看 ...

  10. perl中的lock

    #!/usr/bin/env perl -w use strict; use threads; use threads::shared; ; print "count的起始值为:$count ...